Fundamentals / Numerical mathematics / Numerical ordinary differential equations

Adaptive steps and event locations

Estimate local integration error with a low–high order pair, choose a dimensionally consistent tolerance and bracket a zero of an event function in time.

From first principles

Obtain a step controller from the measured error of two approximations

Take a proposed step \(h\) twice from the same current state, once using an order-\(p\) formula and once using a more accurate formula whose leading defect is one order smaller. For smooth data and sufficiently short steps the two values have a common exact target \(y_*\). Their expansion gives

\[y_{\rm low}=y_*+Ch^{p+1}+O(h^{p+2}),\]
\[y_{\rm high}=y_*+O(h^{p+2}),\]
\[d=y_{\rm low}-y_{\rm high}.\]

The difference estimates the leading local error of the lower-order formula. It has the units of the state. Scale each component by an absolute tolerance with those units plus a dimensionless relative tolerance times the current magnitude:

\[r=\max_i\frac{|d_i|}{{\rm atol}_i+{\rm rtol}\max(|y_{n,i}|,|y_{{\rm high},i}|)} .\]

If \(r\le1\) the proposed step passes this error estimate. Provided its leading coefficient varies slowly, a new step should change the estimated error by the ratio of step sizes raised to \(p+1\). Targeting unit scaled error gives

\[h_{\rm new}=s\,h\,r^{-1/(p+1)},\qquad 0<s<1 .\]

The safety factor leaves room for variation in the neglected terms. A real implementation bounds growth and handles \(r=0\) separately. A discontinuity or an event between endpoints can violate the smooth-error assumption even when the endpoints appear accurate.

Background · Embedded error estimate
\[e=y_{n+1}^{[p]}-y_{n+1}^{[p-1]}\]

Two formulas share stages and estimate a local error. The controller also needs absolute and relative scales for each component.

Derivation and applications

How component tolerances become a single step decision

A time step acceptable for one component can be inaccurate for another whose magnitude or units differ. Consider an embedded pair of methods sharing their internal stages and producing two endpoint estimates \(y_i^{H},y_i^{L}\). Let \(d_i=y_i^H-y_i^L\). For a smooth right-hand side and a pair whose lower formula has order \(p\), this difference is normally dominated by an \(h^{p+1}\) local term. Assume all components use the same step but have physically appropriate absolute scales \(a_i>0\) and relative tolerances \(r_i\).

Convert each difference to a dimensionless ratio using the larger recent endpoint magnitude. An infinity norm requires every component to meet its own allowance:

\[s_i=a_i+r_i\max(|y_{i,n}|,|y^H_i|),\]
\[E=\max_i |d_i|/s_i.\]

With approximately constant leading error coefficient and scale over the proposed retry, the new norm is predicted from the power law. Solving for a target near one and including a safety factor \(\nu<1\) gives

\[E_{\rm new}\simeq E(h_{\rm new}/h)^{p+1},\]
\[h_{\rm new}=\nu h E^{-1/(p+1)} .\]

Accept and advance with the higher-order estimate only when \(E\le1\) under this policy. If a component is converted to another unit by a factor \(c\), both its measured difference and absolute tolerance must scale:

\[|c d_i|/(|c|a_i+r_i|c|\max|y_i|)=|d_i|/s_i.\]

The decision is therefore invariant under consistent unit conversion. A bounded growth factor handles zero estimated differences and prevents a single unusually small defect from triggering a huge next step. This controller targets local agreement; its acceptance is not a proof of a global error bound.

Worked example

Decide a two-component retry

Let an order-two embedded lower estimate at \(h=0.100\) s produce differences \(0.000800\) m and \(0.00600\) m/s. The chosen component scales are \(0.00100\) m and \(0.00200\) m/s. Their ratios are \(0.800\) and \(3.00\). Reject the step and take \(\nu=0.9\), giving \(h_{\rm new}=0.9(0.100)3^{-1/3}=0.0624\) s. The leading-error model predicts a new norm \(0.9^3=0.729\).

Second application

Compare a componentwise limit with an averaged one

Suppose a system has 100 components, with 99 normalized errors zero and one equal to \(3.00\). Its root-mean-square norm is \(\sqrt{9/100}=0.300\), so an RMS policy would accept. The infinity policy rejects because one variable missed its tolerance threefold. An RMS policy can be appropriate when errors are distributed, while a strict component requirement demands the maximum or a separate check for a critical variable.

Ask different parts of a trajectory for different resolutions

A cooling transient changes rapidly at first and slowly near equilibrium. A uniform step small enough for the beginning can waste effort later. An adaptive controller proposes a step, estimates its error and accepts or rejects it. To estimate the error without the exact solution, two formulas can share function evaluations. With Euler and Heun, compute \(k_1=f(t_n,y_n)\), predict \(y_E=y_n+hk_1\), then evaluate \(k_2=f(t_n+h,y_E)\) and \(y_H=y_n+h(k_1+k_2)/2\).

Their difference \(y_H-y_E=h(k_2-k_1)/2\) estimates the leading local error of the lower-order Euler step. It normally scales as \(h^2\) in smooth regions. An embedded difference does not equal the local error of the higher-order accepted value; the higher-order value generally has a smaller error starting from accurate data. Nor does a local tolerance directly bound the error at the end of the whole run. Previous accepted steps already carry accumulated trajectory error.

Scale the estimate and choose a step

For a scalar use \(w=\mathrm{atol}+\mathrm{rtol}\max(|y_n|,|y_H|)\) and the dimensionless estimate \(E=|y_H-y_E|/w\). Accept when \(E\leq1\). If the next difference scales as \(h^2\), a proposed change is \(h_{new}=0.9h E^{-1/2}\). The factor 0.9 leaves a margin for changes in curvature. Practical implementations limit the ratio between successive steps and handle a zero difference by bounded growth. A rejected step is retried from the same original state and time.

For \(y'=-2y,\ y_n=1,\ h=0.1\), Euler gives 0.8 and Heun gives 0.82, a difference 0.02. With absolute tolerance 0.005 and zero relative tolerance for this demonstration, \(E=4\). The step is rejected and a revised step is

\[0.9(0.1)/2=0.045.\]

Re-evaluation from the original state gives

\[y_E=0.91,\ y_H=0.91405,\]

with difference 0.00405 and \(E=0.81\). The accepted state corresponds to time 0.045, not 0.145. Advancing time on rejection would skip the unresolved portion.

Background · What tolerance should a solver apply near zero concentration?

Two state components can be reported in very different units. An error of \(10^{-6}\) metres may be harmless for a position and an error of \(10^{-6}\) moles may dominate a trace species. A common error test divides each component by its own physical absolute tolerance plus a relative contribution, for instance

\[e_i^{\rm scaled}=\frac{e_i}{a_i+r\max(|y_{n,i}|,|y_{n+1,i}|)}.\]

At a zero crossing, \(a_i>0\) keeps the requested absolute accuracy finite. If concentration units change from mol/L to mmol/L, multiply the matching absolute tolerance by 1000 along with the numerical concentrations. The relative tolerance is dimensionless. Leaving the old absolute tolerance in place silently imposes a thousandfold tighter accuracy requirement near zero.

Estimate error using one full step and two half steps

A solver without an embedded pair can estimate its local error by applying the same order-\(p\) stepper twice from a common starting state: take one full step for \(Y_c\), and separately two successive half steps for \(Y_f\). Suppose the local error for one smooth step, numerical minus exact, is \(C h^{p+1}+O(h^{p+2})\). The two half-step errors accumulate to \(2C(h/2)^{p+1}+O(h^{p+2})\) because propagating the first over the short second half changes its leading coefficient only at higher order. Thus \(Y_f\) carries approximately \(2^{-p}\) of the full-step error.

Subtracting the two approximations gives an estimate of the correction to \(Y_f\): \((Y_f-Y_c)/(2^p-1)\). Adding it gives a Richardson-extrapolated value. The exponent in a local step controller remains \(1/(p+1)\) because the correction estimate scales as \(h^{p+1}\). The denominator \(2^p-1\) comes from taking two shorter steps to the same endpoint. Confusing that denominator with a single half-step local error factor introduces a systematic error in the estimate.

Take Heun's second-order method on \(y'=-y\), starting from one with \(h=0.4\). The full-step amplification is

\[1-0.4+0.4^2/2=0.68.\]

Each half step has amplification \(0.82\), so \(Y_f=0.6724\). The fine correction is

\[(0.6724-0.68)/3\approx-0.0025333.\]

The extrapolated value is 0.6698667. The exact endpoint \(e^{-0.4}\approx0.6703200\) requires correction \(-0.0020800\) to \(Y_f\). The estimated sign and scale are correct while higher-order terms are still visible. Discontinuous forcing, a skipped event or inconsistent internal solver tolerances can spoil this comparison even when the two output times agree.

Practise on sheet 3 · Accept or retry a nonautonomous Euler step. Further reading · University of British Columbia · Numerical Solution of Ordinary Differential Equations, Joel Feldman · Variable Step Size Methods, Euler and Euler–2step, pp. 1–4.

Locate events between stored states

An event is a zero of \(g(t,y(t))\), such as crossing a chosen temperature threshold. When its endpoint values have opposite signs, continuity brackets at least one event. Use a dense approximation to the trajectory or refine the time interval to locate the crossing. A linear interpolation between endpoints may be adequate for a smooth signal away from a turning point. If a value changes by a small amount near a flat crossing, a small field error can produce a much larger error in event time.

For \(y=e^{-t}\), readings at 0.6 and 0.8 are 0.54881 and 0.44933. A straight interpolation to 0.5 gives \(t\simeq0.69813\), while the exact time is \(\ln2=0.69315\). The discrepancy arises from the curved trajectory even if the endpoints are exact. A tangential event such as \((t-1)^2=0\) never changes sign, and two crossings in one step can also be missed. Declare the crossing direction, the required timing accuracy and the physical effect of reaching it. After an event that changes the model, start the next segment using the event state and preserve the time at which the transition actually occurred.

Test the event interpolant with a known direction

At a crossing where a scalar signal has nonzero derivative, an approximate amplitude error \(\delta y\) produces a first-order timing error \(\delta t\simeq-\delta y/y'\). For \(e^{-t}=0.5\), the derivative is \(-0.5\) per second. A signal that is 0.002 too high near the crossing therefore appears to reach the threshold about 0.004 s late. This local estimate converts a solver tolerance on a state into a physically interpretable timing requirement.

A source that switches precisely at the threshold makes the event time part of the subsequent model. A timing error then affects all later states even if ordinary integration error within each regime is small. When comparing grids, compare both the located event times and the post-event states at shared physical times. Two traces that agree before a switch can diverge later purely because one entered the new law sooner.

Relate state accuracy to event-time accuracy

Let an event be defined by \(G(t,y(t))=0\) and write the scalar event signal as \(g(t)\). Its derivative along a smooth trajectory is \(g'=G_t+G_y f\). At a simple crossing \(g'(t_\star)\ne0\), an error \(\delta g\) in evaluating the signal displaces the reported time by approximately \(-\delta g/g'(t_\star)\). More rigorously, if the true signal derivative has magnitude at least \(m\) throughout the final bracket, the mean value theorem bounds time error by the event-signal error divided by \(m\), in addition to any error left by the scalar root search.

Thus an event locator can solve its interpolated scalar equation to a tiny tolerance while returning a poor physical time. Errors in the interpolated state, uncertain event threshold and flat slope all propagate into the time estimate. At a tangential contact the derivative vanishes and the first-order estimate ceases to apply. Two simple events can merge into one contact or disappear altogether under a perturbation that would merely shift a steep isolated crossing by a small amount.

Time two crossings around the top of a trajectory

Take vertical position \(y(t)=10t-5t^2\) metres, with time in seconds. Its maximum is 5 m at one second. A detector at 4.8 m records two crossings, since \(5-5(t-1)^2=4.8\) gives \(t=1\pm0.2\), namely 0.8 and 1.2 s. The respective vertical velocities are \(+2\) and \(-2\) m/s. A small threshold increase of 0.01 m therefore moves the rising crossing about 0.005 s later and the falling crossing about 0.005 s earlier.

Check the exact effect at 4.81 m. The half-separation becomes

\[\sqrt{0.19/5}=\sqrt{0.038}\approx0.194936\,\mathrm{s}.\]

The actual time shifts have magnitude approximately 0.005064 s, so the local slope estimate is already informative. At a threshold of 4.999 m the speed is instead only \(10\sqrt{0.001/5}\approx0.14142\) m/s. Another increase of 0.01 m moves the threshold above the maximum and removes both crossings. Applying the previous linear timing estimate at this near-tangent setting would predict a displacement for an event that no longer exists.

A direction flag distinguishes the rising and falling events if each is bracketed, but it cannot recover two crossings hidden inside one large step with endpoint signals of the same sign. In this model the velocity event \(y'=0\) identifies the turning point and splits the time interval into monotone pieces. On each piece check the threshold against its attainable position range before starting a time root search. This strategy uses knowledge of the dynamics to ensure the event actually exists.

For example, a single step from 0.7 to 1.3 s gives position 4.55 m at both endpoints and therefore no sign difference relative to the 4.8 m detector. Inserting the peak at 1 s gives a positive signal there and immediately produces two valid brackets. Decreasing only the scalar root tolerance would have found neither event on the original unsplit step.

Choose the next step when a local error estimate misses its target

Suppose the local estimator behaves as \(E\simeq C h^p\). Matching the next error to tolerance by rescaling \(h\), then applying a safety factor 0.9, predicts

\[h_{\rm next}/h=0.9(\mathrm{tol}/E)^{1/p}.\]

Treat \(p\) as the power of this estimator.

Choose the next step when a local error estimate misses its target. Horizontal axis: Estimated local error / target. Vertical axis: Proposed next step / current step. Data follow the figure.
With E eight times the target, a cubic estimator requests a new step 0.45 times the old one. The formula proposes a length; an acceptance test must separately decide whether to retry the current step. Sudden events can invalidate the assumed scaling.
Read values from the plot
Proposed next step / current step at selected values of Estimated local error / target.
Estimated local error / targetLocal error power 2Local error power 3Local error power 5
0.035.19622.89651.8148
0.169072.18881.62761.2842
0.97980.909230.906140.90368
5.6780.37770.504480.63592
320.15910.283480.45

Download all 251 plotted samples (CSV)

Change parameters · Choose the next step when a local error estimate misses its target

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.

Study the full derivation

Find a crossing between accepted time steps · Dynamics and numerical time integration

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.