According to the documentation for the SciPy function solve_ivp
, RK23
is based on the Bogacki-Shampine method, which is implemented in the MATLAB function ode23.
Below are numerical results obtained from applying ode23
to a long-time integration of two Hamiltonian systems: a simple double-well example and the OP's earth orbit example.
Double-Well Example
Here I run ode23
on a double-well Hamiltonian system with Hamiltonian function $H(q,p) = (1/2) p^2 + (1/4) (q^2 - 1)^2$ using the following MATLAB code
ff=@(t,y) [y(2); y(1)-y(1).^3];
opts=odeset('RelTol',tol);
[t,y]=ode23(ff,[0 5000],[0; 2],opts);
for the tol values indicated in the figure titles below starting with the default relative tolerance of $10^{-3}$. Basically this code numerically integrates $$
\dot{q} = p \;, \quad \dot{p} = q - q^3 \;, \quad (q(0),p(0)) = (0,2) \;,
$$ for a (long) time span of $5000$. ode23
outputs a vector of times $t$ and a matrix $y$ whose rows are the corresponding numerical approximation.
Below are the outputted discrete trajectories in phase space. The dots are made a bit lighter with time. The solid red curve is the level set of the Hamiltonian corresponding to the initial point. The actual solutions lie on this red curve for all time since they preserve $H$. In contrast, the outputted dots seem to converge to the right well, and the ones with smaller tol seem to take longer to converge.
OP's Earth Orbit Example
Following the OP's description, one can similarly simulate the earth revolving around the sun using
m1=1.989e30;
m2=5.972e24;
G=6.674e-11;
ff=@(t,y) [y(3); y(4); ...
-G*m1*y(1)/(y(1)^2+y(2)^2)^(3/2); ...
-G*m1*y(2)/(y(1)^2+y(2)^2)^(3/2)];
T=100*31556925.9747; % time-span is 100 years!
opts=odeset('RelTol',tol);
[t,y]=ode23(ff,[0 T],[147*1e9; 0; 0; -30300],opts);
The following figure shows the relative energy error after 100 years for two different tol
values.
Discussion
Since we see a systematic energy drift in both of these relatively simple Hamiltonian test problems, these numerical counterexamples illustrate that ode23
is probably not a geometric integrator. For example, a geometric integrator that preserves the symplectic form of the Hamiltonian system (called symplectic integrators), typically do not preserve energy, but they do have bounded energy errors over long-time simulations. One can construct adaptive geometric integrators, but this is a bit tricky. See, e.g.,
Calvo, M. P.; López-Marcos, M. A.; Sanz-Serna, J. M., Variable step implementation of geometric integrators, Appl. Numer. Math. 28, No. 1, 1-16 (1998). ZBL0930.65136..
from x import *
. $\endgroup$