How to use ODE 45 to generate a SIR model

22 ビュー (過去 30 日間)
Zhukun Wang
Zhukun Wang 2021 年 2 月 23 日
コメント済み: darova 2021 年 2 月 24 日
function Math462hw2Q6parte
alpha=1.99;
beta=1;
gamma=1/7;
S=0.99;
I=0.01;
R=0.1;
%tstart=0;
%tend=100;
y=[S,I,R];
tspan=0:1:100;
[t,soln]=ode45(@(t,y)dotfunctions,tspan,y);
%plot function
plot(t,soln);
xlabel('time');
ylabel('value');
title('double pendulum');
function ddt=dotfunctions(~,sir)
S=sir(1);
I=sir(2);
R=sir(3);
Sdot=-beta*S*I+alpha*R;
Idot=beta*S*I-gamma*I;
Rdot=gamma*I-alpha*R;
ddt=[S,I,R,Sdot,Idot,Rdot];
end
end
The above is my code and MATLAB keeps saying the line with the function ode45 is not right. I do not know where the mistakes are. Could someone help me figure it out? THANKS!

回答 (1 件)

Alan Stevens
Alan Stevens 2021 年 2 月 23 日
Like so:
alpha=1.99;
beta=1;
gamma=1/7;
S=0.99;
I=0.01;
R=0.1;
%tstart=0;
%tend=100;
y=[S,I,R];
tspan=0:1:100;
[t,soln]=ode45(@(t,y)dotfunctions(t,y,alpha,beta,gamma),tspan,y); % Pass constants to function
%plot function
plot(t,soln);
xlabel('time');
ylabel('value');
title('double pendulum');
function ddt=dotfunctions(~,sir,alpha,beta,gamma)
S=sir(1);
I=sir(2);
R=sir(3);
Sdot=-beta*S*I+alpha*R;
Idot=beta*S*I-gamma*I;
Rdot=gamma*I-alpha*R;
ddt=[Sdot;Idot;Rdot]; % Just the gradients. Must be column vector
end
  2 件のコメント
Zhukun Wang
Zhukun Wang 2021 年 2 月 23 日
Gotcha, that works. Thanks so much!
darova
darova 2021 年 2 月 24 日

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

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by