How do i vary parameter between 0 to 1 and the value of the parameter asteriked on the curved for State variable I only
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
The parameter value of beta is 0.25, but i need to vary beta between 0 - 1, with 0.25 inclusive and asteriked on the curves for state variable I alone.
The code is attached. Thanks.
0 Commenti
Risposte (1)
Arjun
il 26 Feb 2025
I see that you want to observe plots of state variable "I" against different values of "beta" ranging between 0 to 1 with 0.25 being included and for value of "beta=0.25", the plot must be asteriked.
In order to vary value of "beta" in a range you can use a "for" loop in MATLAB with incremental steps of 0.05 or any other step value that you want and wrap your code inside this for loop construct. Kindly refer to the modified version of your code below to achieve this:
h = 0.1;
tmax = 20;
S = zeros(1, tmax/h + 1);
I = zeros(1, tmax/h + 1);
R = zeros(1, tmax/h + 1);
D = zeros(1, tmax/h + 1);
S(1) = 80;
I(1) = 60;
R(1) = 50;
D(1) = 45;
gamma = 0.0015;
mu = 0.2;
alpha = 0.5;
t = 0:h:tmax;
% hold is used so that all the plots can be plotted in one figure so that
% you can track changes in curves of I with variation in beta values
figure;
hold on;
% for loop to vary beta between 0 to 1 with steps of 0.05
for b = 0:0.05:1;
beta = b;
S(:) = 0; I(:) = 0; R(:) = 0; D(:) = 0;
S(1) = 80;
I(1) = 60;
R(1) = 50;
D(1) = 45;
for n = 1:(tmax/h)
S(n+1) = S(n) + h*(-gamma*S(n)*I(n) + mu*R(n));
I(n+1) = I(n) + h*(gamma*S(n)*I(n) - beta*I(n) - alpha*I(n));
R(n+1) = R(n) + h*(beta*I(n) - mu*R(n));
D(n+1) = D(n) + h*(alpha*I(n));
end
% Plot the curve for I
if beta == 0.25
plot(t, I, 'LineWidth', 4.0, 'DisplayName', ['\beta = ', num2str(beta)], 'LineStyle', '-');
% After drawing line mark it with *
plot(t, I, '*', 'LineWidth', 2.0, 'DisplayName', ['\beta = ', num2str(beta), ' (Asterisk)']);
else
plot(t, I, 'LineWidth', 2.0, 'DisplayName', ['\beta = ', num2str(beta)]);
end
end
xlabel('Time');
ylabel('Number of Individuals');
title('Variation of I with Different \beta Values');
legend show;
hold off;
Kindly refer to the documentation of "for" loop to understand better: https://www.mathworks.com/help//releases/R2021a/matlab/ref/for.html
I hope this will help!
0 Commenti
Vedere anche
Categorie
Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!