Research / chaos and numerics

How long a simulated double pendulum stays right

Work with Luke Wang, begun as our Math 165 final project. A student who animates a double pendulum for a minute has no way of knowing whether the last forty seconds mean anything. Integrating in arbitrary precision, so that the step $h$ and the number of significand bits $p$ are both knobs, turns that worry into a measurement. The time a simulation stays correct follows one formula, and the only property of the pendulum in it is the Lyapunov exponent.

The question

The double pendulum is the standard classroom example of chaos, and the standard lesson is that two nearby starting states separate exponentially. The simulation is subject to the same amplification, with one difference. Its error is not confined to the initial condition. A small error is committed at every step, by truncating the Taylor series and by rounding each arithmetic result to finitely many bits, and every one of those errors is amplified from the moment it is made.

Two point masses hang in series on massless rods of length $l$. With angles measured from the downward vertical and time in units of $\sqrt{l/g}$, the equations carry no parameters at all:

$$\ddot\theta_1 = \frac{\omega_1^2 \sin\Delta\cos\Delta + \sin\theta_2\cos\Delta + \omega_2^2\sin\Delta - 2\sin\theta_1}{2 - \cos^2\Delta},$$ $$\ddot\theta_2 = \frac{-\omega_2^2 \sin\Delta\cos\Delta + 2\left(\sin\theta_1\cos\Delta - \omega_1^2\sin\Delta - \sin\theta_2\right)}{2 - \cos^2\Delta},$$

where $\Delta = \theta_2 - \theta_1$. Every run starts from rest with both rods horizontal, $\theta_1 = \theta_2 = \pi/2$, which has energy $0$. One natural time unit is $0.319$ s for metre-long rods.

Two choices make the experiment work. The step is held fixed, because an adaptive integrator tunes $h$ to hold its own error estimate near a tolerance and so hides the dependence we want to expose. And every arithmetic operation is rounded to $p$ bits by MPFR, with $p$ set by the program rather than by the hardware, so that truncation error and roundoff error can be varied one at a time. Steps are powers of two, so that lowering $p$ changes the arithmetic and nothing else.

Watching it fail

Three pendulums released from the same state, integrated by the same method, differing only in the step. They agree to within the width of a line for a long time, and then the coarsest one leaves, followed later by the next. The panel on the right is the distance between each bob and the reference bob, on a log scale, drawn as it happens.

vary speed

Live, in your browser, integrated with the same fourth-order Runge–Kutta method and the same starting state as the paper. In step mode the runs differ only in $h$, and they separate at $t \approx 57$ and $t \approx 85$, matching the measured horizons of $56.6$ and $84.9$. The precision mode is a demonstration rather than a reproduction: the browser has only double precision, so it rounds the state to $p$ bits after each step instead of rounding every operation, which injects an error of the same size at the same rate but is not the published measurement.

One formula

Write the error committed in a single step as a local truncation term of size $O(h^5)$ plus a rounding term of size $O(2^{-p})$. Unrolling the recursion expresses the error at time $t$ as a sum over all earlier steps, each carried forward by the intervening Jacobians, which grow on average as $e^{\lambda(t-t_j)}$. The sum is a geometric series dominated by its earliest terms, so

$$e(t) \approx \left(A h^4 + B\,2^{-p} h^{-\gamma}\right)e^{\lambda t}.$$

The first term collects $1/h$ truncation errors of size $h^5$ per unit time. The second collects $1/h$ rounding errors of size $2^{-p}$, which add like a random walk if their signs are independent, giving $\gamma = \tfrac12$, and linearly if not, giving $\gamma = 1$. The measured value is $\gamma \approx 0.6$, between the two.

Define the horizon $T$ as the first time the computed lower bob is a distance $10^{-2}$ from the reference one, which is roughly when two superimposed animations become visibly different. Setting $e(T) = \epsilon$ gives

$$T = \frac{1}{\lambda}\ln\frac{\epsilon}{A h^4 + B\,2^{-p}h^{-\gamma}}.$$

Everything follows from that. When truncation dominates and $h = 2^{-k}$,

$$T = T_A + \frac{4\ln 2}{\lambda}\,k,$$

so each halving of the step buys a fixed extra amount of accurate time, the $4$ being the order of the method. When roundoff dominates,

$$T = T_B + \frac{\ln 2}{\lambda}\left(p - \gamma k\right),$$

so each extra bit buys a fixed amount, and halving the step now costs. Both slopes are properties of the pendulum alone. That is what lets the numerical error measure $\lambda$.

Notice what the logarithm does. The horizon depends only logarithmically on the error per step, so enormous improvements in the integrator buy modest extensions of the accurate time, and the work needed grows exponentially with $T$.

0255075100125150-20-15-10-50time t √(g/l)log₁₀ (d / l)separation saturates at 4ltolerance, d = 10⁻²h = 2⁻6h = 2⁻8h = 2⁻10h = 2⁻12h = 2⁻14h = 2⁻16
Separation of the lower bob from the reference run, for six steps, all in 237-bit arithmetic so that truncation is the only error. Each curve rises through many orders of magnitude at the same average rate, then saturates near $4l$ once the two runs have nothing to do with each other. The curves are evenly spaced, which is the fixed $4\ln 2/\lambda$ per halving.

