How to use ODE 45 to generate a SIR model

7 次查看(过去 30 天)
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

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by