Fundamentals / Numerical mathematics / Numerical ordinary differential equations / Dynamics and numerical time integration

Find a crossing between accepted time steps

An integrator can approximate a trajectory accurately yet report a poor switch time. Construct a continuous interpolant, relate a state error to an event-time error and inspect what happens when a trajectory merely touches a threshold.

An event is a root of a composite function

Let \(y'=f(t,y)\) and let a sensor or change of dynamics be described by \(g(t,y)=0\). Along an exact trajectory the scalar event function is \(h(t)=g(t,y(t))\). A simple event at \(t_*\) satisfies

\[\dot h_*=g_t+g_yf\ne0.\]

If approximate trajectory or sensor evaluation introduces error \(\delta h\), linearisation around the crossing gives

\[\delta t\simeq-\delta h/\dot h_*.\]

A small state error can therefore imply a much larger time error if the crossing is nearly tangential. Reporting an event timestamp to machine precision after bisecting a low-order interpolant is not equivalent to knowing the physical crossing time that accurately.

Consider a dimensionless unit-frequency oscillator with \(y=\cos t,\ y'=-\sin t\) during the first half cycle. Its first falling crossing of threshold \(a\in(0,1)\) occurs at \(t_*=\arccos a\), with slope \(-\sqrt{1-a^2}\). At \(a=0.5\), a positive vertical interpolation error \(10^{-3}\) can delay the crossing by about \(0.00115\) time units. At \(a=0.9999\) the same linear estimate is about 0.0707, comparable with or larger than the event time itself; the simple linear local approximation then also fails. At precisely \(a=1\) the surface is tangent at the maximum and sign-change tests can miss it altogether.

Construct a state and slope interpolant on one accepted interval

Suppose samples of state and derivative are available at \(t_n,t_{n+1}=t_n+h\). Write \(s=(t-t_n)/h\) and choose a cubic \(p(s)\) matching \(p(0)=y_n,p(1)=y_{n+1},p_s(0)=hf_n,p_s(1)=hf_{n+1}\). Solving these four linear conditions gives

\[p=(2s^3-3s^2+1)y_n+(s^3-2s^2+s)hf_n+(-2s^3+3s^2)y_{n+1}+(s^3-s^2)hf_{n+1}.\]

For an exact scalar trajectory sufficiently smooth in time, the Hermite error is \(y(t)-p(t)=y^{(4)}(\xi)(t-t_n)^2(t-t_{n+1})^2/4!\), bounded by \(h^4\max|y^{(4)}|/384\). Linear interpolation instead has error bounded by \(h^2\max|y''|/8\). Combine either bound with a known lower bound on \(|\dot h|\) in a bracket to bound event-time error. In an actual ODE solve, \(y_n,f_n\) are numerical, so interpolation and integrated state errors both contribute. A high-order interpolant cannot repair incorrect endpoint data or sample a discontinuity assumed smooth.

Bracket on the physical trajectory and restart after a transition

At each accepted interval compare event values at both ends. For a simple crossing of the requested direction, solve \(g(t,p(t))=0\) inside that interval using a bracketed root finder. The bracket controls the algebraic root-finding error; independent time-step refinement measures trajectory error. If the dynamics change at the event, truncate the old step there, apply any specified reset \(y^+=R(t_*,y^-)\) and restart the integrator under the new right side. Propagating the old smooth interpolant through a change of dynamics can corrupt both state and subsequent event ordering.

If a surface stays exactly active over a finite interval, repeated detection at the same time can stall the integration. A model of contact, hysteresis or directional rearming may be physically appropriate, depending on what the event means. Merely nudging the time forward by an arbitrary tolerance can skip a nearby second event. Similarly two opposite crossings within one large accepted interval may yield equal endpoint signs. A maximum step guided by known physical timescales, supplementary extrema detection or an analytically enclosed event function can be needed. Accuracy control on the smooth state alone does not guarantee detection of every specified surface crossing.

Compare interpolation errors independently of the propagation error

The calculator uses exact cosine endpoint samples and their derivatives, then locates the first falling crossing on a uniform time grid by linear or cubic Hermite interpolation. The curves show error against step spacing on a logarithmic vertical scale. Their irregular minima occur when a grid point happens to coincide with the event. Look for the error envelope over several spacings. At threshold 0.5 the exact result is 1.047198 s. Moving the threshold changes both the crossing slope and its position relative to the sampled grid.

Distinguish uncertainty in the threshold from time discretisation

Two clocks can agree on an event and still report a poorly determined arrival time if the physical threshold is uncertain. For a calibrated level \(a\) the falling crossing solves \(\cos t_*=a\), so implicit differentiation gives

\[dt_*/da=-1/\sqrt{1-a^2}.\]

A level error of \(0.002\) near \(a=0.5\) produces a timing uncertainty of about \(0.00231\) s for the unit-frequency benchmark. At \(a=0.9\) the same device gives \(0.00459\) s. Close to the turning point the trace is almost horizontal. Reducing the root finder's tolerance cannot remove that physical loss in sensitivity.

If the numerical trajectory also has a known absolute state error bound \(\eta\), divide that bound by a lower bound on the crossing speed in the bracket. The result is a deterministic timing allowance. An independent random threshold error has a standard deviation and calls for a statistical interpretation. Adding those numbers in quadrature would need a probability model for both. Reporting the two separately preserves what the measurement and the solver actually established.

Tutorial · assess a reported event time

Get a crossing from just two samples

Use \(y=\cos t,\ a=0.5\) with samples at \(t=0.8,1.2\). Compute the linearly interpolated time and its error. Why does its sign follow from curvature?

Work through the solution

The endpoint values are 0.696707 and 0.362358; the fractional position is \((0.696707-0.5)/(0.696707-0.362358)=0.5883\), giving \(t_L\simeq1.0353\). It is about 0.0119 s early. Over this interval \(\cos t>0\), so \(y''=-\cos t<0\). A straight chord lies below the concave curve; the falling chord reaches the threshold before the true trajectory.

Bound a Hermite crossing-time error

On the same interval use \(|y^{(4)}|\le1,\ |\dot y|\ge\sin0.8\). Give a simple guaranteed bound assuming exact endpoints and exact solution of the interpolating cubic root.

Work through the solution

The state interpolation bound is

\[0.4^4/384=6.67\times10^{-5}.\]

Dividing by \(\sin0.8=0.7174\) gives \(9.29\times10^{-5}\) s. This uses monotonicity of the exact trajectory to convert a vertical defect at the computed root into a horizontal difference. It does not assume the cubic derivative equals the true derivative. Numerical endpoint error would need to be added in state units first.

Distinguish a tangency from a missed sign change

A gap sensor follows \(h(t)=(t-1)^2-\delta\). Compare event detection at \(\delta=0,10^{-4},-10^{-4}\) over the interval \([0.9,1.1]\).

Work through the solution

Both endpoints are positive in all three cases. At zero there is a repeated touching event. At positive \(\delta\) there are two true crossings at 0.99 and 1.01 with opposite directions. At negative \(\delta\) there is no root. Locating and evaluating the internal minimum distinguishes the cases when the uncertainty is below \(|\delta|\). A sign test at just the endpoints cannot determine any of these distinctions, however accurately it evaluates those endpoints.

Further reading