I have the following RK4 solver which splits the two 2nd order ODEs, used to calculate x and y positions under the influence of a gravitating body where $$x''(t)=\frac{G m x(t)}{(x(t)^2+y(t)^2)^{3/2}}$$ and $$y''(t)=\frac{G m y(t)}{(x(t)^2+y(t)^2)^{3/2}}$$into four 1st order ODEs using the following code, and have attempted to plot the results which I had hoped would be a "spacecraft" in a 300km circular orbit about Earth:
Remove["Global`*"]
(*dx/dt=*)f[t_, x_, y_, dx_, dy_] := -((G m x)/(x^2 + y^2)^(3/2));
(*dy/dt=*)g[t_, x_, y_, dx_, dy_] := -((G m y)/(x^2 + y^2)^(3/2));
(*d^2x/dt^2=*)u[t_, x_, y_, dx_, dy_] := dx;
(*d^2y/dt^2=*)v[t_, x_, y_, dx_, dy_] := dy;
G = 6.672*10^-11; (*Gravitational constant*)
m = 5.97219*10^24; (*Mass of Earth*)
r = 6.37101 *10^6; (*Earth mean equatorial radius*)
t[0] = 0; (*Initial time*)
x[0] = 0; (*Initial x-position*)
y[0] = r + 300000; (*Initial y-position*)
dx[0] = Sqrt[(G m)/(r + 300000)]; (*Initial x-velocity*)
dy[0] = 0; (*Initial y-velocity)
h = 0.005; (*Step size*)
tmax = 10000; (*Maximum running time*)
(*RK4 solver*)
Do[
{t[n] = t[0] + h n,
a1 = h f[t[n], x[n], y[n], dx[n], dy[n]];
b1 = h g[t[n], x[n], y[n], dx[n], dy[n]];
c1 = h u[t[n], x[n], y[n], dx[n], dy[n]];
d1 = h v[t[n], x[n], y[n], dx[n], dy[n]];
a2 = h f[t[n] + h/2, x[n] + a1/2, y[n] + b1/2, dx[n] + c1/2,
dy[n] + d1/2];
b2 = h g[t[n] + h/2, x[n] + a1/2, y[n] + b1/2, dx[n] + c1/2,
dy[n] + d1/2];
c2 = h u[t[n] + h/2, x[n] + a1/2, y[n] + b1/2, dx[n] + c1/2,
dy[n] + d1/2];
d2 = h v[t[n] + h/2, x[n] + a1/2, y[n] + b1/2, dx[n] + c1/2,
dy[n] + d1/2];
a3 = h f[t[n] + h/2, x[n] + a2/2, y[n] + b2/2, dx[n] + c2/2,
dy[n] + d2/2];
b3 = h g[t[n] + h/2, x[n] + a2/2, y[n] + b2/2, dx[n] + c2/2,
dy[n] + d2/2];
c3 = h u[t[n] + h/2, x[n] + a2/2, y[n] + b2/2, dx[n] + c2/2,
dy[n] + d2/2];
d3 = h v[t[n] + h/2, x[n] + a2/2, y[n] + b2/2, dx[n] + c2/2,
dy[n] + d2/2];
a4 = h f[t[n] + h, x[n] + a3, y[n] + b3, dx[n] + c3, dy[n] + d3];
b4 = h g[t[n] + h, x[n] + a3, y[n] + b3, dx[n] + c3, dy[n] + d3];
c4 = h u[t[n] + h, x[n] + a3, y[n] + b3, dx[n] + c3, dy[n] + d3];
d4 = h v[t[n] + h, x[n] + a3, y[n] + b3, dx[n] + c3, dy[n] + d3];
x[n + 1] = x[n] + 1/6 (a1 + 2 a2 + 2 a3 + a4);
y[n + 1] = y[n] + 1/6 (b1 + 2 b2 + 2 b3 + b4);
dx[n + 1] = dx[n] + 1/6 (c1 + 2 c2 + 2 c3 + c4);
dy[n + 1] = dy[n] + 1/6 (d1 + 2 d2 + 2 d3 + d4);
}, {n, 0, tmax}]
T1 = Table[{t[i], x[i]}, {i, 0, tmax}];
T2 = Table[{t[i], y[i]}, {i, 0, tmax}];
T3 = Table[{t[i], dx[i]}, {i, 0, tmax}];
T4 = Table[{t[i], dy[i]}, {i, 0, tmax}];
(*Graphical output*)
ListLinePlot[T1]
ListLinePlot[T2]
ListLinePlot[T3]
ListLinePlot[T4]
ListLinePlot[Table[{x[i], y[i]}, {i, 0, tmax}]]
When I compare the above output results to the following NDSolve code (where here $e=x$ and $p=y$), I see that my above code is incorrect and I do not get a circular orbit as is given below. Does anyone know where I might have gone wrong?
soln = NDSolve[{
e''[t] == -((G m e[t])/(e[t]^2 + p[t]^2)^(3/2)),
p''[t] == -((G m p[t])/(e[t]^2 + p[t]^2)^(3/2)),
e[0] == 0, p[0] == r + 300000, e'[0] == 7700, p'[0] == 0}, {e[t],
p[t]}, {t, 0, tmax}, Method -> "StiffnessSwitching",
MaxSteps -> 10000000]
Show[ParametricPlot[Evaluate[{{e[t], p[t]}} /. soln], {t, 0, tmax},
AxesLabel -> {e, p}, PlotStyle -> Automatic, PlotRange -> Full,
ImageSize -> Large]]
Any help would be appreciated, thanks very much.