The algorithm isn't given any output, the graph is unidentifiable. The systems of Differential Equations considered:
$$ x'[t]=-x[t]+y[t]\\ y'[t]=-x[t]-y[t]\\ x[0]=0, y[0]=4, h=0.1$$
Please how do I represent k1, l1, k2, l2, k3, l3, k4, l4, y(n+1), and x(n+1) for the simulation to run?
Here is the code:
DSolve[{x'[t] == -x[t] + y[t], y'[t] == -x[t] - y[t],
x[0] == 0, y[0] == 4}, {x[t], y[t]}, t]
{{x[t] -> 4 E^-t Sin[t], y[t] -> 4 E^-t Cos[t]}}
In[2]:= ti = 0;
tf = 10 ;
m = 100;
Δt = (tf - ti)/m;
T = Range[ti, tf, Δt] // N;
X = ConstantArray[0., m + 1];
Y = ConstantArray[0., m + 1];
In[1]:= X[[1]] = 0.;
Y[[1]] = 4.;
Do[
k1 = Δt ( Y[[n]] - X[[n]]);
l1 = -Δt (X[[n]] + Y[[n]]);
k2 = Δt ( (Y[[n]] + l1/2) - (X[[n]] + k1/2));
l2 = -Δt ( (X[[n]] + k1/2) + (Y[[n]] + l1/2));
k3 = Δt ( (Y[[n]] + k2/2) - (X[[n]] + l2/2));
l3 = -Δt ( (X[[n]] + k2/2) + (Y[[n]] + l2/2));
k4 = Δt ( (Y[[n]] + k3) - (X[[n]] + l3));
l4 = -Δt ( (X[[n]] + k3) + (Y[[n]] + l3));
X[[n + 1]] = Y[[n]] - X[[n]] + 1/6 (k1 + 2 k2 + 2 k3 + k4);
Y[[n + 1]] = -X[[n]] - Y[[n]] + 1/6 (l1 + 2 l2 + 2 l3 + l4),{n, 1, m}]
I am having a hard time using this algorithm to compute for the systems of ODE. The first algorithm seems to be much. I would be glad if anyone can help out
rk4Step[{f_, t_, y_, stepSize_}] :=
Module[{k1, k2, k3, k4, h = N[stepSize]},
k1 = h f[N[t], y];
k2 = h f[t + h/2, y + 1/2 k1];
k3 = h f[t + h/2, y + 1/2 k2];
k4 = h f[t + h, y + k3];
{f, t + h, y + 1/6 (k1 + 2 k2 + 2 k3 + k4), h}]
RK4[f_, t0_, y0_, tEnd_, n_] :=
Module[{h = N[(tEnd - t0)/n]},
Most /@ Rest /@ NestList[rk4Step, {f, t0, y0, h}, n]]

Do[]has no output. What happens if you executeXorY? – Michael E2 Feb 04 '21 at 01:36k2. The expression refers to a variableK1. Maybe that should be lowercasek1. – LouisB Feb 04 '21 at 01:44Do[]command) shouldn't. Why do you think it should? That you got output with a different code (Euler's method) does not mean this code would produce output; it just means you used a command that produced output. In changing the code to RK4, you (inadvertently) got rid of the output command. From your code above, I do get output fromListLinePlot[Transpose@{X, Y}]. – Michael E2 Feb 04 '21 at 02:44