Symbolic function doesn't work
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 suspect the problem is ‘S’. We can’t run this because we don’t have the function.
Meanwhile:
syms T(L,x)
Eqn = diff(T,L) + diff(T,x)
produces:
Eqn(L, x) =
diff(T(L, x), L) + diff(T(L, x), x)
so ‘T’ itself is not the problem.
John F
on 22 Dec 2020
Walter Roberson
on 22 Dec 2020
use odeFunction not matlabFunction when you are working with ode
Star Strider
on 22 Dec 2020
The problem is likely that ‘T’ is a multivariate function, so you are attempting to have ode45 solve a partial differential equation. It is not designed to do that.
I usually use odeToVectorField before using matlabFunction for systems of equations. When I tried that here:
eqns1 = [derivL, diff(T,L) == dL];
eqns2 = odeToVectorField(eqns1);
the error was:
Error using odeToVectorField>mupadOdeToVectorField (line 160)
Symbolic ODEs must have exactly one independent variable.
You need to revisit this to understand what you want to do, and what you want the ordinary differential equation solvers to do.
I also suspect that ‘S’ needs to be defined and called in your code with all its arguments, rather that the way you are calling it now.
John F
on 22 Dec 2020
Walter Roberson
on 22 Dec 2020
eqns = [derivL, diff(T,L) == dL];
You cannot include boundary conditions in the equations. Please read the first example for odeFunction to see the work flow -- but also take into consideration that ode45 cannot be used for partial differential equations
John F
on 22 Dec 2020
Star Strider
on 22 Dec 2020
I posted an Answer to your other Question (that you cited in your Comment) just now.
Answers (0)
Categories
Find more on Equation Solving 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!