Astronomy and Space Exploration ES

Higher Education / Numerical methods in astrophysics

Symplectic integrators and chaos in the N-body problem

Why leapfrog conserves energy over long times and RK4 does not, and how to measure sensitivity to initial conditions with the Lyapunov exponent, in the figure-eight and in the Pythagorean problem.

⏱ 150 min ODEsHamiltonian mechanicsPoisson bracketssymplectic mapsBaker–Campbell–Hausdorff formulanumerical error analysisLyapunov exponents revision

By the end you will be able to:
  • Formulate the gravitational N-body problem as a separable Hamiltonian system and identify its first integrals.
  • Derive the kick-drift-kick leapfrog as a composition of exact flows and prove that it is symplectic, symmetric and of order 2.
  • Explain, through the modified Hamiltonian, why the energy error of a symplectic method of order p stays bounded in O(hᵖ) over exponentially long times whereas that of RK4 drifts secularly.
  • Construct the order-4 Yoshida integrator by composing three leapfrogs and verify its order numerically.
  • Define and numerically estimate the maximal Lyapunov exponent and the predictability horizon.
  • Analyze two paradigmatic solutions of the three-body problem: the Chenciner–Montgomery figure-eight and Burrau's Pythagorean problem.

1. The N-body problem

Illustration: The N-body problem

Let NN point masses mim_i have positions ri∈R3\mathbf r_i\in\mathbb R^3 and velocities vi=r˙i\mathbf v_i = \dot{\mathbf r}_i, i=1,…,Ni = 1,\dots,N. Newton’s law of gravitation gives

r¨i=∑j≠iG mj rj−ri∥rj−ri∥3,\ddot{\mathbf r}_i = \sum_{j\ne i} G\,m_j\,\frac{\mathbf r_j - \mathbf r_i}{\lVert\mathbf r_j - \mathbf r_i\rVert^3},

where GG is the gravitational constant. Throughout the lesson (and in src/lib/math/nbody.ts) we use dimensionless units with G=1G = 1: once a reference mass MM and a length RR are fixed, the unit of time is R3/(GM)\sqrt{R^3/(GM)}.

With momenta pi=mivi\mathbf p_i = m_i\mathbf v_i, the system is Hamiltonian with

H(q,p)=T(p)+V(q),T=∑i∥pi∥22mi,V=−∑i<jG mimj∥qi−qj∥,H(\mathbf q,\mathbf p) = T(\mathbf p) + V(\mathbf q),\qquad T = \sum_i \frac{\lVert\mathbf p_i\rVert^2}{2m_i},\qquad V = -\sum_{i<j}\frac{G\,m_i m_j}{\lVert\mathbf q_i-\mathbf q_j\rVert},

and q˙=∂H/∂p\dot{\mathbf q} = \partial H/\partial\mathbf p, p˙=−∂H/∂q\dot{\mathbf p} = -\partial H/\partial\mathbf q. It is separable: TT depends only on p\mathbf p and VV only on q\mathbf q. The ten classical integrals are the energy E=HE = H, the linear momentum P=∑ipi\mathbf P = \sum_i\mathbf p_i, the uniform motion of the center of mass and the angular momentum

L=∑iri×pi.\mathbf L = \sum_i \mathbf r_i\times\mathbf p_i .

For N≥3N\ge 3 there are not enough additional integrals to solve the problem by quadratures (results of Bruns and Poincaré), so numerical integration is unavoidable. The question is: what structure of the exact flow should a good integrator respect?

2. Two integrators

Illustration: Two integrators

We write y=(q,p)y = (\mathbf q,\mathbf p), y˙=f(y)\dot y = f(y), and a step of size hh from yny_n to yn+1≈y(tn+h)y_{n+1}\approx y(t_n+h).

2.1 Classical Runge–Kutta (RK4)

k1=f(yn),k2=f ⁣(yn+h2k1),k3=f ⁣(yn+h2k2),k4=f(yn+hk3),yn+1=yn+h6(k1+2k2+2k3+k4).\begin{aligned} k_1 &= f(y_n), & k_2 &= f\!\left(y_n + \tfrac h2 k_1\right),\\ k_3 &= f\!\left(y_n + \tfrac h2 k_2\right), & k_4 &= f(y_n + h k_3), \end{aligned} \qquad y_{n+1} = y_n + \frac h6\left(k_1 + 2k_2 + 2k_3 + k_4\right).

Local error O(h5)O(h^5) and global error O(h4)O(h^4) on a fixed interval. But RK4 is not symplectic: nothing prevents the energy errors of each step from accumulating with the same sign. The typical outcome in bounded orbits is a secular drift, ∣ΔE∣∼C t hp\lvert\Delta E\rvert \sim C\,t\,h^{p} (with p≤4p\le 4 depending on the problem; see exercise 3, where for the harmonic oscillator one gets t h5t\,h^5).

2.2 Leapfrog (Störmer–Verlet, kick-drift-kick)

Since H=T+VH = T + V, the exact flows of TT and of VV separately are trivial:

  • flow of VV (kick): φτV(q,p)=(q, p−τ ∇V(q))\varphi^V_\tau(\mathbf q,\mathbf p) = (\mathbf q,\ \mathbf p - \tau\,\nabla V(\mathbf q));
  • flow of TT (drift): φτT(q,p)=(q+τM−1p, p)\varphi^T_\tau(\mathbf q,\mathbf p) = (\mathbf q + \tau M^{-1}\mathbf p,\ \mathbf p),

with M=diag⁡(mi)M = \operatorname{diag}(m_i). The leapfrog is the symmetric composition (Strang splitting)

