Skip to content
Tutorials Center
My learning

Topics

Lesson 14 of 47 Β· Ordinary Differential Equations

Oscillators and Energy: Leapfrog and Verlet

Learn why Euler and RK4 drift in energy on oscillators and how symplectic methods such as leapfrog and velocity Verlet keep energy bounded, with a pendulum period test.

A frictionless oscillator conserves its energy forever. A numerical simulation of it should not gain or lose energy by itself. Yet Euler's method pumps energy in, and even RK4 slowly leaks it out. For planets orbiting for billions of years, atoms vibrating for millions of steps, or any long run where energy matters, you need methods built to respect the structure of mechanics. The simplest are leapfrog and velocity Verlet.

Left: energy against time for Euler and leapfrog. Right: phase-space plot where Euler spirals outward and leapfrog stays on an ellipse
Same oscillator, 400 steps of 0.1: explicit Euler climbs in energy and spirals out, while leapfrog stays flat on a closed curve.

What you will learn

  • Why energy drift matters and how to measure it.
  • The semi-implicit (symplectic) Euler, leapfrog and velocity Verlet updates.
  • What "symplectic" buys you in practice.
  • How to test a pendulum code against the exact period.

Key terms

Hamiltonian H: the total energy as a function of position and momentum.

Energy drift: a steady growth or decay of the computed energy.

Symplectic method: one that preserves the geometric structure of Hamiltonian motion; its energy error stays bounded and oscillates.

Time-reversible: running the method backward retraces the path (up to rounding).

Newton's law in the form xβ€³ = a(x)

Many physical systems have forces that depend only on position, so xβ€³ = a(x) with a = F/m. For the harmonic oscillator a(x) = βˆ’Ο‰Β² x, for a pendulum a(ΞΈ) = βˆ’(g/L) sin ΞΈ, for gravity a = βˆ’G M x / |x|Β³. The energy E = Β½ vΒ² + U(x) is conserved when a = βˆ’dU/dx.

Three simple symplectic updates

Semi-implicit (symplectic) Euler. Update the velocity first, then use the new velocity to update the position:

v ← v + h a(x) x ← x + h v

It is one line different from explicit Euler, still first order, and its energy error stays bounded rather than growing.

Leapfrog (kick-drift-kick). A half step for velocity, a full step for position, and another half step for velocity:

v ← v + (h/2) a(x) x ← x + h v v ← v + (h/2) a(x) (with the new x)

Velocity Verlet is the same thing written so that one force evaluation is reused: after the position update, the new acceleration is computed once and serves both for the second half-kick and for the first half-kick of the next step. Leapfrog and velocity Verlet produce identical trajectories; they differ only in bookkeeping. Both are second order, time-reversible and symplectic.

Python Energy drift of four methods

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Explicit Euler's energy is multiplied by 1.01 every step, so after 1000 steps it is about 21,000 times too large. RK4 loses a very small amount per step, which would matter in a much longer run. The two symplectic methods oscillate within a narrow band around the true value and never drift.

Python Phase space and energy plot

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why symplectic methods behave

A symplectic method does not conserve the true energy exactly, but it conserves a slightly modified energy H̃ = H + O(h²) almost exactly (a result from "backward error analysis"). The numerical trajectory is the exact trajectory of a nearby system, so the computed energy oscillates around a fixed value instead of walking away. This is why molecular dynamics (chapter 6) and planetary dynamics use Verlet-type methods almost exclusively. Two cautions apply: a fixed step size is needed (changing h on the fly breaks the guarantee), and the force must depend only on position.

A nonlinear test: the pendulum

For a pendulum with a(ΞΈ) = βˆ’sin ΞΈ (units where g/L = 1), the small-angle period is Tβ‚€ = 2Ο€. For amplitude ΞΈβ‚€ the exact period is 4 K(m) with m = sinΒ²(ΞΈβ‚€/2), a complete elliptic integral. We can measure the period of the simulation by finding when ΞΈ crosses zero going upward, and compare.

Python Pendulum period from leapfrog vs the exact value

Press Run (or Ctrl + Enter) to see the result here.

Program input Β· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common misconceptions

"Symplectic means exact energy." The energy error stays bounded and small, but it is not zero; it oscillates.

"Higher order always conserves energy better." RK4 is more accurate over short times but drifts over long times; a second-order symplectic method wins over millions of steps.

"Leapfrog and Verlet are different algorithms." In their standard forms they give the same positions; the difference is how the velocity is stored.

Check your understanding

Quick check Β· 5 questions

of 5 answered You scored / 5 Β· perfect!

  1. 1. What is the only change that turns explicit Euler into semi-implicit Euler?

    Using the freshly updated velocity in the position update gives a symplectic method.
  2. 2. Explicit Euler multiplies the energy of a harmonic oscillator each step by

    That factor is larger than 1, so the energy always grows.
  3. 3. Which is true of leapfrog?

    Those are its three key properties.
  4. 4. Why do molecular dynamics codes prefer Verlet-type methods over RK4?

    Bounded energy error matters more than short-term accuracy in very long runs.
  5. 5. A weakness of symplectic integrators is that

    Varying the step size on the fly destroys the near-conservation property.

Summary

  • Euler gains energy on oscillators; RK4 slowly loses it; symplectic methods keep it bounded.
  • Semi-implicit Euler, leapfrog and velocity Verlet differ only in details; leapfrog and Verlet are second order and time-reversible.
  • Backward error analysis explains the good long-term behavior: a nearby energy is conserved.
  • Test a pendulum code against the elliptic-integral period.

Next: Projectile Motion with Drag