Task 5 of 6
Velocity Verlet reads like Euler with one extra term. Move the position with a quadratic instead of a straight line, then update the velocity using the average of the accelerations at the two ends of the step:
x ← x + v·dt + ½·a·dt²
v ← v + ½·(a + a_new)·dt where a_new = −x_new
It is second order — the same exponent midpoint gave you, so the instrument you have
been using cannot tell them apart by exponent alone. (It is about four times
more accurate than midpoint at the same step size, and a real implementation carries
a_new into the next step as its a, so it costs one force
evaluation per step against midpoint's two. Here a = −x is free, so the code
below just recomputes it.)
What it has that midpoint does not is invisible in a single step and decisive over a
million. The Verlet step is an exactly area-preserving map of the (x, v)
plane — symplectic — and for this oscillator it conserves
(1 − dt²/4)·x² + v² exactly, forever, in a way no accumulation of steps can
erode. Measure the order here. The next task is where that sentence starts to matter.
x += v·dt + 0.5·a·dt·dt where a = −x at the start of the stepaNext = −xv += 0.5·(a + aNext)·dta is captured before x moves; aNext is
read after. The velocity update needs both, so it has to come last.
0.5 * a * dt * dt is the constant-acceleration formula from
first-year mechanics. Dropping it leaves a scheme that still looks plausible and is
back to first order.
const a = -x;
x = x + v * dt + 0.5 * a * dt * dt;
const aNext = -x;
v = v + 0.5 * (a + aNext) * dt;This page is an interactive exercise — the editor, the GPU runner and your saved progress need JavaScript. The text above is the full brief.