Φh=φh/2V∘φhT∘φh/2V,\Phi_h = \varphi^V_{h/2}\circ\varphi^T_{h}\circ\varphi^V_{h/2},

that is, in terms of accelerations a(r)\mathbf a(\mathbf r):

vn+1/2=vn+h2 a(rn),rn+1=rn+h vn+1/2,vn+1=vn+1/2+h2 a(rn+1).\mathbf v_{n+1/2} = \mathbf v_n + \tfrac h2\,\mathbf a(\mathbf r_n),\qquad \mathbf r_{n+1} = \mathbf r_n + h\,\mathbf v_{n+1/2},\qquad \mathbf v_{n+1} = \mathbf v_{n+1/2} + \tfrac h2\,\mathbf a(\mathbf r_{n+1}).

It costs one force evaluation per step (the last one is reused in the next), versus four for RK4. It has three structural properties:

  1. Symplectic. Each factor is the exact flow of a Hamiltonian, hence symplectic, and the composition of symplectic maps is symplectic (exercise 1). Recall that Φ\Phi is symplectic if its Jacobian satisfies Φ′(y)TJ Φ′(y)=J\Phi'(y)^{\mathsf T} J\,\Phi'(y) = J with J=(0I−I0)J = \begin{pmatrix}0 & I\\ -I & 0\end{pmatrix}.
  2. Symmetric (reversible): Φ−h=Φh−1\Phi_{-h} = \Phi_h^{-1}; this is why its order is even, here 22.
  3. Conserves L\mathbf L exactly (up to round-off), because each kick applies pairwise central forces and each drift moves ri\mathbf r_i parallel to vi\mathbf v_i (exercise 2).

3. Why does the energy not drift? The modified Hamiltonian

Illustration: Why does the energy not drift? The modified Hamiltonian

Backward error analysis looks for a Hamiltonian H~\tilde H whose exact flow coincides with the numerical method. If XFX_F denotes the Hamiltonian vector field of FF, the flow of VV over τ\tau is exp⁡(τXV)\exp(\tau X_V) and the symmetric Baker–Campbell–Hausdorff formula gives

log⁡ ⁣(eh2XV ehXT eh2XV)=h (XV+XT)+O(h3),\log\!\left(e^{\frac h2 X_V}\,e^{h X_T}\,e^{\frac h2 X_V}\right) = h\,(X_V + X_T) + O(h^3),

where the terms of order h3h^3 are double commutators of XVX_V and XTX_T. Since the commutator of Hamiltonian vector fields is (up to sign) the field of the Poisson bracket {F,G}=∑i(∂qiF⋅∂piG−∂piF⋅∂qiG)\{F,G\} = \sum_i\left(\partial_{\mathbf q_i}F\cdot\partial_{\mathbf p_i}G - \partial_{\mathbf p_i}F\cdot\partial_{\mathbf q_i}G\right), the method is (formally) the exact flow of

H~=H+h2(112{T,{T,V}}−124{V,{V,T}})+O(h4).\tilde H = H + h^2\left(\frac1{12}\{T,\{T,V\}\} - \frac1{24}\{V,\{V,T\}\}\right) + O(h^4).

The coefficients are checked on the harmonic oscillator, where everything is explicit (exercise 4). Since Φh\Phi_h is symplectic, the series H~\tilde H is Hamiltonian (this is what fails for RK4) and the numerical solution conserves H~\tilde H up to exponentially small terms. The precise result (Hairer, Lubich & Wanner, ch. IX, Theorem 8.1, for analytic HH) is

∣H(yn)−H(y0)∣≤C h2fornh≤e h0/(2h),\lvert H(y_n) - H(y_0)\rvert \le C\,h^2 \qquad \text{for}\qquad n h \le e^{\,h_0/(2h)},

with CC, h0h_0 independent of nn and hh. That is: energy error bounded in O(h2)O(h^2) over exponentially long times, with no drift. The error oscillates (it grows at pericenters and comes back down), but it does not accumulate.

Two honest caveats:

  • The constant CC and the scale h0h_0 depend on how analytic HH is along the orbit. In a close encounter (rij→0r_{ij}\to 0) the local time scale ∼rij3/(Gm)\sim\sqrt{r_{ij}^3/(G m)} becomes tiny and a fixed step no longer resolves it: the fixed-step leapfrog loses its guarantees. In practice one uses regularizations (Kustaanheimo–Stiefel), symmetric adaptive steps or specialized integrators.
  • A naive adaptive step destroys symplecticity: the map changes from step to step with an h(y)h(y).

3.1 Higher order without losing the structure: the Yoshida integrator

The leapfrog is of order 2. Yoshida (1990) observed that, being symmetric (Φ−h=Φh−1\Phi_{-h} = \Phi_h^{-1}), its expansion in hh contains only odd powers, Φh=exp⁡ ⁣(hXH+h3X3+h5X5+… )\Phi_h = \exp\!\left(h X_H + h^3 X_3 + h^5 X_5 + \dots\right), and that a symmetric composition of three steps of sizes w1hw_1 h, w0hw_0 h, w1hw_1 h,

Φh(4)=Φw1h∘Φw0h∘Φw1h,2w1+w0=1,2w13+w03=0,\Phi^{(4)}_h = \Phi_{w_1 h}\circ\Phi_{w_0 h}\circ\Phi_{w_1 h},\qquad 2w_1 + w_0 = 1,\qquad 2w_1^3 + w_0^3 = 0,

