Symbolic function doesn't work
12 views (last 30 days)
Show older comments
syms L alpha gamma Y2 Y3 Y4 Y5 Y6 x X dL T(L,x)
syms T(L,x)
M1 = 0;
M2 = 7;
Y1 = 1 + M2 / 10;
Y7 = 3 + M1 / 10;
xcp = [0 0.1*L 0.25*L 0.5*L 0.6*L 0.75*L L];
ycp = [Y1 Y2 Y3 Y4 Y5 Y6 Y7];
T0 = 5 + 1/L - 25/(L^2);
S = bezier_syms();
ode = diff(T,x) == (1+alpha*M1)*(T^2)*S(L,x,Y2,Y3,Y4,Y5,Y6) - 4*(2 + gamma*M2)*x*T;
derivL = diff(ode,L);
U = diff(T,L);
eqns = [derivL, diff(T,L) == dL];
eqns = matlabFunction(eqns);
sol = ode45(eqns,[0 5],diff(T0,L));
I get the error:
Warning: Function 'T' not verified to be a valid MATLAB function.
For some reason it doesn't let me define the T function. Any ideas?
Edit:
function S = bezier_syms()
syms t x y s X Y2 Y3 Y4 Y5 Y6 L
M1 = 0;
M2 = 7;
Y1 = 1 + M2 / 10;
Y7 = 3 + M1 / 10;
xcp = [0 0.1*L 0.25*L 0.5*L 0.6*L 0.75*L L];
ycp = [Y1 Y2 Y3 Y4 Y5 Y6 Y7];
N = length(xcp) - 1;
% Calculate m matrix
m = zeros(N+1,N+1);
for i=0:N
for j=0:N
if i <= j
m(i+1,j+1) = ((-1)^(j-i)) * nchoosek(N,j) * nchoosek(j,i);
end
end
end
% Calculate C
T = sym(zeros(N+1,1));
for i = 1:length(T)
T(i) = t^(i-1);
end
C = m * T;
x = 0;
y = 0;
for i = 1:N+1
x = x + xcp(i)*C(i);
y = y + ycp(i)*C(i);
end
% Convert syms to functions
getx = matlabFunction(x);
gety = matlabFunction(y);
% Polynomial regression
regt = linspace(0,1,8);
n = length(regt);
xreg = sym(zeros(n,1));
Yreg = sym(zeros(n,1));
for i=1:n
xreg(i) = getx(L,regt(i));
Yreg(i) = gety(Y2,Y3,Y4,Y5,Y6,regt(i));
end
Xreg = sym(zeros(n,n));
Xreg(:,1) = ones(n,1);
for i=1:(n-1)
Xreg(:,i+1) = xreg.^i;
end
coef = inv(Xreg' * Xreg) * Xreg' * Yreg; %#ok<MINV>
s = 0;
for i=1:n
s = s + coef(i)*(X^(i-1));
end
S = matlabFunction(s);
end
8 Comments
Star Strider
on 22 Dec 2020
I posted an Answer to your other Question (that you cited in your Comment) just now.
Answers (0)
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!