Graphing several curves of a variable for various values of a parameter
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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
0 commentaires
Réponse acceptée
Matt J
le 27 Mar 2023
Modifié(e) : Matt J
le 27 Mar 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 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Gamma Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!