cancels the h3h^3 term: the first condition guarantees consistency (advancing hh in total) and the second cancels X3X_3 (the commutators of order h3h^3 add linearly in the wi3w_i^3 to first order). The solution is

w1=12−21/3≈1.3512,w0=−21/32−21/3≈−1.7024.w_1 = \frac{1}{2 - 2^{1/3}} \approx 1.3512,\qquad w_0 = -\frac{2^{1/3}}{2 - 2^{1/3}} \approx -1.7024 .

The central step is negative and larger than hh in absolute value: the method goes forward, backward and forward again. It is unavoidable (Suzuki 1991): no composition of positive leapfrog steps exceeds order 2. The result is symplectic and symmetric (composition of symplectic and symmetric maps), of order 4, with 3 force evaluations per step. The same trick iterated gives orders 6 and 8 (Yoshida gives the coefficients), with 7 and 15 leapfrogs per step.

It is in src/lib/math/yoshida.ts. Measurements with our code (test yoshida.test.ts):

Problemhhmax⁡∣ΔE/E0∣\max\lvert\Delta E/E_0\rvert leapfrogmax⁡∣ΔE/E0∣\max\lvert\Delta E/E_0\rvert Yoshida 4
Figure-eight, 1 period0.022.4×10−42.4\times10^{-4}1.1×10−71.1\times10^{-7}
Figure-eight, 1 period0.015.9×10−55.9\times10^{-5}7.0×10−97.0\times10^{-9}
Figure-eight, 1 period0.0051.5×10−51.5\times10^{-5}4.4×10−104.4\times10^{-10}
Two bodies e=0.7e = 0.7, 100 periods0.032.2×10−22.2\times10^{-2}6.2×10−46.2\times10^{-4}

Dividing hh by 2 divides the leapfrog error by 4 and the Yoshida error by 16: orders 2 and 4. And in the long eccentric orbit both remain bounded (the maximum of the second half does not exceed that of the first), whereas RK4 with the same step ends at 6.7×10−36.7\times10^{-3} and keeps growing. At equal cost (3 evaluations of Yoshida versus 3 leapfrog steps of size h/3h/3, whose error would be 2.2×10−2/9=2.4×10−32.2\times10^{-2}/9 = 2.4\times10^{-3}), Yoshida wins by a factor of 4 in this problem, and the advantage grows as hh is reduced.

3.2 The energy conservation theorem in general

The result of the previous section is not exclusive to order 2. For any symplectic integrator of order pp applied to an analytic Hamiltonian, with a sufficiently small step (Hairer, Lubich & Wanner, ch. IX, Theorem 8.1),

∣H(yn)−H(y0)∣≤C hpfornh≤e h0/(2h).\lvert H(y_n) - H(y_0)\rvert \le C\,h^{p}\qquad\text{for}\qquad n h \le e^{\,h_0/(2h)} .

The proof follows the same path: the method is the exact flow, up to exponentially small terms, of a modified Hamiltonian H~=H+hpHp+1+hp+1Hp+2+…\tilde H = H + h^p H_{p+1} + h^{p+1}H_{p+2} + \dots (optimally truncated), and H~\tilde H is conserved exactly by that flow; the difference H~−H=O(hp)\tilde H - H = O(h^p) is bounded on a bounded region of phase space. For Yoshida, p=4p = 4: that is what the table shows. The condition “sufficiently small step” is serious: Yoshida has internal steps of size 1.7h1.7h, so its stability limit on the oscillator is hω<2/1.7024=1.17h\omega \lt 2/1.7024 = 1.17 instead of 22 (exercise 7).

three equal masses, T ≈ 6.3259 · t = 0.00

What to observe. With the scenario “2 bodies, e = 0.8” and “Both overlaid”, the leapfrog error (solid line) oscillates with peaks at each pericenter but its envelope does not grow; the RK4 error (dashed) starts smaller over many stretches and grows monotonically until it exceeds it. Raise the speed to see tens of periods. In the tests of src/lib/math/__tests__/nbody.test.ts (e = 0.7, h=0.03h = 0.03, 100 periods) the RK4 error grows linearly with tt, whereas the leapfrog maximum in the second half does not exceed that of the first. In our runs the RK4 drift scales approximately as h5h^5 (step 0.02→0.030.02\to0.03 multiplies the error by ≈7.5≈1.55\approx 7.5 \approx 1.5^5), in line with exercise 3.

4. Sensitivity to initial conditions

Illustration: Sensitivity to initial conditions

Let y(t)y(t) be a solution and y(t)+δy(t)y(t) + \delta y(t) another that starts at distance ∥δy(0)∥=δ0\lVert\delta y(0)\rVert = \delta_0 in phase space. The maximal Lyapunov exponent is

λ=lim⁡t→∞ lim⁡δ0→0 1t ln⁡∥δy(t)∥δ0.\lambda = \lim_{t\to\infty}\ \lim_{\delta_0\to 0}\ \frac1t\,\ln\frac{\lVert\delta y(t)\rVert}{\delta_0}.

If λ>0\lambda > 0 the separation grows as δ0eλt\delta_0 e^{\lambda t} and the orbit is chaotic; tL=1/λt_L = 1/\lambda is the Lyapunov time. In an integrable system (Kepler) neighboring orbits separate only polynomially (through the difference of periods) and λ=0\lambda = 0.

Numerically the separation saturates when it stops being small, so the method of Benettin et al. (1980) renormalizes: every interval τ\tau it measures δk\delta_k, accumulates ln⁡(δk/δ0)\ln(\delta_k/\delta_0) and rescales the neighboring trajectory to distance δ0\delta_0 along the current direction. After KK intervals,

