How two numerical steppers keep or lose energy
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 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.