Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Matt J
Matt J el 27 de Mzo. de 2023
Editada: Matt J el 27 de Mzo. de 2023
%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 comentarios
Matt J
Matt J el 27 de Mzo. de 2023
See what I've done above. I'm not sure exactly how it's supposed to look.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by