Info

This question is closed. Reopen it to edit or answer.

How can I get N to be a different value for a different t?

1 view (last 30 days)
Im working on an SIR model for my physics lab. And to get a sort of general idea behid quarantine measures, I would want N in the following code to change for different intervals, ie be N=100000 for t>=0 & t<100 and N=30000 for t>100 but am unsure how to implement this. Keep in mind coding is very very new to me and would like some simple models.
%To solve the equations dS/dt = -(k/N)*S* I, dI/dt=(k/N)*S*I-g*I, dR/dt=g*I
%I defined values for k, N, and g in accordance with Alexander Ware's work to see if I could reach a similar graph
k=3/14;
N=100000;
g=1/14;
t=linspace(0,365,365);
f = @(t,x) [-(k/N)*x(1)*x(2);(k/N)*x(1)*x(2)-g*x(2);g*x(2)];
%The @(t,x) term is just the function handle. Here I have created a function with three pieces, each representing the right sides of the equations above.
%In place, I have x(1)=S, x(2)=I, and x(3)=R
[t,xa]=ode45(f,[0 365], [N 500 0]);
%ode45 takes each term in the function f and integrates the function from 0 to 365. [N 500 0] represents the inital conditions for S, I, and R respectively
a1=plot(t,xa(:,1)); M1='S';
hold on
%Hold allows for the addition of more plots without deleting the first
a2=plot(t,xa(:,2),'k'); M2='I';
a3=plot(t,xa(:,3),'r'); M3='R';
hold off
%Blue curve represents S, red curve represents R, and black represents I.
legend([a1, a2 a3], M1, M2, M3)

Answers (1)

darova
darova on 30 Apr 2020
Try this modification

This question is closed.

Community Treasure Hunt

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

Start Hunting!