λ≈1Kτ∑k=1Kln⁡δkδ0.\lambda \approx \frac1{K\tau}\sum_{k=1}^{K}\ln\frac{\delta_k}{\delta_0}.

It is implemented in lyapunovBenettin; the simulator also shows the estimate without renormalization, ln⁡(δ/δ0)/t\ln(\delta/\delta_0)/t (valid while δ≪1\delta \ll 1). The distance used is the Euclidean one in R6N\mathbb R^{6N}, δ2=∑i∥ri−ri′∥2+∥vi−vi′∥2\delta^2 = \sum_i \lVert\mathbf r_i-\mathbf r_i'\rVert^2 + \lVert\mathbf v_i-\mathbf v_i'\rVert^2.

How to measure it in practice. Three decisions affect the number that comes out: (1) the renormalization interval τ\tau must be shorter than the time in which δ\delta leaves the linear regime (δ0eλτ≪1\delta_0 e^{\lambda\tau} \ll 1) but not so short that round-off noise dominates; (2) δ0\delta_0 must comfortably exceed round-off (10−810^{-8} versus 10−1610^{-16}) so that the initial direction of the perturbation is meaningful; (3) λ\lambda is defined as a limit t→∞t \to \infty, but the finite-time estimate converges as ∼1/t\sim 1/t (law of large numbers over the ln⁡(δk/δ0)\ln(\delta_k/\delta_0)) and, in systems that alternate regular stretches and close encounters, it fluctuates strongly from one interval to the next. It must therefore always be given with its integration time and, if possible, averaged over neighboring initial conditions. A useful check: in an integrable system (two bodies) the estimator must decay as ln⁡t/t\ln t/t toward zero; if it does not, numerical error is being mistaken for chaos. And another: λ\lambda does not depend on the integrator if hh is small; if it changes when hh changes (as happens in the Pythagorean problem with h=10−3h = 10^{-3} versus 10−410^{-4}), the close encounters are not resolved.

Practical consequence. If we want the state with tolerance Δ\Delta starting from an uncertainty δ0\delta_0, the prediction horizon is

tpred≈1λ ln⁡Δδ0,t_{\text{pred}} \approx \frac1\lambda\,\ln\frac{\Delta}{\delta_0},

which grows only logarithmically with the initial precision. Not even double-precision round-off (δ0∼10−16\delta_0\sim 10^{-16}) escapes: it is also a perturbation that chaos amplifies. This is why in a chaotic system it makes no sense to ask for the exact trajectory at long times, but rather for statistical or structural properties (energy, final state), and why it matters so much that the integrator respects the conserved quantities.

5. Two paradigmatic solutions

Illustration: Two paradigmatic solutions

5.1 The figure-eight

Moore (1993) numerically found a periodic orbit in which three equal masses chase one another along the same figure-eight curve; Chenciner and Montgomery (2000) proved its existence by minimizing the action ∫0T(T−V) dt\int_0^T (T - V)\,dt over loops with the symmetries of the eight. With mi=1m_i = 1, G=1G = 1 and the conditions of Simó (2002),

x1=−x2=(0.97000436, −0.24308753),x3=0,v3=−2v1=−2v2=(−0.93240737, −0.86473146),\mathbf x_1 = -\mathbf x_2 = (0.97000436,\ -0.24308753),\quad \mathbf x_3 = \mathbf 0,\qquad \mathbf v_3 = -2\mathbf v_1 = -2\mathbf v_2 = (-0.93240737,\ -0.86473146),

the period is T≈6.3259T \approx 6.3259, the angular momentum is zero and the energy is E≈−1.28714E \approx -1.28714 (checked in the tests). Simó showed that it is linearly stable: a perturbation of 10−610^{-6} does not destroy the eight, and in the simulator δ(t)\delta(t) grows slowly, not exponentially. It is a good test of integrators: after one period the state must return to the initial one (in the tests, to within 10−410^{-4} with h=T/6000h = T/6000).

5.2 Burrau’s Pythagorean problem

Burrau (1913) posed three masses 33, 44 and 55 at rest at the vertices of a right triangle with sides 33, 44, 55, each mass opposite the side of equal length. With the coordinates of Szebehely & Peters (1967),

m=3: (1,3),m=4: (−2,−1),m=5: (1,−1),m=3:\ (1,3),\qquad m=4:\ (-2,-1),\qquad m=5:\ (1,-1),

the center of mass is at the origin, L=0\mathbf L = \mathbf 0 and E=V=−(125+154+203)=−76960E = V = -\left(\tfrac{12}{5} + \tfrac{15}{4} + \tfrac{20}{3}\right) = -\tfrac{769}{60}. Burrau could only integrate the beginning by hand; Szebehely and Peters (1967), now with a computer and regularization, showed that after a series of close encounters the system breaks up: around t≈59.4t \approx 59.4 (units G=1G = 1) a last triple encounter occurs and mass 33 is ejected for good, while masses 44 and 55 remain as a binary; Peters extended the integration to t=67t = 67 to confirm the escape (“elliptic-hyperbolic” motion).

Here the two lessons appear together: the close encounters make the fixed-step leapfrog show spikes in ∣ΔE/E0∣\lvert\Delta E/E_0\rvert (try varying hh) and the dynamics is chaotic. With our implementation (lyapunovBenettin, h=10−4h = 10^{-4}, t∈[0,20]t\in[0,20]) the finite-time estimate is λ≈1\lambda \approx 1 in units G=1G = 1, versus ≈0.1\approx 0.1 for the circular two-body orbit over t∈[0,50]t\in[0,50] (in this case the true value is 00 and what is measured is the slow decay ∼ln⁡t/t\sim\ln t/t of the estimate). These are estimates from our code, not reference values.

