Graphing several curves of a variable for various values of a parameter

3 次查看(过去 30 天)
I am trying to produce a graph like the one below for an SIR model.
In particular, I would like to draw five plots of the variable I (denoted in the code as y(2) for the values of beta = (0, 0,2, 0.4, 0.6, 0.8). The code that I used is given below. Any help would be highly appreciated.
%Plotting on command window
[t,y] = ode23s(@SIR_Model, [0 180], [1 1.13*10^(-5) 0]);
plot(t,y(:,2))
title('Number of infections')
xlabel('t')
ylabel('I')
function dy=SIR_Model(t,y)
%Create an empty vector
dy = zeros(3,1);
%Parameters
beta = 1/3; %transmission coefficient
gamma = 1/4; %recovery rate
%ODES
dy(1) = -beta*y(1)*y(2);
dy(2) = beta*y(1)*y(2) - gamma*y(2);
dy(3) = gamma*y(2);
end

采纳的回答

Matt J
Matt J 2023-3-27
编辑:Matt J 2023-3-27
%Plotting on command window
BETAS=[0, 0.2, 0.4, 0.6, 0.8];
for i=1:numel(BETAS)
beta=BETAS(i);
[t,y] = ode23s(@(t,y) SIR_Model(t,y,beta), [0 180], [1 1.13*10^(-5) 0]);
plot(t,y(:,2)); hold on
end; hold off
legend("Is:"+(1:numel(BETAS)) + "(" + BETAS + ")" )
title('Number of infections')
xlabel('t')
ylabel('I')
function dy=SIR_Model(t,y,beta)
%Create an empty vector
dy = zeros(3,1);
%Parameters
%beta = 1/3; %transmission coefficient
gamma = 1/4; %recovery rate
%ODES
dy(1) = -beta*y(1)*y(2);
dy(2) = beta*y(1)*y(2) - gamma*y(2);
dy(3) = gamma*y(2);
end
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by