The error is a perturbation

The curves are evenly spaced for a reason. Divide each by $h^4$ and they fall on top of one another, to within $0.21$ decades for every $h \le 2^{-8}$ wherever none has saturated, and the error at $t = 10$ scales as $h^{4.000}$. What they collapse onto is the amplification factor of the trajectory itself.

The test of that reading is to produce the same curve a different way. Perturb $\theta_1(0)$ by $2^{-170}$, small enough to stay linear for the whole run, and plot how the two trajectories separate. It follows the collapsed error curves through every plateau and every burst. The error of the integrator grows exactly as a perturbation of the initial condition does, because that is what it is.

The growth is exponential only on average. The local rate fluctuates as the pendulum passes through more and less unstable parts of its motion, and those fluctuations are as much a part of the lesson as the mean.

025507510012515005101520time t √(g/l)log₁₀ (d / l h⁴)thick line: a deliberate perturbation of 2⁻¹⁷⁰
The same curves multiplied by $h^{-4}$. The thick line is not an error at all: it is the separation of two runs whose initial conditions differ by $2^{-170}$, shifted vertically. Truncation error and a deliberate perturbation grow the same way.

Measuring the exponent

Both slopes give $\lambda$, and they are measured on entirely different axes. Fitting the horizon against $k$ over $7 \le k \le 16$ gives $7.87 \pm 0.38$ time units per halving, and $\lambda = 4\ln 2 / \text{slope} = 0.35 \pm 0.02$. Fitting against $p$ at fixed step over $24 \le p \le 80$ gives $1.99 \pm 0.06$ time units per bit, and $\lambda = \ln 2 / \text{slope} = 0.35 \pm 0.01$.

The scatter about the step line is not noise. It is the fluctuating local growth rate again: a horizon that falls just before a burst of growth gains little from the next halving, and one that falls just after gains a great deal.

4816243248648096050100150200k = log₂(1/h) and significand bits phorizon T √(g/l)step sweep7.9 per halvingprecision sweep1.99 per bit
The horizon against the step exponent $k$, and against the significand width $p$ at fixed step. The step sweep is the short steep series on the left, the precision sweep the long one running across. Two independent experiments, two straight lines, and the same $\lambda$ out of both.
Method$\lambda$ in units of $\sqrt{g/l}$
Two-trajectory method, $2\times10^4$ time units$0.299$
Growth of a perturbation of $2^{-170}$$0.33$
Horizon against step$0.35 \pm 0.02$
Horizon against precision$0.35 \pm 0.01$

The last three sample the growth rate over the first 160 time units only, within which it averages $0.28$ in the first half and $0.33$ in the second. The first is the long-time value. The numerical measurements land within 20% of it, which is what you should expect from a quantity that has not finished converging.

One failure is worth keeping. With $p = 11$ and $h = 2^{-12}$ the pendulum never moves at all. The first increment to $\theta_1 = \pi/2$ is smaller than half a unit in the last of eleven bits, the sum rounds back to $\pi/2$, and the computed pendulum stays horizontal forever. With the same eleven bits and $h = 2^{-8}$ it falls. A smaller step has not reduced the error, it has made it total.

Energy proves nothing

The check most people reach for is conservation of energy. It does not settle the question. Below, the energy drift and the trajectory error are on the same axes, for the same run. The energy is still good to a part in $10^{10}$ at a time when the trajectory has been meaningless for fifty time units.

0255075100125150-16-12-8-40time t √(g/l)log₁₀ of energy drift and of separationtrajectory is wrong from hereenergy drift stays near 10⁻¹¹
Double precision, $h = 2^{-10}$. Energy drift stays near $4\times10^{-11}$ for the whole run. The trajectory crosses the tolerance at $t \approx 92$ and saturates soon after. A conserved quantity constrains the trajectory to a surface. It says nothing about where on that surface the pendulum is.

The other common check fares no better. Halving the step and seeing no visible change tests only the interval over which nothing has yet gone wrong.

The double-precision ceiling

For a given floating-point format the two error terms compete, and there is a best step. Below it, truncation dominates and halving helps. Above it, roundoff dominates and halving hurts. In double precision the optimum for this pendulum is $h = 2^{-11}$, and the horizon there is $100.9$ natural time units, about $32$ seconds for metre-long rods. No choice of step does better. Buying more accurate time past that point means changing the arithmetic, and each extra bit buys only $2$ time units.

None of the underlying facts is new, and the paper credits the research literature that established them for other systems. Lorenz found sensitive dependence by re-entering rounded numbers; the competition between truncation and roundoff is standard numerical analysis; the reliability of computed chaotic solutions has been studied with multiple precision. What seems not to exist is a treatment at undergraduate level where the student separates the two sources of error, measures each, and connects both to the Lyapunov exponent. The exercise needs about forty lines of code, and it turns numerical error from a nuisance into a measuring instrument.