Problems

Exercise 1 (symplecticity of the leapfrog). Show that φτV\varphi^V_\tau and φτT\varphi^T_\tau are symplectic and conclude that Φh\Phi_h is.

Show solution

Proof. The Jacobian of the kick is A=(I0−τSI)A = \begin{pmatrix} I & 0\\ -\tau S & I\end{pmatrix} with S=∇2VS = \nabla^2 V symmetric. Then

ATJA=(I−τS0I)(0I−I0)(I0−τSI)=(−τS+τSTI−I0)=J,A^{\mathsf T} J A = \begin{pmatrix} I & -\tau S\\ 0 & I\end{pmatrix}\begin{pmatrix}0 & I\\ -I & 0\end{pmatrix}\begin{pmatrix} I & 0\\ -\tau S & I\end{pmatrix} = \begin{pmatrix} -\tau S + \tau S^{\mathsf T} & I\\ -I & 0\end{pmatrix} = J,

using S=STS = S^{\mathsf T}. For the drift, B=(IτM−10I)B = \begin{pmatrix} I & \tau M^{-1}\\ 0 & I\end{pmatrix} and the same computation works because M−1M^{-1} is symmetric. By the chain rule (Φ∘Ψ)′=Φ′(Ψ) Ψ′(\Phi\circ\Psi)' = \Phi'(\Psi)\,\Psi', and if Φ′TJΦ′=J\Phi'^{\mathsf T}J\Phi' = J and Ψ′TJΨ′=J\Psi'^{\mathsf T}J\Psi' = J then (Φ′Ψ′)TJ(Φ′Ψ′)=Ψ′TJΨ′=J(\Phi'\Psi')^{\mathsf T}J(\Phi'\Psi') = \Psi'^{\mathsf T}J\Psi' = J. ■\blacksquare

Exercise 2 (exact conservation of L\mathbf L). Show that the leapfrog conserves L\mathbf L for any hh.

Show solution

Proof. In a kick, the positions do not change and Δpi=τFi\Delta\mathbf p_i = \tau\mathbf F_i with Fi=∑j≠iFij\mathbf F_i = \sum_{j\ne i}\mathbf F_{ij}, Fij=−Fji\mathbf F_{ij} = -\mathbf F_{ji} parallel to ri−rj\mathbf r_i - \mathbf r_j. Thus ΔL=τ∑i<j(ri−rj)×Fij=0\Delta\mathbf L = \tau\sum_{i<j}(\mathbf r_i - \mathbf r_j)\times\mathbf F_{ij} = \mathbf 0. In a drift, ri↦ri+τpi/mi\mathbf r_i \mapsto \mathbf r_i + \tau\mathbf p_i/m_i and ΔL=∑i(τ/mi) pi×pi=0\Delta\mathbf L = \sum_i (\tau/m_i)\,\mathbf p_i\times\mathbf p_i = \mathbf 0. ■\blacksquare (RK4 does not have this property: its intermediate stages mix positions and velocities from different instants.)

Exercise 3 (RK4 dissipates energy in the oscillator). For q˙=p\dot q = p, p˙=−q\dot p = -q write z=q+ipz = q + i p, so that z˙=−iz\dot z = -i z. Show that RK4 gives zn+1=R(−ih) znz_{n+1} = R(-ih)\,z_n with R(w)=1+w+w2/2+w3/6+w4/24R(w) = 1 + w + w^2/2 + w^3/6 + w^4/24 and that

∣R(−ih)∣2=1−h672+h8576.\lvert R(-ih)\rvert^2 = 1 - \frac{h^6}{72} + \frac{h^8}{576}.
Show solution

Proof. Applying RK4 to z˙=μz\dot z = \mu z gives exactly the degree-4 Taylor polynomial of ehμe^{h\mu}. With θ=h\theta = h, R(−iθ)=(1−θ22+θ424)−i(θ−θ36)R(-i\theta) = \left(1 - \tfrac{\theta^2}2 + \tfrac{\theta^4}{24}\right) - i\left(\theta - \tfrac{\theta^3}6\right), and

∣R∣2=1−θ2+(14+112)θ4−θ624+θ8576+θ2−θ43+θ636=1−θ672+θ8576. ■\lvert R\rvert^2 = 1 - \theta^2 + \left(\tfrac14 + \tfrac1{12}\right)\theta^4 - \tfrac{\theta^6}{24} + \tfrac{\theta^8}{576} + \theta^2 - \tfrac{\theta^4}3 + \tfrac{\theta^6}{36} = 1 - \tfrac{\theta^6}{72} + \tfrac{\theta^8}{576}. \ \blacksquare

Since E=∣z∣2/2E = \lvert z\rvert^2/2, after n=t/hn = t/h steps En=E0(1−h6/72+… )t/h≈E0 e−th5/72E_n = E_0\left(1 - h^6/72 + \dots\right)^{t/h} \approx E_0\,e^{-t h^5/72}: a secular drift, linear in tt for th5≪1t h^5 \ll 1, and unbounded at long times.

Exercise 4 (exact invariant of the leapfrog in the oscillator). Show that the leapfrog applied to H=(p2+q2)/2H = (p^2 + q^2)/2 is the linear map

