How two numerical steppers keep or lose energy

Physics and motion

One planet stepped through fifty four periods at a coarse step. Velocity Verlet, the heavier line, turns once about the centre as a rosette and holds its size. Runge Kutta, the fine line, winds inward and is thrown clear. Cobalt is the exact ellipse.

The state is a unit harmonic oscillator, the small-angle stand-in for a pendulum. Symplectic Euler and explicit Euler take the same rational step. Energy is the exact rational (q squared plus p squared) over 2. The drift of each stepper is the certificate.

Below it, the full pendulum swings to a large angle. Velocity Verlet and fourth-order Runge-Kutta take the same step, energy drift is again the certificate, and the exact period from the arithmetic-geometric mean is the reference for the period each one measures. Nothing here controls a machine.

Last, the two-body problem reduced to one body circling a fixed centre under an inverse-square pull, the Kepler problem. The same two integrators start it at pericentre. Energy and angular momentum are its certificates, and Kepler's third law, T = 2 pi a^(3/2) with a from the starting energy, is the reference for the period each one measures.

The default run in the phase plane: the arc of starting energy, filled dots for symplectic Euler, open dots for explicit Euler.
The default run, four steps of 1/5 from position 1 and momentum 0, drawn in the phase plane with position across and momentum downward. The arc is the starting energy, 1/2. Filled dots are symplectic Euler and stay near the arc; open dots on the dashed line are explicit Euler and move outward as its energy grows.

Position and momentum are integers from -8 to 8, not both zero. The step is a positive rational at most 1/2. Steps run from 1 to 40.

The full pendulum starts at rest at 1 to 175 degrees. Its step is 1 to 50 hundredths and its span is 10 to 1000, both in time units of the square root of L over g.

The orbit's eccentricity is 0 to 90 hundredths, so 0 to 0.9. Its step is 1 to 200 thousandths of a time unit, and it runs for 1 to 100 periods of Kepler's law.

The result appears here when the page script runs.

The full pendulum

The large-angle result appears here when the page script runs.

The Kepler orbit

The orbit result appears here when the page script runs.

How it is computed

Method

Acceleration is minus position. Symplectic Euler updates momentum first, then position from the new momentum. Explicit Euler updates both from the old state. Energy is half the sum of the squares. Drift is energy minus the starting energy, reduced.

The full pendulum obeys theta double dot equals minus g over L times sin theta, with time in units of the square root of L over g so that g over L is 1. Velocity Verlet takes a half kick, a drift and a half kick, one new force per step. Runge-Kutta 4 averages four slopes, four forces per step. Energy is half omega squared plus 2 sin squared of half theta, which is 1 minus cos theta without the cancellation at small angles. The chart keeps the lowest and highest relative error in each of 120 time slices, so a fast oscillation shows as a band.

The reference period is 4K(k) with k the sine of half the amplitude, and K(k) is pi over twice the arithmetic-geometric mean of 1 and the cosine of half the amplitude. The measured period comes from the zero crossings of theta, each placed by linear interpolation between two steps: twice the time from the first crossing to the last, over the number of gaps. Fewer than two crossings measures nothing.

The orbit obeys r double dot equals minus GM r over r cubed. GM is 1, lengths are in units of the semi-major axis a and time in units of the square root of a cubed over GM, so one orbit takes 2 pi. It starts at pericentre, at 1 minus e on the x axis, moving across it at the square root of (1 plus e) over (1 minus e). Energy is half v squared minus 1 over r, and angular momentum is x times vy minus y times vx. Each kick of velocity Verlet points along the position it is taken at, so neither kick nor the drift changes angular momentum, and Verlet keeps it to rounding. Runge-Kutta 4 mixes slopes taken at different positions and lets it drift.

The reference period is 2 pi a^(3/2), with a equal to minus 1 over twice the starting energy. The measured period comes from the angle the position sweeps, unwrapped step by step: each crossing of a whole turn is placed by linear interpolation, and the period is the time of the last crossing over the number of turns. A pericentre is the step where the radial rate r dot v turns from inward to outward, placed the same way. Its angle less the whole turns, over the number of passages, is the shift per orbit, positive when it turns with the orbit. Below an eccentricity of 0.10 no shift is measured.

Assumptions

The step is at most one half so the explicit stepper's growth stays visible inside the step cap. The state is exact at every row. No sine is evaluated in the linearised view.

The full pendulum starts at rest, has no friction and no drive, and runs in double-precision floating point, at most 100,000 steps.

The orbit runs in double precision for the whole number of steps that covers the chosen number of Kepler periods, at most 628,319 steps. A coarse step at a high eccentricity can throw the body out of the field at pericentre, and a note under the table then says so.

Limits

The exact fractions cover the linearised pendulum. The full pendulum is rounded at every step, and its reference period is exact up to rounding. One pendulum is modelled. The orbit is a point mass in an ideal inverse-square field about a fixed centre, the two-body problem reduced to the motion of one body relative to the other. Nothing is sent to a device.

Reproducibility

The inputs are kept in the address bar, so the same link reruns the same calculation.