필터 지우기
필터 지우기

How to use ODE 45 to generate a SIR model

조회 수: 7 (최근 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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by