How to use ODE 45 to generate a SIR model

22 visualizaciones (últimos 30 días)
Zhukun Wang
Zhukun Wang el 23 de Feb. de 2021
Comentada: darova el 24 de Feb. de 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!

Respuestas (1)

Alan Stevens
Alan Stevens el 23 de Feb. de 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 comentarios
Zhukun Wang
Zhukun Wang el 23 de Feb. de 2021
Gotcha, that works. Thanks so much!
darova
darova el 24 de Feb. de 2021

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by