Flow Lab

Syllabus / Module 4: Numerical Methods for ODEs — The Theory

Lesson 4.7

Energy & Symplectic Integrators

Why RK4 makes planets spiral away: Hamiltonian systems, energy drift, symplectic Euler, Störmer–Verlet/leapfrog.

Written byClaude Sonnet

🌊
Why this matters for simulation: SPH and particle-based fluid simulators use leapfrog/Verlet integration.

By the end of this lesson you can…

  • Measure the long-run energy behavior of Euler, RK4, symplectic Euler and velocity Verlet on a pendulum and a Kepler orbit, and say which drifts and which stays bounded.
  • Write a system in Hamiltonian form and derive symplectic Euler (both variants) and velocity Verlet as compositions of exact kicks and drifts.
  • Show that a step map has Jacobian determinant 1 by writing it as a product of shears, and compare with the determinants of Euler and RK4.
  • Verify time reversibility and the equivalence of leapfrog, velocity Verlet and position Verlet in code.
  • Explain the shadow-Hamiltonian picture on the harmonic oscillator and say why variable step sizes and dissipation break the guarantee.
  • Implement velocity Verlet and use it for long simulations with a fixed time step.
On this page
·
  1. What a long run does to RK4
  2. Hamiltonian form
  3. Symplectic Euler: kick, then drift
  4. Why the determinant is 1: shears
  5. Velocity Verlet, leapfrog and position Verlet
  6. Reversibility
  7. The shadow Hamiltonian
  8. Where the guarantee fails
  9. The lab
  10. Leapfrog’s parasitic root
  11. Exercise
  12. Key Takeaways

At the end of 4.3 Runge–Kutta Methods & Butcher Tableaux RK4 on a pendulum lost energy slowly and steadily, and in 4.6 Multistep Methods AB2 gained it. A game, a molecular-dynamics code, an SPH solver or an N-body integrator takes millions of steps at a step size far too coarse for RK4, and what it needs is that the energy does not drift, even when the individual steps are not very accurate. A family of methods gives exactly that. They are old (Verlet 1967, from memory), cheap (one force evaluation per step) and the reason they work is an area-preservation property you can check with the determinant of 1.4 Determinants as Volume Scaling.

▶ Video Energy drift of RK4 against Verlet, Hamiltonian form, kick and drift, area-preserving shears, reversibility, the shadow Hamiltonian, and where the guarantee failscaptions only
Transcript
  1. Run a pendulum from one radian to time one thousand, step one half. RK4 loses energy steadily, minus 0.123. Symplectic Euler and Verlet wobble but never drift; Verlet stays within 0.0266. At step 0.1, though, RK4 still wins: 4.59 times ten to the minus 5 against 1.06 times ten to the minus 3.
  2. In Hamiltonian form, q prime is dH by dp and p prime is minus dH by dq. Symplectic Euler is a kick, which changes the momentum, then a drift, which moves the position using the new momentum. Drift first gives the other variant.
  3. Why does this keep area? A kick shears a blob of states vertically, a drift shears it horizontally. A shear keeps area: its determinant is one. Determinants multiply, so any chain of kicks and drifts preserves area.
  4. One step on the oscillator, applied to a blob of area one. Euler inflates it to 1.25; RK4 shrinks it by 2.1 times ten to the minus 4, which is its energy loss; both symplectic methods give one. On the pendulum, Euler is plus 0.135, RK4 minus 1.76 times ten to the minus 5, the others zero.
  5. To time ten times two pi, 126 steps: the product of determinants is 0.345 for Euler, 0.9857 for RK4, one for the symplectic pair. A polygon of 240 states agrees. But at step 0.1 Euler's polygon claims 2730 while the product says 1.14: trust the product.
  6. Half a kick, a drift, half a kick: velocity Verlet, second order. Merging half kicks gives leapfrog; drift first gives position Verlet, agreeing to about ten to the minus 14. Run a thousand steps, flip the momentum, run back: Verlet returns to a few times ten to the minus 14, RK4 misses by 7.3 times ten to the minus 5, symplectic Euler by 0.555.
  7. On the oscillator, symplectic Euler exactly conserves a nearby energy, p squared plus q squared minus h p q, over two: to about ten to the minus 14 over a hundred thousand steps, while the true energy ranges from 0.4 to 0.6667. Verlet's was checked by code, not derived. The general theory is from memory.
  8. Kepler, eccentricity 0.6. RK4's worst energy error grows steadily: 3.5 times ten to the minus 4 at ten orbits, 3.55 times ten to the minus 2 at a thousand. Verlet stays at 9.49 times ten to the minus 3. RK4 leads for about a hundred orbits; extending its line, they meet near 270 orbits, an extrapolation.
  9. But bounded energy is not accurate position. After ten orbits RK4 is 0.073 from the truth, Verlet 1.77 and symplectic Euler 1.99, about the orbit's size. The orbit precesses.
  10. Verlet needs a fixed step. With h equal to 0.05 r to the three halves, 12 percent more steps, the energy drifts in a straight line: minus 0.0246 after a thousand orbits, against a fixed-step bound of 0.0095. Random steps wander to nearly 0.09.
  11. Damping breaks the premise: a damped oscillator is not Hamiltonian. At step one half, to time 50, Verlet's energy is 8.55 percent low and RK4's 0.23 percent high, ordinary accuracy, not a symplectic failure. Ge and Marsden: no fixed-step symplectic method conserves energy exactly, from memory.
  12. Leapfrog on y prime equals minus y has a second root of modulus 1.105, and 1.105 to the fiftieth is 147: unstable on decay. On y prime equals i y both moduli are exactly one up to s of one; at 1.1 they split to 1.558 and 0.642. Verlet's step is twice s, so h is at most two.
  13. So: kicks and drifts are shears, area survives, energy stays bounded. Use a fixed step and velocity Verlet for game physics; games, molecular dynamics, N-body and SPH do, from memory. Avoid variable steps and damping. Next, Module 5: second-order equations.

