Is the time vector t for the function odefun() inside ode45() always required?

Hi
I am new to using ode45 in Matlab and there is a question that popped up.
When I call the odefun function, which is called "ODE_SEIR" in my case (see below), I was wondering why it is necessary to specify the time vector "timevec" even if ODE_SEIR does not use it? I tried removing timevec in the place marked bold below but for some reason the script doesn't run then and I get the following output:
error code
Index exceeds the number of array elements (1).
Error in ODE_SEIR (line 11)
E = initial(2);
Error in SEIR>@(value_initial,param)ODE_SEIR(value_initial,param)
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 SEIR (line 52)
[timevec, sol] = ode45(@(value_initial, param)...
It looks like ode45 interprets the parameter value_initial (called "initial" inside the ODE_SEIR function) as the time vector timevec. I assume that the time vector just always has to be added as a parameter but I am not sure. Thanks a lot for your help!
main script
% Integrieren des Systems (Differentialgleichungen) von t0 bis tend (= timevec)
% und den jeweiligen Anfangsbedingungen.
[timevec, sol] = ode45(@(timevec, value_initial, param)...
ODE_SEIR(timevec, value_initial, param),tspan, value_initial);
function ODE_SEIR
function deriv = ODE_SEIR(~, initial, param)
% Herauslesen der Anfangsbedingungen für die ODEs
S = initial(1);
E = initial(2);
I = initial(3);
R = initial(4);
% Berechnung der ODEs
dS = -param.beta*I*S/param.population;
dE = param.beta*I*S/param.population-param.alpha*E;
dI = param.alpha*E-param.gamma*I;
dR = param.gamma*I;
deriv = [dS; dE; dI; dR];
end

4 件のコメント

madhan ravi
madhan ravi 2020 年 3 月 29 日
編集済み: madhan ravi 2020 年 3 月 29 日
Kannst du die Daten als .mat hochladen? Vielleicht kannst du den folgenden Code ausprobieren.
[timevec, sol] = ode45(@(t, initial) % guck mal hier ...
ODE_SEIR(t, initial, param),tspan, value_initial);
darova
darova 2020 年 3 月 29 日
Can you show also value_initial?
Martin Vontobel
Martin Vontobel 2020 年 4 月 4 日
Hi Madhan Ravi, thanks for your comment. It looks like Stephen found the answer to the question in the Matlab documentation (see his answer below). Best wishes, Martin
Martin Vontobel
Martin Vontobel 2020 年 4 月 4 日
Hi Darova,
Thanks for pointing out that the argument "param" was superfluous there, where I put it in the first place.

サインインしてコメントする。

 採用された回答

Steven Lord
Steven Lord 2020 年 3 月 29 日

0 投票

From the documentation page for the ode45 function "The function dydt = odefun(t,y), for a scalar t and a column vector y, must return a column vector dydt of data type single or double that corresponds to f(t,y). odefun must accept both input arguments, t and y, even if one of the arguments is not used in the function."

2 件のコメント

Martin Vontobel
Martin Vontobel 2020 年 4 月 4 日
Hi Stephen, thanks a lot for your reply. I did not find this comment you mentioned above in the MATLAB documentation. Your reply confirms what I assumed might be the case. Cheers, Martin

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by