error in ode45

5 visualizaciones (últimos 30 días)
Boyu Liu
Boyu Liu el 5 de Jun. de 2019
Respondida: Walter Roberson el 5 de Jun. de 2019
Th = 10;
Tts= 10;
Qspec= 700000;
F= 10000;
T0= 350;
dsty=1000;
Ca0= 1;
k0=1.97*10^24;
Ea= 20000;
Cp= 1;
Hrxn=160000;
Vr=100;
function set=ODEs(t,v)
set(1)=(1/Th)*(Qspec-v(1));
set(2)=F*(Ca0-v(2))/(dsty*Vr)-k0*v(2)*exp(-Ea/v(3));
set(3)=F*(T0-v(3))/(Vr*dsty)-(Hrxn*v(2)*k0*exp(-Ea/v(3)))/(dsty*Cp)-v(1);
set(4)=(1/Tts)*(v(3)-v(4));
set=set(:);
end
%%
Qzero=700000;
Cazero=0.25;
Tzero=350;
Tszero=350;
init=[Qzero Cazero Tzero Tszero];
tspan=[0 200];
[t,v]=ode45(@ODEs,tspan, init);
Q=v(:,1);
Ca=v(:,2);
T=v(:,3);
Ts=v(:,4);
These are two separate parts that I use. But this will give errors like below.
odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Untitled5 (line 7)
[t,v]=ode45(@ODEs,tspan, init);

Respuestas (1)

Walter Roberson
Walter Roberson el 5 de Jun. de 2019
Assigning to variables in the script at the top affects the base workspace.
Calling
set(1)=(1/Th)*(Qspec-v(1));
within the function ODEs causes MATLAB to look in the direct workspace of the function ODEs first, then at the parameter list in the ODEs function header, and then in lexically enclosing functions (nested functions) for shared variables, and then for functions with that name. Within a function, the base workspace is never referred to unless the user specifically uses evalin('base') or assignin('base')
You mention having two parts. It is not valid to have a script (the part starting from Qzero) after a function, so we deduce that you must be splitting at the %% with the first file being a script that has a function ODEs at the end of it, and the second part being the script that tries to invoke ODEs through the ode45() call. However, when you define a function inside a script, it is private to the script, and is only available to other functions or scripts if the defining script deliberately creates a function handle and stores it somewhere the other execution unit can get access to.
The easiest solution is to move
function set=ODEs(t,v)
to the top of that script, and save that in ODEs.m

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by