(qn+1pn+1)=(1−h22h−h(1−h24)1−h22)(qnpn),\begin{pmatrix} q_{n+1}\\ p_{n+1}\end{pmatrix} = \begin{pmatrix} 1 - \tfrac{h^2}2 & h\\ -h\left(1 - \tfrac{h^2}4\right) & 1 - \tfrac{h^2}2\end{pmatrix}\begin{pmatrix} q_{n}\\ p_{n}\end{pmatrix},

which conserves exactly Ih=p2+(1−h24)q2I_h = p^2 + \left(1 - \tfrac{h^2}4\right)q^2, which is stable if and only if 0<h<20 \lt h \lt 2, and that this agrees with H~\tilde H of section 3.

Show solution

Proof. Substituting, p1/2=p−h2qp_{1/2} = p - \tfrac h2 q, q1=(1−h22)q+hpq_1 = (1 - \tfrac{h^2}2)q + h p and p1=p1/2−h2q1p_1 = p_{1/2} - \tfrac h2 q_1 give the matrix M=(ah−hca)\mathcal M = \begin{pmatrix} a & h\\ -hc & a\end{pmatrix} with a=1−h2/2a = 1 - h^2/2, c=1−h2/4c = 1 - h^2/4. Its determinant is a2+h2c=1a^2 + h^2 c = 1 (area preserved: symplectic in 1 degree of freedom). With D=diag⁡(c,1)D = \operatorname{diag}(c, 1) one checks MTD M=D\mathcal M^{\mathsf T} D\,\mathcal M = D: the (1,1)(1,1) entry is a2c+h2c2=c(a2+h2c)=ca^2 c + h^2c^2 = c(a^2 + h^2c) = c, the (2,2)(2,2) entry is h2c+a2=1h^2 c + a^2 = 1 and the cross entry ahc−hca=0a h c - h c a = 0. Hence IhI_h is conserved. The eigenvalues satisfy λ2−2aλ+1=0\lambda^2 - 2a\lambda + 1 = 0; they have modulus 1 and are distinct if ∣a∣<1\lvert a\rvert \lt 1, that is, 0<h<20 \lt h \lt 2, with numerical frequency cos⁡(ω~h)=1−h2/2\cos(\tilde\omega h) = 1 - h^2/2. On the other hand, with T=p2/2T = p^2/2, V=q2/2V = q^2/2: {T,V}=−pq\{T,V\} = -pq, {T,{T,V}}=p2\{T,\{T,V\}\} = p^2, {V,{V,T}}=q2\{V,\{V,T\}\} = q^2, and

H~=p2+q22+h2(p212−q224)+O(h4)=1+h2/62[p2+(1−h24)q2]+O(h4),\tilde H = \frac{p^2+q^2}2 + h^2\left(\frac{p^2}{12} - \frac{q^2}{24}\right) + O(h^4) = \frac{1 + h^2/6}{2}\left[p^2 + \left(1 - \tfrac{h^2}4\right)q^2\right] + O(h^4),

proportional to IhI_h up to O(h4)O(h^4). ■\blacksquare The energy error H−H(0)H - H(0) is therefore bounded and of size O(h2)O(h^2), exactly what the general theory predicts.

Exercise 5 (predictability horizon). With λ=1\lambda = 1 (units G=1G=1), by how much does the prediction horizon with tolerance Δ=10−2\Delta = 10^{-2} lengthen if we go from δ0=10−6\delta_0 = 10^{-6} to δ0=10−16\delta_0 = 10^{-16}?

Show solution

Solution. tpred=ln⁡(Δ/δ0)/λt_{\text{pred}} = \ln(\Delta/\delta_0)/\lambda: from ln⁡104≈9.2\ln 10^{4} \approx 9.2 to ln⁡1014≈32.2\ln 10^{14} \approx 32.2. Ten orders of magnitude of initial precision buy only a factor ≈3.5\approx 3.5 in time.

Exercise 6 (with the simulator). (a) In the figure-eight with “Both”, set h=0.1h = 0.1: which integrator keeps the shape of the orbit for more periods? Relate it to section 3. (b) Turn on “Perturb” in the figure-eight and in the Pythagorean problem. Compare how δ(t)\delta(t) grows in the logarithmic plot: a straight line of slope λ/ln⁡10\lambda/\ln 10 indicates exponential growth. (c) In the Pythagorean problem, reduce hh from 10−210^{-2} to 10−410^{-4}. Does the final state change? Why does this not contradict the theorem of section 3? (Hint: close encounters and chaos.)

Show solution

(a) The leapfrog: with h=0.1h = 0.1 (≈63\approx 63 steps per period) RK4 deforms the eight within a few turns because its energy error accumulates, whereas the leapfrog keeps the shape (although with a growing phase lag along the curve: the phase error does grow; what does not grow is the energy error). (b) In the eight, log⁡10δ\log_{10}\delta grows slowly and almost linearly in tt (polynomial growth: linearly stable orbit); in the Pythagorean problem it grows as a straight line in the logarithmic plot, with jumps at each close encounter: slope λ/ln⁡10\lambda/\ln 10. (c) It changes: with h=10−2h = 10^{-2} the close encounters are not resolved (the local time scale r3/Gm\sqrt{r^3/Gm} drops below 10−210^{-2}) and the error in them, amplified by chaos (λ∼1\lambda \sim 1), leads to a different final state. The theorem requires h<h0h \lt h_0 with h0h_0 set by the analyticity of HH along the orbit, and in a close encounter h0h_0 becomes tiny; moreover, the theorem bounds the energy, not the trajectory, and in a chaotic system two trajectories with the same energy can end in completely different states.

