MATLAB Answers

Error using odearguments with a symbolic system of differential equations

5 views (last 30 days)
Gabriele Galli
Gabriele Galli on 20 Sep 2020
Commented: Walter Roberson on 23 Sep 2020 at 21:48
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

Show 5 older comments
Gabriele Galli
Gabriele Galli on 21 Sep 2020
Thank you,
I tried to add to the function
...
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});
output=subs(output);
end
but unfortunately I am still getting the same error:
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);
Gabriele Galli
Gabriele Galli on 21 Sep 2020
I am still getting the same error. This is the way I called the function:
tspan1 =[0 9.5];
IC1=zeros(1,12); IC1(11)=1;
[t1, sol1] = ode45(@(t,X) Sxd1(t,X), tspan1, IC1);
Plus, tspan and IC1 looks right to me.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 21 Sep 2020
Your ODE function can use symbolic calculations internally but it must return a double or single array to the ODE solver. Call double on your output immediately before the end of your ODE function.
Alternately if you want to solve it symbolically (and if you want to use such stringent tolerances you may) use the functions included in Symbolic Math Toolbox for solving differential equations like dsolve.

  4 Comments

Show 1 older comment
Gabriele Galli
Gabriele Galli on 21 Sep 2020
Hello Steven,
thank you for the help, I have already tried using
output=double(output)
but still doesn't work, I got the following error:
Error using symengine
Unable to convert expression into double array.
Error in sym/double (line 698)
Xstr = mupadmex('symobj::double', S.s, 0);
Error in Sxd1 (line 37)
output=double(output)
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
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);
Concerning the tolerance, it is ok using a less stringent tolerance as well.
Thank you!
Steven Lord
Steven Lord on 21 Sep 2020
Set an error breakpoint and run your code. When MATLAB stops, select the workspace of the Sxd1 function and look at the contents of the variable output. Does it contain any symbolic variables? [It does.] If it does, substitute values for all those symbolic variables into output then try converting it to double again.
Walter Roberson
Walter Roberson on 23 Sep 2020 at 21:48
We would need to see your current code to say more.
But I repeat my advice from https://www.mathworks.com/matlabcentral/answers/596998-error-using-odearguments-with-a-symbolic-system-of-differential-equations#comment_1016971 : Do not do that symbolic calculation each time. Do the symbolic calculation once and use odeFunction or matlabFunction to generate a function handle for you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by