dS := -beta*S*Q;
dQ := Q*S*beta - Q*alpha;
dR := alpha*Q;
beta := 0.2;
alpha := 0.1;
S0 := 0.8;
Q0 := 0.2;
R0 := 0;
RungeKutta := proc(f::list, y0::list, t0::float, tf::float, h::float) local n, t, y, k1, k2, k3, k4, i; n := 1 + floor((tf - t0)/h); t := Vector(n, fill = 0); y := Matrix(n, length(y0), fill = 0); t[1] := t0; y[1] := Vector(y0); for i to n - 1 do k1 := Vector(map(f, t[i], y[i])); k2 := Vector(map(f, t[i] + 1/2*h, y[i] + 1/2*h*k1)); k3 := Vector(map(f, t[i] + 1/2*h, y[i] + 1/2*h*k2)); k4 := Vector(map(f, t[i] + h, y[i] + h*k3)); y[i + 1] := y[i] + 1/6*h*(k1 + 2*k2 + 2*k3 + k4); t[i + 1] := t[i] + h; end do; [t, y]; end proc;
f = [dS, dQ, dR];
t0 := 0;
tf := 50;
h := 0.1;
result := RungeKutta(f, [S0, Q0, R0], t0, tf, h);
t_values := result[1];
S_values := result[2][() .. (), 1];
Q_values := result[2][() .. (), 2];
R_values := result[2][() .. (), 3];
plots:-display(plot(t_values, S_values, color = "blue", legend = "Susceptible"), plot(t_values, Q_values, color = "red", legend = "Infected"), plot(t_values, R_values, color = "green", legend = "Recovered"), legend = ["Susceptible", "Infected", "Recovered"], title = "Simulation of Infectious Disease Model", xlabel = "Time", ylabel = "Population", view = [0 .. tf, 0 .. 1]);
Warning, (in RungeKutta) `i` is implicitly declared local
dS := -0.2 S Q
dQ := 0.2 S Q - 0.1 Q
dR := 0.1 Q
beta := 0.2
alpha := 0.1
S0 := 0.8
Q0 := 0.2
R0 := 0
RungeKutta := proc (f::list, y0::list, t0::float, tf::float,
h::float) local n, t, y, k1, k2, k3, k4, i; n := 1+floor((tf-\
t0)/h); t := Vector(n, fill = 0); y := Matrix(n, length(y0),
fill = 0); t[1] := t0; y[1] := Vector(y0); for i to n-1 do
k1 := Vector(map(f, t[i], y[i])); k2 := Vector(map(f,
t[i]+(1/2)*h, y[i]+(1/2)*h*k1)); k3 := Vector(map(f,
t[i]+(1/2)*h, y[i]+(1/2)*h*k2)); k4 := Vector(map(f, t[i]+h,
y[i]+h*k3)); y[i+1] := y[i]+(1/6)*h*(k1+2*k2+2*k3+k4);
t[i+1] := t[i]+h end do; [t, y] end proc
[-0.2 S Q, 0.2 S Q - 0.1 Q, 0.1 Q] = [-0.2 S Q, 0.2 S Q - 0.1 Q,
0.1 Q]
t0 := 0
tf := 50
h := 0.1
Error, invalid input: RungeKutta expects its 3rd argument, t0, to be of type float, but received 0
t_values := result[1]
S_values := result[2][() .. (), 1]
Q_values := result[2][() .. (), 2]
R_values := result[2][() .. (), 3]
Warning, expecting only range variable result[2][(NULL) .. (NULL),1] in expression result[1] to be plotted but found name result[1]
Warning, expecting only range variable result[2][(NULL) .. (NULL),2] in expression result[1] to be plotted but found name result[1]
Warning, expecting only range variable result[2][(NULL) .. (NULL),3] in expression result[1] to be plotted but found name result[1]
Error, (in plots:-display) unexpected options: [xlabel = "Time", ylabel = "Population"]