What a long run does to RK4

Block A of the lab at the end runs the pendulum θ′′=−sin⁡θ\theta'' = -\sin\theta from 11 rad at rest to T=1000T = 1000 with energy E=12p2−cos⁡qE = \tfrac12 p^2 - \cos q, p=θ′p = \theta' (the example of 4.3 Runge–Kutta Methods & Butcher Tableaux). The columns are E−E0E - E_0 at the end and the largest ∣E−E0∣\lvert E - E_0\rvert seen. Euler, symplectic Euler and Verlet are defined below; Euler is included so you can see the problem.

methodh=0.5h = 0.5: endmaxh=0.1h = 0.1: endmax
Euler+19.28+19.2823.6823.68+48.56+48.5648.5948.59
RK4−0.12292-0.122920.122920.12292−4.578×10−5-4.578\times10^{-5}4.587×10−54.587\times10^{-5}
symplectic Euler−0.08439-0.084390.137450.13745+0.01301+0.013010.022620.02262
Verlet−0.01316-0.013160.026560.02656−8.524×10−4-8.524\times10^{-4}1.061×10−31.061\times10^{-3}

The RK4 column only ever grows (the loss per step is 1−∣R(ih)∣1 - \lvert R(ih)\rvert, 4.3 Runge–Kutta Methods & Butcher Tableaux), while the symplectic columns wobble. At h=0.1h = 0.1 RK4 is still ahead of Verlet by a factor 2323 at T=1000T = 1000. A harder case is a Kepler orbit, H=12∣p∣2−1/∣q∣H = \tfrac12\lvert p\rvert^2 - 1/\lvert q\rvert, eccentricity e=0.6e = 0.6, period 2π2\pi, 125125 steps per orbit (block B; E0=−0.5E_0 = -0.5):

methodmax⁡∣ΔE∣\max\lvert\Delta E\rvert after 10 orbits100 orbits1000 orbits
RK43.497×10−43.497\times10^{-4}3.476×10−33.476\times10^{-3}3.554×10−23.554\times10^{-2}
symplectic Euler9.730×10−29.730\times10^{-2}9.731×10−29.731\times10^{-2}9.731×10−29.731\times10^{-2}
Verlet9.490×10−39.490\times10^{-3}9.490×10−39.490\times10^{-3}9.490×10−39.490\times10^{-3}

RK4 is better for the first hundred orbits and then loses, because its error grows by about 3.5×10−53.5\times10^{-5} per orbit while Verlet’s does not grow at all. Extrapolating that straight line, the two meet near 9.49×10−3/3.5×10−5≈2709.49\times10^{-3} / 3.5\times10^{-5} \approx 270 orbits (an estimate from these three columns, not a computed crossing). Honesty about the other side: bounded energy is not accurate position. Ten orbits in, the distance from the true position is 0.07280.0728 for RK4 but 1.771.77 for Verlet and 1.991.99 for symplectic Euler (block B). The orbit precesses, it does not lose its energy. What the symplectic methods buy is the right qualitative long-time behavior, not a smaller error at any step.

