How to use ODE 45 to generate a SIR model

24 vues (au cours des 30 derniers jours)
Zhukun Wang
Zhukun Wang le 23 Fév 2021
Commenté : darova le 24 Fév 2021
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!

Réponses (1)

Alan Stevens
Alan Stevens le 23 Fév 2021
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 commentaires
Zhukun Wang
Zhukun Wang le 23 Fév 2021
Gotcha, that works. Thanks so much!
darova
darova le 24 Fév 2021

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by