Error using odearguments with a symbolic system of differential equations
Show older comments
Hello Everyone,
I got the following errors:
Error using odearguments (line 113)
Inputs must be floats, namely single or double.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in SxMatrix_grinding (line 11)
[t1, sol1] = ode45('Sxd1', tspan1, IC1);
My function is:
function output=Sxd1(t, X)
syms u x1 x2 k1 k2 k3 k4 k5 k1_ k2_ k3_ k4_ k5_ x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14
u=0.0254;
var=[x1;x2];
k=[k1_;k2_;k3_;k4_; k5_];
f=[k1*k1_*x2;
(u-x2)/(k2*k2_+k3*k3_*x1)];
dfdk=jacobian(f,k);
dfdx=jacobian(f,var);
Sx = [x3 x5 x7 x9 x11; x4 x6 x8 x10 x12];
Sxd=dfdx*Sx+dfdk;
x1=X(1);
x2=X(2);
x3=X(3);
x4=X(4);
x5=X(5);
x6=X(6);
x7=X(7);
x8=X(8);
x9=X(9);
x10=X(10);
x11=X(11);
x12=X(12);
output=[k1*k1_*x2;
(u-x2)/(k2*k2_+k3*k3_*x1);
Sxd(:)];
output=subs(output, {k1, k2, k3, k4, k1_, k2_, k3_, k4_}, {220, 0.7463, 0.00216, 88000, 1, 1, 1, 1});
end
and my main code is:
tspan1 =[0 9.5];
%options = odeset('RelTol', 1e-36, 'AbsTol', 1e-50);
IC1=zeros(1,12); IC1(11)=1;
[t1, sol1] = ode45('Sxd1', tspan1, IC1);
I am having issues understaing the reason of the error I got. Any help is really appreciated!
Thank you in advance!
8 Comments
Walter Roberson
on 21 Sep 2020
dfdk=jacobian(f,k);
It is a waste of time to do that calculation every time. Do it once before-hand, and use odeFunction() or matlabFunction() to convert to a function handle that works on numeric values.
Gabriele Galli
on 21 Sep 2020
Walter Roberson
on 21 Sep 2020
syms u x1 x2 k1 k2 k3 k4 k5 k1_ k2_ k3_ k4_ k5_ x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14
at that point, x1 through x14 are symbolic
var=[x1;x2];
f=[k1*k1_*x2;
(u-x2)/(k2*k2_+k3*k3_*x1)];
...
Sx = [x3 x5 x7 x9 x11; x4 x6 x8 x10 x12];
Those use the symbolic x1 through x12 values.
Sxd=dfdx*Sx+dfdk;
Still symbolic.
x1=X(1);
x2=X(2);
etc
Okay now, at the MATLAB level, x1 through x12 are assigned numeric values. But only at the MATLAB level. This does not affect Sxd, which still uses the symbolic variables.
The situation is exactly like
A = 1
B = A + 1
A = 2
B does not suddenly become 3. B is not a formula: at the time that B was assigned to, the current value of A was copied and used in the computation to create B, and B then promptly forgets that it knew anything about the source variable A.
Just the same way if you have
syms A
B = A + 1
A = 2
then B does not suddenly become 3. At the time that B was assigned to, the current value of A was copied and used in the computation to create B. That current value is sym('A'), a reference to a symbol that lives inside the symbolic engine. B promptly forgets that it knew anything about where that sym('A') came from. B is not "reference to variable A, dereference, add 1": B is sym('A')+1 and no change to MATLAB variable A will affect B unless you tell MATLAB to force it to.
output=[k1*k1_*x2;
(u-x2)/(k2*k2_+k3*k3_*x1);
Sxd(:)];
At that point, the x1 and x2 that you can see in the first two lines of output have become the input numeric variables. However, Sxd has not been affected, and still contains vectors of symbolic variables including sym('x1') and sym('x2')
output=subs(output, {k1, k2, k3, k4, k1_, k2_, k3_, k4_}, {220, 0.7463, 0.00216, 88000, 1, 1, 1, 1});
Okay, you have explicitly told MATLAB to look inside output to find references to the variables indicated by those k* and k*_ and replace them with specific numeric values. That is generally a good thing to do.
But you are not telling it to replace any of the symbolic x* values, so output still potentially contains references to symbolic sym('x1') through sym('x14') . And that is why the double() fails.
Gabriele Galli
on 21 Sep 2020
Walter Roberson
on 21 Sep 2020
output = subs(output);
Gabriele Galli
on 21 Sep 2020
Walter Roberson
on 21 Sep 2020
Try with @Sxd1 instead of 'Sxd1'
Double check class() tspan1 and IC1
Gabriele Galli
on 21 Sep 2020
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations 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!