Hamiltonian form

Take a mass with position qq and momentum pp in a potential V(q)V(q) (unit mass), force F(q)=−V′(q)F(q) = -V'(q), and Hamiltonian H(q,p)=12p2+V(q)H(q, p) = \tfrac12 p^2 + V(q). Then

q′=∂H∂p=p,p′=−∂H∂q=F(q).q' = \frac{\partial H}{\partial p} = p, \qquad p' = -\frac{\partial H}{\partial q} = F(q).

Along a solution H′=Hqq′+Hpp′=HqHp−HpHq=0H' = H_q q' + H_p p' = H_q H_p - H_p H_q = 0: energy is conserved. The pendulum is V=−cos⁡qV = -\cos q; the harmonic oscillator is V=q2/2V = q^2/2; with vectors q,p∈R2q, p \in \mathbb{R}^2 and V=−1/∣q∣V = -1/\lvert q\rvert you get Kepler, and NN particles with pair potentials give N-body, molecular dynamics and (without viscosity) SPH.

There is a second conservation law, geometric. The velocity field (Hp,−Hq)(H_p, -H_q) in the (q,p)(q, p) plane has divergence Hpq−Hqp=0H_{pq} - H_{qp} = 0, so by 2.7 Divergence: Sources & Sinks the flow neither compresses nor expands area: a blob of initial conditions is carried to a blob of the same area (Liouville’s theorem, from memory in this form). For a step map Φh\Phi_h the area factor is the Jacobian determinant of 2.2 Linearization, the Chain Rule & the Jacobian, and a method whose det⁡DΦh\det D\Phi_h is exactly 11 is called symplectic (in one degree of freedom; in higher dimensions it preserves the 2-form ∑dqi∧dpi\sum dq_i \wedge dp_i, from memory).

Symplectic Euler: kick, then drift