Exercise 7 (computational: Yoshida). Implement the leapfrog and the order-4 Yoshida integrator for the two-body problem (G=1G = 1, m1=1m_1 = 1, m2=0.01m_2 = 0.01, a=1a = 1, e=0.7e = 0.7). (a) Reproduce the table of §3.1 by measuring max⁡∣ΔE/E0∣\max\lvert\Delta E/E_0\rvert over 100 periods with h=0.03h = 0.03. (b) Measure the order of both over one period with h=0.02,0.01,0.005h = 0.02, 0.01, 0.005. (c) Apply Yoshida to the harmonic oscillator q˙=p\dot q = p, p˙=−q\dot p = -q and determine numerically the hh beyond which the solution blows up; compare it with 2/1.70242/1.7024.

Show solution

Skeleton in Python for (a) and (b):

import numpy as np
w1 = 1/(2 - 2**(1/3)); w0 = -2**(1/3)/(2 - 2**(1/3))
def acel(r):                      # r: (N,3); global masses m
    a = np.zeros_like(r)
    for i in range(len(r)):
        d = r - r[i]; dist = np.linalg.norm(d, axis=1); dist[i] = np.inf
        a[i] = np.sum((m/dist**3)[:, None]*d, axis=0)
    return a
def leapfrog(r, v, h):
    v = v + 0.5*h*acel(r); r = r + h*v; v = v + 0.5*h*acel(r); return r, v
def yoshida4(r, v, h):
    for w in (w1, w0, w1): r, v = leapfrog(r, v, w*h)
    return r, v
def energia(r, v):
    K = 0.5*np.sum(m*np.sum(v**2, axis=1)); U = 0
    for i in range(len(r)):
        for j in range(i+1, len(r)): U -= m[i]*m[j]/np.linalg.norm(r[i]-r[j])
    return K + U

The initial conditions are taken from dosCuerpos (apocenter: r=a(1+e)r = a(1+e), v=μ(1−e)/(a(1+e))v = \sqrt{\mu(1-e)/(a(1+e))}, shared according to the masses). Expected results: table of §3.1 (leapfrog 2.2×10−22.2\times10^{-2}, Yoshida 6.2×10−46.2\times10^{-4}), error ratios ≈4\approx 4 and ≈16\approx 16 when dividing hh by 2. (c) For the oscillator, the leapfrog matrix with step whwh has eigenvalues of modulus 1 if ∣wh∣<2\lvert wh\rvert \lt 2; the longest step is ∣w0∣h=1.7024 h\lvert w_0\rvert h = 1.7024\,h, hence Yoshida is stable if h<2/1.7024=1.175h \lt 2/1.7024 = 1.175. Numerically, with h=1.17h = 1.17 the energy oscillates within bounds and with h=1.18h = 1.18 it grows exponentially. It is the price of the negative step: order 4 buys precision in exchange for 40% less stability margin.

✦ Self-assessment

  1. 1. Which property of the leapfrog explains that its energy error stays bounded over very long times?
  2. 2. RK4 applied to a bounded orbit over thousands of periods typically shows…
  3. 3. The order-4 Yoshida integrator is obtained by composing three leapfrogs with weights w₁, w₀, w₁ such that…
  4. 4. If the maximal Lyapunov exponent of a system is λ = 1 (in units G = 1), improving the precision of the initial conditions from 10⁻⁶ to 10⁻¹² lengthens the prediction horizon (tolerance 10⁻²)…
  5. 5. The Chenciner–Montgomery figure-eight is…
  6. 6. In Burrau's Pythagorean problem, the fixed-step leapfrog shows energy-error spikes at the close encounters because…
Answer all the questions to check.

Further reading

  • Hairer, Lubich & Wanner (2006), Geometric Numerical Integration: ch. I (examples and leapfrog), §II.4–II.5 (composition and splitting methods: construction of Yoshida), ch. VI (symplectic integrators), ch. IX (backward error analysis; Theorem 8.1 on energy conservation).
  • Yoshida (1990), Phys. Lett. A 150, 262: the coefficients of orders 4, 6 and 8 and the proof by BCH.
  • Chenciner & Montgomery (2000), Ann. Math. 152, 881, and Simó (2002): existence and stability of the eight. Moore (1993), PRL 70, 3675: its numerical discovery.
  • Szebehely & Peters (1967), AJ 72, 876: the complete solution of the Pythagorean problem, with the figures of the ejection.
  • Benettin, Galgani, Giorgilli & Strelcyn (1980), Meccanica 15, 9: the renormalization method for Lyapunov exponents.
  • Aarseth, S. J. (2003), Gravitational N-Body Simulations, chs. 1–2: the N-body specialist’s point of view on integrators and regularization.
🍎 Teacher guide
Time
2 classes of 90 min plus a 2-hour programming workshop
Learning outcome
By the end, the student proves that the leapfrog is symplectic and conserves angular momentum, explains through the modified Hamiltonian why its energy error stays bounded, constructs the order-4 Yoshida integrator, and numerically estimates the Lyapunov exponent of a three-body system while interpreting its limitations.
Evidence of learning
Submission of exercise 7 (implementation of leapfrog and Yoshida with error table and measured order) and of exercise 6 (report with simulator screenshots and the estimate of λ for the Pythagorean problem with two different steps).

Materials

  • Python 3 with NumPy and Matplotlib
  • Reference code: src/lib/math/nbody.ts, yoshida.ts and their tests
  • The portal's N-body simulator
  • The papers of Chenciner–Montgomery (2000) and Szebehely–Peters (1967), for optional reading

Suggested sequence

