Fundamentals / Numerical mathematics / Numerical ordinary differential equations

A constrained integrator can get the circle right and the clock wrong

A frictionless bead moves on an ideal circular guide. Its exact path, speed and angular phase can all be calculated by hand. Use this unusually informative reference to derive RATTLE's two constraint corrections and see which numerical errors would escape a camera that measured position alone.

Recover the force exerted by a perfectly smooth guide

Use a circle of radius one metre, and measure mass in units of the bead mass. The position \(q=(x,y)\) and velocity \(v\) are expressed in Cartesian coordinates. The guide exerts a reaction normal to its surface and no tangential force. Writing this reaction per unit mass as a scalar multiplier times \(q\) gives

\[\dot q=v,\]
\[\dot v=-\lambda q.\]

The sign convention makes positive \(\lambda\) point inward. The equation \(q\cdot q=1\) fixes the position but does not by itself describe which velocities are physically admissible.

Differentiate the constraint once to find

\[q\cdot v=0.\]

Differentiate again to obtain

\[v\cdot v+q\cdot\dot v=0.\]

Substituting the force yields

\[\lambda=(v\cdot v)/(q\cdot q).\]

Thus the required inward acceleration grows as the square of speed. The power supplied by the guide is \(-\lambda q\cdot v=0\), so the speed stays constant. The angular momentum per unit mass \(xv_y-yv_x\) also stays constant because the guide force passes through the centre. These are independent diagnostics one can compute directly from a numerical trajectory.

Start at \(q=(1,0)\) with \(v=(0,\omega)\). The exact position is \((\cos\omega t,\sin\omega t)\), the exact velocity is its derivative, and the exact angular momentum is \(\omega\) in the unit-radius convention. An angle coordinate would make this example elementary. The point of the exercise is to keep Cartesian variables, as would be necessary for many connected bodies whose constraints cannot conveniently be eliminated in one global chart.

Solve for the first constraint impulse

Think of a velocity-Verlet step as an initial impulse, a drift, and a final impulse. With no external force the initial impulse is parallel to the old normal \(q_n\). Absorb its unknown magnitude and factors of time into a dimensionless coefficient \(a\). After the drift the candidate position must have the form \(q_{n+1}=a q_n+h v_n\). Since \(q_n\) has unit length and \(v_n\) is tangent, the norm of this expression has no cross term. Imposing the new position constraint gives a scalar quadratic equation and two possible roots.

\[a^2+h^2\|v_n\|^2=1,\]
\[a=\sqrt{1-h^2\|v_n\|^2}.\]

Choose the positive solution because the computed point must approach the old one as \(h\) goes to zero. The negative root approaches the opposite point on the circle. Its algebraic admissibility is not evidence that it represents the same initial-value problem. When \(h\|v_n\|>1\) the construction has no real solution at all. This is a geometric limit on the step, distinct from an estimate of integration accuracy. Even before reaching the limit the step can introduce a substantial angular error.

The velocity used during the drift is the chord velocity \(v_{n+1/2}=(q_{n+1}-q_n)/h\). It contains a small inward term \((a-1)q_n/h\). Expanding the square root yields

\[a=1-h^2\|v_n\|^2/2+O(h^4).\]

This shows how a half-step of centripetal acceleration emerges from the constraint impulse. There was no need to insert the exact circular trajectory to obtain it.

Recover a tangent velocity at the new point

Although both ends of a chord lie on the guide, a chord velocity is generally not tangent at its destination. Apply the second normal impulse along \(q_{n+1}\). To eliminate the normal component subtract its scalar projection, using the fact that the new normal again has unit length. This gives the velocity form of the second constraint correction.

\[v_{n+1}=v_{n+1/2}-(q_{n+1}\cdot v_{n+1/2})q_{n+1}.\]

The two impulses preserve angular momentum about the origin. The old-normal impulse makes no torque about the old position. The straight drift preserves the cross product of chord velocity with position because adding a parallel displacement does not change that cross product. The final impulse makes no torque about the new position. In this special problem tangency, unit radius and preserved angular momentum together also preserve speed. For a more general constrained Hamiltonian system, exact preservation of kinetic energy does not follow from the same argument.

Obtain the phase change directly from the new position. Its component along the old unit tangent is \(h\omega\); its component along the old radius is \(a\). For the branch chosen here the step advances the phase by \(\arcsin(h\omega)\). The exact time increment advances by \(h\omega\). Their difference is \(h^3\omega^3/6+O(h^5)\) for a small step. After \(T/h\) steps at fixed final time the total leading phase error is \(T h^2\omega^3/6\). This is a precise second-order error prediction for an observable that energy and radius cannot diagnose.

A comparison with a plausible geometric shortcut