Euler’s method on this system uses old values in both updates: qn+1=qn+hpnq_{n+1} = q_n + h p_n, pn+1=pn+hF(qn)p_{n+1} = p_n + hF(q_n). Its Jacobian is (1hhF′1)\begin{pmatrix}1 & h\\ hF' & 1\end{pmatrix} with determinant 1−h2F′1 - h^2F', which is 1+h21 + h^2 on the oscillator (F′=−1F' = -1): every step inflates area by 1+h21 + h^2, and also energy, since (q+hp)2+(p−hq)2=(1+h2)(q2+p2)(q + hp)^2 + (p - hq)^2 = (1 + h^2)(q^2 + p^2). That is the energy gain Euler shows in the table.

The cure is to use the newest value:

pn+1=pn+hF(qn),qn+1=qn+h pn+1.p_{n+1} = p_n + hF(q_n), \qquad q_{n+1} = q_n + h\,p_{n+1}.

This is symplectic Euler, “kick then drift”. The other variant drifts first, qn+1=qn+hpnq_{n+1} = q_n + hp_n, pn+1=pn+hF(qn+1)p_{n+1} = p_n + hF(q_{n+1}). Both come from splitting H=T+VH = T + V with T=12p2T = \tfrac12p^2: the Hamiltonian VV alone has the exact flow q′=0q' = 0, p′=F(q)p' = F(q), a kick Kt:p↦p+tF(q)K_t: p \mapsto p + tF(q); the Hamiltonian TT alone has q′=pq' = p, p′=0p' = 0, a drift Dt:q↦q+tpD_t: q \mapsto q + tp. Both flows are explicit, and applying them in turn (Dh∘KhD_h \circ K_h or Kh∘DhK_h \circ D_h) is one step. The error is first order: the splitting is wrong by the commutator of the two flows, O(h2)O(h^2) per step.

Why the determinant is 1: shears

The Jacobians of the pieces are

DKt=(10tF′(q)1),DDt=(1t01).DK_t = \begin{pmatrix}1 & 0\\ tF'(q) & 1\end{pmatrix}, \qquad DD_t = \begin{pmatrix}1 & t\\ 0 & 1\end{pmatrix}.

Each is a shear (1.2 Matrices as Transformations): it slides rows or columns of the plane without changing the area of a parallelogram, and has determinant 11. A product has determinant det⁡Adet⁡B=1\det A \det B = 1 (1.4 Determinants as Volume Scaling), so any composition of kicks and drifts, with any step sizes and at any point, preserves area exactly. That is the entire proof; the nonlinearity of FF hides in the entry F′(q)F'(q) and does not matter. Block E multiplies these matrices and, for RK4, differentiates the step by running RK4 on (q,p,A)(q, p, A) with A′=(01F′0)AA' = \begin{pmatrix}0&1\\F'&0\end{pmatrix}A (the derivative of the RK4 map). It prints det⁡J−1\det J - 1 at h=0.5h = 0.5 and q=1q = 1:

methodoscillatorpendulum at q=1q = 1
Euler+0.25+0.25+0.13508+0.13508
RK4−2.1023×10−4-2.1023\times10^{-4}−1.7648×10−5-1.7648\times10^{-5}
symplectic Euler0000
Verlet00−1.1×10−16-1.1\times10^{-16} (rounding)

The Euler entries are h2h^2 and h2cos⁡1=0.13508h^2\cos 1 = 0.13508. The RK4 oscillator entry is −h6/72+h8/576=−2.1023×10−4-h^6/72 + h^8/576 = -2.1023\times10^{-4}, which is ∣R(ih)∣2−1\lvert R(ih)\rvert^2 - 1 of 4.3 Runge–Kutta Methods & Butcher Tableaux: the amplitude factor 1−2.1023×10−4=0.9998949\sqrt{1 - 2.1023\times10^{-4}} = 0.9998949 you met there. So RK4’s energy loss and its shrinking of area are the same fact. The sim below measures them on a blob.

Velocity Verlet, leapfrog and position Verlet

One kick-drift pair has only first order, but a symmetric composition does better. Velocity Verlet is half a kick, a full drift, half a kick,

pn+1/2=pn+h2F(qn),qn+1=qn+h pn+1/2,pn+1=pn+1/2+h2F(qn+1),p_{n+1/2} = p_n + \tfrac h2F(q_n), \qquad q_{n+1} = q_n + h\,p_{n+1/2}, \qquad p_{n+1} = p_{n+1/2} + \tfrac h2F(q_{n+1}),

i.e. Kh/2 Dh Kh/2K_{h/2}\,D_h\,K_{h/2}. That a palindromic splitting has even order is a standard fact (from memory); the data agree: the largest energy error over T=200T = 200 is 1.699×10−2,4.245×10−3,1.061×10−3,2.653×10−41.699\times10^{-2}, 4.245\times10^{-3}, 1.061\times10^{-3}, 2.653\times10^{-4} for h=0.4,0.2,0.1,0.05h = 0.4, 0.2, 0.1, 0.05, ratios 4.002,4.000,4.0004.002, 4.000, 4.000 (block A): second order. Counting force evaluations, the last half kick of one step and the first of the next share the same F(qn+1)F(q_{n+1}), so a run costs one evaluation per step.

Glue consecutive half kicks into full kicks and you get leapfrog, with vv living at half steps: vn+1/2=vn−1/2+hF(qn)v_{n+1/2} = v_{n-1/2} + hF(q_n), qn+1=qn+hvn+1/2q_{n+1} = q_n + hv_{n+1/2}. Do the drift first and you get position Verlet, Dh/2KhDh/2D_{h/2}K_hD_{h/2}. Block C verifies, over 500500 steps at h=0.3h = 0.3 on the pendulum, that leapfrog and velocity Verlet agree to about 10−1410^{-14} (rounding; the last digits depend on the platform’s math library), and that position Verlet is velocity Verlet seen through a half-step change of variables. With maps applied right to left, (Dh/2KhDh/2)n=(Kh/2Dh/2)−1 (Kh/2DhKh/2)n (Kh/2Dh/2)(D_{h/2}K_hD_{h/2})^n = (K_{h/2}D_{h/2})^{-1}\,(K_{h/2}D_hK_{h/2})^n\,(K_{h/2}D_{h/2}): both sides are the same kicks and drifts regrouped. The code finds the two sides equal to the same 10−1410^{-14}. They are one method viewed from different places.

Reversibility

Let S(q,p)=(q,−p)S(q, p) = (q, -p) flip the momentum. Then SKtS=K−t=Kt−1S K_t S = K_{-t} = K_t^{-1} (a kick changes pp by tF(q)tF(q), and FF does not see pp) and SDtS=D−t=Dt−1S D_t S = D_{-t} = D_t^{-1}. For the palindrome, S (KDK) S=K−1D−1K−1=(KDK)−1S\,(K D K)\,S = K^{-1}D^{-1}K^{-1} = (KDK)^{-1}: run forward nn steps, flip pp, run nn steps, and you are back at the start. For symplectic Euler, S(KD)S=K−1D−1=(DK)−1S (KD) S = K^{-1}D^{-1} = (DK)^{-1}, the other variant’s inverse, so it is not reversible. Block D runs the pendulum from (1,0.3)(1, 0.3) with h=0.1h = 0.1 for 10001000 steps, flips, and returns 10001000 steps:

methoddistance from the start
Verlet≈3×10−14\approx 3\times10^{-14} (rounding)
RK47.3×10−57.3\times10^{-5}
symplectic Euler0.5550.555

Verlet comes back to rounding; RK4 does not, since its error is not symmetric. Reversibility matters for the long run as well (it keeps the energy error from drifting in typical cases; from memory), but the main account is the next section.

The shadow Hamiltonian

On the oscillator, symplectic Euler is p′=p−hqp' = p - hq, q′=q+hp′q' = q + hp'. Claim: H~=12(p2+q2−hpq)\tilde H = \tfrac12(p^2 + q^2 - hpq) is conserved exactly. With the new values, p′2+q′2−hp′q′=p′2+q′(q′−hp′)=p′2+q′qp'^2 + q'^2 - hp'q' = p'^2 + q'(q' - hp') = p'^2 + q'q. Now q′q=q2+hp′q=q2+hpq−h2q2q'q = q^2 + hp'q = q^2 + hpq - h^2q^2, and p′2=p2−2hpq+h2q2p'^2 = p^2 - 2hpq + h^2q^2. The sum is p2+q2−hpqp^2 + q^2 - hpq. □\square So the numerical orbit lies on an ellipse of H~\tilde H and does not spiral. The true energy H=H~+12hpqH = \tilde H + \tfrac12hpq moves along that ellipse. On p2+q2−hpq=1p^2 + q^2 - hpq = 1 the product pqpq ranges from −1/(2+h)-1/(2+h) to 1/(2−h)1/(2-h), so at h=0.5h = 0.5, HH ranges over [0.4,0.6667][0.4, 0.6667]. Block F runs 10510^5 steps and finds max⁡∣H~−H~0∣=1.4×10−14\max\lvert\tilde H - \tilde H_0\rvert = 1.4\times10^{-14} and H∈[0.40000,0.66667]H \in [0.40000, 0.66667]. For Verlet the same code finds H~=12(p2+(1−h2/4)q2)\tilde H = \tfrac12(p^2 + (1 - h^2/4)q^2) conserved to 1.8×10−141.8\times10^{-14} over 10510^5 steps, with H∈[0.46875,0.5]H \in [0.46875, 0.5] (not derived here).

The general statement is backward error analysis (from memory): a symplectic method of order rr is, to any chosen order of accuracy, the exact flow of a nearby Hamiltonian H~=H+hrHr+…\tilde H = H + h^rH_r + \dots, and H~\tilde H is conserved up to errors exponentially small in 1/h1/h over exponentially long times. So HH stays within O(hr)O(h^r) of its starting value for those times, with no drift. For the pendulum with Verlet the bound scales like h2h^2, as block A’s ratios of 44 show. A related theorem of Ge and Marsden (from memory) says that no symplectic method with a fixed step can conserve the energy exactly for a generic HH either; the shadow H~\tilde H is what you get instead.

◆ Interactive Euler, RK4, symplectic Euler and Verlet on the oscillator, the pendulum or a Kepler orbit: energy error, blob area, orbit

Loading simulation…

The sim advects the center point and a ring of 240240 initial conditions with each method. The ring area is the shoelace formula, A=12∑(qipi+1−qi+1pi)A = \tfrac12\sum (q_ip_{i+1} - q_{i+1}p_i); it is shown next to the product of the one-step det⁡J\det J along the center orbit, which is the area factor of an infinitesimal blob and needs no polygon. A headless run reproduced these:

  1. Oscillator, h=0.5h = 0.5 (steps of 0.49870.4987), T=10×2πT = 10\times2\pi: area factor 1.42×10121.42\times10^{12} (Euler, det⁡−1=+0.249\det - 1 = +0.249 per step), 0.9742620.974262 (RK4, −2.07×10−4-2.07\times10^{-4} per step), 1.000001.00000 for symplectic Euler and Verlet. The shadow readout shows 6.7×10−166.7\times10^{-16} and 1.1×10−151.1\times10^{-15}.
  2. Pendulum, h=0.5h = 0.5, T=10×2πT = 10\times2\pi: the product of det⁡J\det J is 0.3450.345 for Euler (below 11: its determinant 1+h2cos⁡q1 + h^2\cos q is under 11 in the upper half of the swing, where the orbit spirals out), 0.98570.9857 for RK4, 11 for the others; the ring polygon agrees (0.3450.345, 0.98560.9856, 0.99980.9998, 0.99990.9999). When a ring is stretched too far (long TT and a large hh) the polygon stops following it, the sim marks the number with a star, and the product of det⁡J\det J is the one to trust.
  3. Kepler, h=0.05h = 0.05, T=30T = 30 orbits: max⁡∣ΔE∣=1.041×10−3\max\lvert\Delta E\rvert = 1.041\times10^{-3} (RK4), 9.388×10−39.388\times10^{-3} (Verlet), 9.662×10−29.662\times10^{-2} (symplectic Euler), 0.4800.480 (Euler). Drag TT from 11 to 3030 and RK4’s line climbs while the symplectic lines stay flat. At h=0.1h = 0.1 RK4’s 3.230×10−23.230\times10^{-2} is already close to Verlet’s 3.922×10−23.922\times10^{-2}.

Where the guarantee fails

Variable step sizes. Each step of Verlet is symplectic for any fixed hh, but the shadow H~\tilde H depends on hh. If the step is chosen from the current state, h=h(q,p)h = h(q, p), the Jacobian of x↦Φh(x)(x)x \mapsto \Phi_{h(x)}(x) gains terms from ∂h/∂x\partial h/\partial x, its determinant is no longer 11, and the argument is lost. Block H runs Verlet on the Kepler orbit for 10001000 orbits. With a fixed h=2π/125h = 2\pi/125 the energy error after 200,400,…,1000200, 400, \dots, 1000 orbits is +9.2×10−3+9.2\times10^{-3} each time, always inside the bound. With h=0.05 r3/2h = 0.05\,r^{3/2} (the natural orbital step: smaller near the Sun; rr at the start of the step) and 140515140515 steps (12%12\% more), the error is −4.40×10−3,−8.71×10−3,−1.39×10−2,−1.92×10−2,−2.46×10−2-4.40\times10^{-3}, -8.71\times10^{-3}, -1.39\times10^{-2}, -1.92\times10^{-2}, -2.46\times10^{-2}: a straight-line drift of the kind RK4 shows, already 2.62.6 times the fixed run’s bound at the end. Even a state-independent but random hh in [0.025,0.075][0.025, 0.075] (seeded) wanders to between 8×10−28\times10^{-2} and 9×10−29\times10^{-2} (8.64×10−28.64\times10^{-2} in the video’s run) against 9.5×10−39.5\times10^{-3} (the digits shift between platforms), since each step has its own H~\tilde H. Choosing steps in a way that keeps symmetry (a function of both ends of the step) exists (from memory, Hairer, Lubich and Wanner). It is why adaptive RK45 of 4.4 Adaptive Step Size & RK45 is the wrong tool for a 10-million-step orbit.

Dissipation. Nothing here applies to q′′=−q−cq′q'' = -q - cq': a damped system is not Hamiltonian and the true flow shrinks area by e−che^{-ch} per step. Block I runs c=0.2c = 0.2 to T=50T = 50 with h=0.5h = 0.5 and compares E(T)/E(0)E(T)/E(0) with the exact solution: Verlet (the damping force uses the most recent velocity) is 8.55%8.55\% low and RK4 is 0.23%0.23\% high. Nothing makes Verlet’s decay rate right; its guarantee concerns conservative problems, and on a dissipative one only the ordinary order of accuracy counts, where RK4 is better.

✅ Concept check

Verlet on a Kepler orbit keeps its energy within 9.49e-3 for 1000 orbits, while RK4 drifts. A student concludes that Verlet's positions are therefore more accurate than RK4's. What is wrong?

✅ Concept check

Explicit Euler is applied to the harmonic oscillator with h = 0.1 for 100 steps. By what factor has the area of a blob of initial conditions grown?

✅ Concept check

Which simulation gains the least from replacing RK4 with a symplectic integrator?

The lab

The playground reproduces every number in this lesson (blocks A to I) with plain Python; it takes a few seconds.

🐍 PythonLab: energy drift, Kepler, leapfrog and position Verlet, reversibility, determinants, shadow Hamiltonians, parasitic roots, variable steps, damping (blocks A to I)
⌘/Ctrl + Enter

Leapfrog’s parasitic root

Multistep’s Problem 3 showed that leapfrog yn+1=yn−1+2sfny_{n+1} = y_{n-1} + 2sf_n on y′=−yy' = -y has roots 0.90500.9050 and −1.1050-1.1050 at s=0.1s = 0.1, and 1.105050=147.21.1050^{50} = 147.2 (block G reproduces both): the second, parasitic root grows and alternates in sign, so the method is unstable on decay. Yet leapfrog is the heart of every code above. The resolution is the test equation. For y′=iyy' = iy the roots solve ξ2−2is ξ−1=0\xi^2 - 2is\,\xi - 1 = 0, so ξ=is±1−s2\xi = is \pm \sqrt{1 - s^2}, and for s≤1s \le 1 the two roots have modulus s2+1−s2=1\sqrt{s^2 + 1 - s^2} = 1 exactly: block G prints moduli 1.01.0 and 1.01.0 for s=0.1,0.5,0.99s = 0.1, 0.5, 0.99. The parasitic mode is a bounded saw-tooth, never amplified, and the physical mode does not decay. Past s=1s = 1 the roots are purely imaginary with moduli 1.55831.5583 and 0.64170.6417: unstable. The leapfrog spacing ss is half a Verlet step (qq is updated every other node), so s≤1s \le 1 is h≤2h \le 2, the stability limit found in Problem 1. Leapfrog is stable only on oscillations, which is what Hamiltonian systems consist of, and that is the sense in which it is both unstable (on dissipation) and excellent (on waves).

Exercise

🧪 Coding exerciseExercise: velocity Verlet on a spring
⌘/Ctrl + Enter
✏️ Pen & paperVelocity Verlet on the spring: one matrix●●○

For F(q)=−qF(q) = -q, one step maps (q,p)T(q, p)^T to M(q,p)TM(q, p)^T.

  1. Find MM and show det⁡M=1\det M = 1 and tr⁡M=2−h2\operatorname{tr} M = 2 - h^2.
  2. Both eigenvalues are on the unit circle exactly when ∣tr⁡M∣<2\lvert\operatorname{tr}M\rvert \lt 2. For which hh?
  3. At h=1h = 1 find the rotation angle and the number of steps after which the state returns exactly. At h=2.1h = 2.1 find the larger eigenvalue modulus.
Show worked solution
  1. pn+1/2=p−h2qp_{n+1/2} = p - \tfrac h2q, q′=q+hpn+1/2=(1−h22)q+hpq' = q + hp_{n+1/2} = (1 - \tfrac{h^2}2)q + hp, p′=pn+1/2−h2q′=(1−h22)p−h(1−h24)qp' = p_{n+1/2} - \tfrac h2q' = (1 - \tfrac{h^2}2)p - h(1 - \tfrac{h^2}4)q. So M=(1−h2/2h−h(1−h2/4)1−h2/2)M = \begin{pmatrix}1 - h^2/2 & h\\ -h(1 - h^2/4) & 1 - h^2/2\end{pmatrix}, tr⁡M=2−h2\operatorname{tr}M = 2 - h^2, det⁡M=(1−h2/2)2+h2(1−h2/4)=1−h2+h4/4+h2−h4/4=1\det M = (1 - h^2/2)^2 + h^2(1 - h^2/4) = 1 - h^2 + h^4/4 + h^2 - h^4/4 = 1.
  2. Eigenvalues solve λ2−(2−h2)λ+1=0\lambda^2 - (2 - h^2)\lambda + 1 = 0 with product 11. They are complex conjugates of modulus 11 iff (2−h2)2<4(2 - h^2)^2 \lt 4, i.e. 0<h<20 \lt h \lt 2.
  3. At h=1h = 1, tr⁡M=1=2cos⁡θ\operatorname{tr}M = 1 = 2\cos\theta gives θ=π/3\theta = \pi/3, so M6=IM^6 = I: six steps return to the start. At h=2.1h = 2.1, tr⁡M=−2.41\operatorname{tr}M = -2.41, λ=(−2.41±2.412−4)/2=−0.5327, −1.8773\lambda = (-2.41 \pm \sqrt{2.41^2 - 4})/2 = -0.5327,\ -1.8773: the amplitude grows by 1.87731.8773 per step.
✏️ Pen & paperThe other symplectic Euler●●○

Drift first: q′=q+hpq' = q + hp, p′=p−hq′p' = p - hq' on the oscillator. Show that H~=12(p2+q2+hpq)\tilde H = \tfrac12(p^2 + q^2 + hpq) is conserved exactly, and say how the sign compares with the kick-drift variant.

Show worked solution

With the new values, p′2+q′2+hp′q′=q′2+p′(p′+hq′)p'^2 + q'^2 + hp'q' = q'^2 + p'(p' + hq'). Since p′+hq′=pp' + hq' = p, this is q′2+p′p=q′2+p(p−hq′)=q′2+p2−hpq′q'^2 + p'p = q'^2 + p(p - hq')= q'^2 + p^2 - hpq'. Then q′2−hpq′=q′(q′−hp)=q′q=q2+hpqq'^2 - hpq' = q'(q' - hp) = q'q = q^2 + hpq. The total is p2+q2+hpqp^2 + q^2 + hpq. □\square (Run for 10410^4 steps at h=0.5h = 0.5 from (1,0.3)(1, 0.3) the largest change is about 10−1410^{-14}, rounding.) The kick-drift variant has −hpq-hpq and this one +hpq+hpq. The two are adjoints, S(KD)S=(DK)−1S(KD)S = (DK)^{-1}, and a step built from half steps of each (Verlet) is palindromic, which cancels the O(h)O(h) term ∓12hpq\mp\tfrac12hpq.

