필터 지우기
필터 지우기

Plotting solution curves separately

조회 수: 2 (최근 30일)
Temesgen
Temesgen 2023년 5월 4일
편집: Dyuman Joshi 2023년 5월 4일
I am tring to plot the solutions of the SIR model using Runge-kutta fourth order. The graphs are ok but I want to sketch them separatly. I am asking your help how to sketch separatly!! The code is attached
Thank you

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 5월 4일
편집: Dyuman Joshi 2023년 5월 4일
% Parameters
c = 0.0020;
a = 0.02;
% SIR model (Ordinary Differential Equations)
odefcn = @(t,x) [- c*x(1)*x(2);
c*x(1)*x(2) - a*x(2);
a*x(2)];
%%The t in the defintion of odefcn feels redundant
% Simulation time span
t0 = 0;
h = 0.1; % step size
tf = 100;
t = t0:h:tf;
% Initial values
y0 = [100 1 1];
% Calling RK4 Solver is similar to ode45 solver
yy = rk4(odefcn, t, y0);
%Define array for legend
str = {'S(t)','I(t)','R(t)'};
%loop to plot in separate figures
for k=1:size(yy,1)
figure(k)
% Plotting the solutions
plot(t, yy(k,:), 'linewidth', 1.5)
%Change other properties accordingly
grid on
xlabel('Time in Days')
ylabel('Population')
title('Time responses of SIR model')
legend(str{k}, 'location', 'best')
end
% Code for Runge-Kutta 4th-order Solver
function y = rk4(f, x, y0)
y(:, 1) = y0; % initial condition
h = x(2) - x(1); % step size
n = length(x); % number of steps
for j = 1 : n-1
k1 = f(x(j), y(:, j)) ;
k2 = f(x(j) + h/2, y(:, j) + h/2*k1) ;
k3 = f(x(j) + h/2, y(:, j) + h/2*k2) ;
k4 = f(x(j) + h, y(:, j) + h*k3) ;
y(:, j+1) = y(:, j) + h/6*(k1 + 2*k2 + 2*k3 + k4) ;
end
end

카테고리

Help CenterFile Exchange에서 Visual Exploration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by