Error using odearguments with a symbolic system of differential equations

2 次查看(过去 30 天)
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 个评论
Gabriele Galli
Gabriele Galli 2020-9-21
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.

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2020-9-21
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 个评论
Steven Lord
Steven Lord 2020-9-21
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 2020-9-23
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Equation Solving 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by