Class 1 (90 min). Opening (15 min): the N-body problem as a Hamiltonian system; the ten integrals. Development (60 min): RK4 and leapfrog; proof of symplecticity (exercise 1) and conservation of L (exercise 2) on the board; the modified Hamiltonian and the statement of the energy conservation theorem; check on the oscillator (exercise 4). Closing (15 min): simulator with “2 bodies, e = 0.8”, compare the energy-error curves.

Class 2 (90 min). Opening (10 min): “can one have order 4 and symplecticity?”: construction of Yoshida (§3.1). Development (55 min): Lyapunov exponent, Benettin’s method, predictability horizon (exercise 5); the eight and the Pythagorean problem, with their stories. Closing (25 min): exercise 6 with the simulator; mini-quiz.

Workshop (2 h). Full exercise 7: leapfrog and Yoshida, error table, measured order and stability limit. Extension for those who finish: estimate λ of the Pythagorean problem with Benettin’s method and two values of h.

Common mistakes

  • Believing that a high-order method conserves energy better at long times than a low-order symplectic one. The table of §3 (RK4 versus leapfrog over 100 periods) refutes it; insist that order and structure are independent properties.
  • Confusing energy error with trajectory error. The leapfrog conserves the energy but its phase error grows linearly; in the eight it shows up as a lag along the curve.
  • Taking a finite-time estimate of λ as “the” Lyapunov exponent. Always ask for the integration time, the dependence on h and the check with an integrable system.
  • Thinking that the negative step of Yoshida is a mistake. Show Suzuki's theorem: with positive steps one cannot go beyond order 2.
  • Using an adaptive step with a symplectic method and expecting the energy to stay bounded. Refer to the next lesson (Runge–Kutta and adaptive step).
Key terms in this lesson (12)

Glossary terms that appear in the text. Each one links to its full entry.

  • Leapfrog A simulation method that alternates a "kick" (changing the velocity) and a "drift" (moving the position), like a frog jumping. It is simple, time-reversible and conserves energy very well.
  • Symplectic integrator A numerical method designed so that, when simulating orbits over millions of years, energy neither leaks away nor builds up little by little. That is why it is used to study the stability of the Solar System.
  • Runge–Kutta method An improvement on Euler's method that "samples" the slope at several intermediate points of each step and averages them. The fourth-order version (RK4) is thousands of times more accurate for the same step size.
  • N-body problem Simulating many bodies that all attract one another: a star cluster, a galaxy, the universe. Since each body feels all the others, the computation grows very quickly with N; the Barnes–Hut trick groups the distant ones together.
  • Deterministic chaos Motion that follows exact rules but is impossible to predict in the long run, because a tiny change at the start produces completely different results. The orbits of the planets are chaotic on scales of millions of years.
  • Perturbation Any small "nudge" that pulls an orbit away from a perfect ellipse: the attraction of other planets, Earth's flattening, air drag. That is how Neptune was discovered, through the perturbations of Uranus.
  • Horizon The line where the sky seems to touch the land or the sea. A celestial object "rises" when it crosses the horizon going up and "sets" when it crosses it going down.
  • Orbit The path one body follows around another because of gravity: the Moon around Earth, Earth around the Sun. Almost all are ellipses, slightly flattened ovals.
  • Lagrange point Five special places near two bodies (such as the Sun and Earth) where a spacecraft can stay "still" with respect to them. The James Webb telescope is at one of them, L2, 1.5 million km away.
  • Two-body problem Working out how two bodies move when they attract only each other, like the Sun and a planet. It has an exact solution: Kepler's ellipses. With three bodies there is no longer a closed formula.
  • Law of universal gravitation Newton's rule: all bodies attract one another with a force that grows with their masses and decreases with the square of the distance. The same force that makes an apple fall holds the Moon in place.
  • Perihelion The point of the orbit closest to the Sun; there the planet moves fastest. Earth passes through it in early January. For the Moon around Earth it is called perigee.

See the whole glossary →

References

  • Hairer, E., Lubich, C. & Wanner, G. (2006). Geometric Numerical Integration: Structure-Preserving Algorithms for Ordinary Differential Equations, 2nd ed. Springer. Chs. I, VI and IX.
  • Chenciner, A. & Montgomery, R. (2000). A remarkable periodic solution of the three-body problem in the case of equal masses. Annals of Mathematics 152, 881–901.
  • Moore, C. (1993). Braids in classical dynamics. Physical Review Letters 70, 3675–3679.
  • Simó, C. (2002). Dynamical properties of the figure eight solution of the three-body problem. Contemporary Mathematics 292, 209–228.
  • Burrau, C. (1913). Numerische Berechnung eines Spezialfalles des Dreikörperproblems. Astronomische Nachrichten 195, 113–118.
  • Szebehely, V. & Peters, C. F. (1967). Complete solution of a general problem of three bodies. The Astronomical Journal 72, 876–883.
  • Benettin, G., Galgani, L., Giorgilli, A. & Strelcyn, J.-M. (1980). Lyapunov characteristic exponents for smooth dynamical systems and for Hamiltonian systems. Meccanica 15, 9–20 (part 1) and 21–30 (part 2). DOI 10.1007/BF02128236 and 10.1007/BF02128237
  • Verlet, L. (1967). Computer 'experiments' on classical fluids. Physical Review 159, 98–103.
  • Yoshida, H. (1990). Construction of higher order symplectic integrators. Physics Letters A 150, 262–268. doi:10.1016/0375-9601(90)90092-3

Simulators for this lesson: N bodies: integrators and chaos