Another algorithm proposes an unconstrained drift \(q_n+h v_n\), rescales this position to the circle and then projects the old velocity onto the new tangent. Under this rule the angle change per step is \(\arctan(h\|v_n\|)\) and the tangential speed changes by a factor \(1/\sqrt{1+h^2\|v_n\|^2}\). One finds the first expression by comparing the radial and tangential components before normalisation. The second follows by taking the dot product of the two successive unit tangents.

Thus this alternative delivers perfect radii and tangent velocities while steadily slowing the bead in a frictionless apparatus. Taking reciprocals of the squared-speed relation even gives

\[1/s_{n+1}^2=1/s_n^2+h^2.\]

After many steps its speed can be predicted without running a single time loop. This is a useful second independent reference for the notebook. A method that projects state components separately need not realise the constraint force and its work correctly.

Computed positions from both methods fall on a circle, while an angle-error plot shows RATTLE running slightly ahead and the separately normalised drift falling behind.Open full-size diagram
Before judging a circular trajectory, choose an observation time. On the circle plot a student can miss the phase displacement; the second panel reads it in radians at the same time.

With the default \(\omega=1.6\,\mathrm{s}^{-1}\) and \(h=0.25\) s, the product \(h\omega\) is 0.4. The constrained step therefore advances by 0.411517 radians instead of 0.4. After 80 steps its lead is about 0.92135 radians. The analytical speed, radius and angular momentum are still reproduced to roundoff. This example makes the choice of reported output a scientific decision. A machine timed by successive crossings of a sensor would give a wrong timing signal even though a ruler would see the correct orbit radius.

Check an order of accuracy without changing the time horizon

Double the number of steps while halving their duration to retain the same final time. For the default run the lead drops from about 0.92135 to about 0.21727 radians. The quotient is a little more than four because the larger step is still influenced by higher powers of \(h\omega\). Keep refining until the quotient settles near four. If instead the step were halved with the number of steps fixed, the run would end earlier and both the elapsed time and the error per step would change. Such a comparison cannot isolate the global order at fixed final time.

An angle reported by an inverse tangent jumps from positive to negative pi across its branch cut. Differentiate or subtract such wrapped angles without care and the resulting spurious jumps can dominate a phase-error graph. Here each step moves through less than a quarter circle, so consecutive angle differences are unambiguous under phase unwrapping. Sampling a more rapidly moving system less often could reintroduce an ambiguity even with an accurate integrator internally.

A local OneSimulation installation permits another independent comparison. The optional notebook cell supplies its classical four-stage Runge–Kutta method with the continuous Cartesian acceleration obtained above. This method has no algebraic position correction; comparing its end radius and angular timing helps to distinguish the order of a differential equation solver from the constraint properties of the discrete update. For a distant endpoint one should test each diagnostic over the whole interval and repeat the comparison at a smaller step.

An additional time-reversal test can be made without knowing the exact phase. Integrate forward for a chosen number of steps, negate the final velocity and integrate the same number of positive steps. For the exact constrained update in this free problem, each second step sequence undoes the corresponding first rotation on the same branch. The final position returns to its initial value and the terminal velocity is the negative initial one. A return-to-origin test can therefore pass even when the intermediate phase differs visibly from the true physical clock. This is why a reversibility check complements, rather than replaces, a measurement at a specified observation time.

Tutorial on enforcing a shape and measuring a time

1. Start at unit radius with tangential speed 1 m/s. Use a half-second constrained step to calculate the new position and the discrete phase advance.

Work through the solution

In the chosen coordinates the new point is \((\sqrt{3}/2,1/2)\). Its polar angle is \(\pi/6=0.52360\) radians. The exact solution at half a second has angle 0.5 radians. A single step is already ahead by approximately 0.02360 radians.

2. For the same initial data and step, find the speed after four separately normalised drifts. What would a kinetic-energy sensor read?

Work through the solution

The reciprocal squared speed starts at one and increases by 0.25 per step. After four steps the speed is \(1/\sqrt2\) m/s. With unit mass the reported energy is 0.25 J instead of the original 0.5 J. All four reported positions still lie on the circle.

3. A student proposes accepting a step whenever its radius error is below \(10^{-10}\) m. Supply a quantitative additional criterion for a phase error below 0.01 radians over ten seconds at \(\omega=1\).

Work through the solution

The leading estimate gives \(10h^2/6<0.01\), hence \(h<0.0775\) s approximately. Use the exact error \(10[\arcsin(h)-h]/h\) or a refinement comparison for the final check; the asymptotic bound alone is not a rigorous guarantee at its edge. Tangency and torque balance remain useful complementary diagnostics.

Further reading · Hans Andersen, the RATTLE velocity algorithm for constrained motion

Change parameters · A constrained integrator can get the circle right and the clock wrong

Change one input at a time and compare the recalculated curves with the argument above. The browser evaluates this small model locally; the accompanying numerical values state its assumptions and reference checks. Reset restores the reference inputs, and the data can be downloaded as CSV.