Energy error (source code)

= Energy error

For a computed state $z_n$ and an exact state $z(t_n)$, an energy error is $H(z_n)-H(z(t_n))$ for a specified <energy> <function> $H$. If the exact dynamics conserve $H$, the reference is simply $H(z(0))$. This is a scalar diagnostic; it does not by itself bound <position> or phase errors.

For the unit-frequency <harmonic oscillator> $q'=p$, $p'=-q$, the exact <energy> is $H=(q^2+p^2)/2$. The <Forward Euler method> gives
$$
q_{n+1}=q_n+hp_n,\qquad p_{n+1}=p_n-hq_n,\qquad H_{n+1}=(1+h^2)H_n.
$$
Thus a fixed positive step produces systematic <energy> growth even though the exact oscillator has constant <energy>. A bounded oscillatory energy error has a different long-time meaning from such cumulative drift. Comparing methods also requires the same step size, reference trajectory and observation interval.