✏️ Pen & paperExplicit Euler on a damped oscillator●●●

q′=pq' = p, p′=−q−cpp' = -q - cp with c=0.2c = 0.2. The true flow shrinks area by e−che^{-ch} per time hh.

  1. Find the Jacobian of one explicit Euler step and its determinant.
  2. Evaluate it for h=0.1h = 0.1 and h=0.5h = 0.5 and compare with e−che^{-ch}.
  3. For which hh does Euler shrink area at all? What does this say about using it on dissipative flows?
Show worked solution
  1. J=(1h−h1−ch)J = \begin{pmatrix}1 & h\\ -h & 1 - ch\end{pmatrix}, det⁡J=1−ch+h2\det J = 1 - ch + h^2.
  2. h=0.1h = 0.1: 0.990.99 against e−0.02=0.9802e^{-0.02} = 0.9802. h=0.5h = 0.5: 1.151.15 against e−0.1=0.9048e^{-0.1} = 0.9048.
  3. det⁡J<1\det J \lt 1 iff h<ch \lt c. For h>ch \gt c the artificial growth h2h^2 beats the real damping chch, so the numerical solution spirals out although the true one decays. For h=0.5h = 0.5 the area grows by 15%15\% per step. The same +h2+h^2 is why Euler fails on the undamped oscillator; stability regions for dissipative problems are the subject of 4.5 Stability Regions & Stiffness.

