Error using odearguments with a symbolic system of differential equations

10 Ansichten (letzte 30 Tage)
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 Kommentare
Walter Roberson
Walter Roberson am 21 Sep. 2020
Try with @Sxd1 instead of 'Sxd1'
Double check class() tspan1 and IC1
Gabriele Galli
Gabriele Galli am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Steven Lord
Steven Lord am 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 Kommentare
Steven Lord
Steven Lord am 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 am 23 Sep. 2020
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by