# how to plot graph for SIR epidemic model

59 views (last 30 days)
Ruksana Shaikh on 18 Jan 2022
Commented: Star Strider on 20 Jan 2022
my SIR epidemic model is
dS/dt= beta-lembda*I*S-u*S;
dI/dt=lembda*I*S-u*I-gamma*R;
dR/dt=gamma*R-u*R;
t=0:1:100;
gamma=.10;
lembda=.5;
u=.2;
S=50;I=20;R=30;
Ruksana Shaikh on 18 Jan 2022
function x = sir(t, x)
x(1)=0:50:100;
x(2)=0:10:50;
x(3)=0:10:70;
beta = .5;
delta = .3;
x(1) = -beta * x(1) * x(2);
x(2) = beta * x(1) * x(2) - delta * x(2);
x(3) = delta * x(2);
plot (t,x(1),t,x(2),t,x(3))
end

Star Strider on 18 Jan 2022
I beleive ‘sir’ is supposed to be a system of ordinary differential equations.
If so, this will get you started —
tspan = linspace(0, 150, 250); % Supply Correct Time Vector
ic = [1 0 0]+1E-7; % Supply Correct Initial Conditions
[t,x] = ode45(@sir, tspan, ic);
figure
plot(t,x(:,1), t,x(:,2), t,x(:,3))
grid
legend('x_1','x_2','x_3', 'Location','best') function dx = sir(t, x)
beta = .5;
delta = .3;
dx(1,:) = -beta * x(1) * x(2);
dx(2,:) = beta * x(1) * x(2) - delta * x(2);
dx(3,:) = delta * x(2);
end
It is not good programming practice to assign ‘x’ to the results of the calculation, since this replaces the original ‘x’ values with the calculated values, and will not produce the correct result. The only substantive changes I made to the function were to eliminate the vectors for the various values of ‘x’ (that would overwrite the values passed as arguments to the function and would not have worked regardless), changed the results of the calculations to ‘dx’ instead, and added the second empty dimension to be certain that the ‘dx’ result is a column vector.
Please thoroughly review the documentation for ode45 to understand what it does and what it needs with respect to the functions it calls.
.
Star Strider on 20 Jan 2022
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

R2021b

### Community Treasure Hunt

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

Start Hunting!