Trying to plot R_K 4th order, but keep getting straight line?

1 view (last 30 days)
Wasim Raza
Wasim Raza on 15 Apr 2022
Commented: Wasim Raza on 15 Apr 2022
The coding is below that i have used
clc; % Clears the screen
clear all;
h=0.1; % step size
x = 0:h:100; % Calculates upto y(1)
s = zeros(1,length(x));
s(1) = 1; % initial condition
i(1) = 0;
r(1) = 0; % initial condition
a = 0.180;
b = 0.037;
F_sir = @(s,i,r) -a*s*i; % change the function as you desire
G_sir = @(s,i,r) a*s*i- b*i;
H_sir = @(s,i,r) b*i;
for k=1:(length(x)-1) % calculation loop
K_1 = F_sir(s(k),i(k),r(k));
L_1 = G_sir(s(k),i(k),r(k));
M_1 = H_sir(s(k),i(k),r(k));
K_2 = F_sir(s(k)+0.5*h,i(k)+0.5*K_1,r(k)+0.5*h*L_1);
L_2 = G_sir(s(k)+0.5*h,i(k)+0.5*K_1,r(k)+0.5*h*L_1);
M_2 = H_sir(s(k)+0.5*h,i(k)+0.5*K_1,r(k)+0.5*h*L_1);
K_3 = F_sir((s(k)+0.5*h),(i(k)+0.5*K_2),(r(k)+0.5*h*L_2));
L_3 = G_sir((s(k)+0.5*h),(i(k)+0.5*K_2),(r(k)+0.5*h*L_2));
M_3 = H_sir(s(k)+0.5*h,i(k)+0.5*K_2,r(k)+0.5*h*L_2);
K_4 = F_sir((s(k)+h),(i(k)+K_3),(r(k)+L_3)); % Corrected
L_4 = G_sir((s(k)+h),(i(k)+K_3),(r(k)+L_3));
M_4 = H_sir((s(k)+h), (i(k)+K_3), (r(k)+L_3));
s(k+1) = s(k)+ (1/6)*(K_1+2*K_2+2*K_3+K_4)*h; % main equation
i(k+1) = (1/6)*(L_1+2*L_2+2*L_3+L_4)*h;
r(k+1) = (1/6)*(M_1+2*M_2+2*M_3+M_4)*h; % main equation
end
display(s(k+1));

Accepted Answer

Alan Stevens
Alan Stevens on 15 Apr 2022
Check these equations
F_sir = @(s,i,r) -a*s*i; % change the function as you desire
G_sir = @(s,i,r) a*s*i- b*i;
H_sir = @(s,i,r) b*i;
If you start with i = 0 these will all stay at zero.
Also, although you have r as an argument, it doesn't appear anywhere in the definition of these functions.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!