Key Takeaways

  • RK4’s energy error on an oscillator grows linearly (the loss per step is built in), so a long run eventually loses to a second-order symplectic method with a worse error per step; for the Kepler orbit at 125125 steps per orbit the crossover estimate is about 270270 orbits.
  • A Hamiltonian system q′=Hpq' = H_p, p′=−Hqp' = -H_q conserves HH and preserves phase-space area; a method is symplectic when its step has Jacobian determinant 11.
  • Kick (p+=tF(q)p \mathrel{+}= tF(q)) and drift (q+=tpq \mathrel{+}= tp) are shears with determinant 11. Any composition is area-preserving: symplectic Euler is kick then drift, velocity Verlet is half-kick, drift, half-kick, with one force evaluation per step and second order.
  • Leapfrog, velocity Verlet and position Verlet are the same method up to relabelling (checked to about 10−1410^{-14}); the symmetric one is time reversible (forward, flip pp, back returns to within a few 10−1410^{-14}) while RK4 is not (7×10−57\times10^{-5}).
  • On the oscillator the numerical orbit exactly conserves a shadow Hamiltonian (H~=12(p2+q2−hpq)\tilde H = \tfrac12(p^2 + q^2 - hpq) for symplectic Euler); in general this holds to exponentially small terms (from memory), so HH oscillates by O(hr)O(h^r) instead of drifting.
  • The guarantee is lost with a step that depends on the state (Kepler energy drift −2.46×10−2-2.46\times10^{-2} after 10001000 orbits against a bounded 9×10−39\times10^{-3}), with random step sizes, and for dissipative systems (Verlet 8.55%8.55\% off on a damped oscillator).
  • Bounded energy is not small position error: the symplectic orbit precesses. Use a fixed step in games, molecular dynamics and N-body; do not expect help for viscous fluids.
  • Leapfrog’s parasitic root has modulus exactly 11 on oscillations (s≤1s \le 1) and is unstable on decay: stable for waves only.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: