From first principles
Integrate the linear part exactly before approximating the source
Suppose \(y'=Ay+g(t,y)\), with a constant matrix \(A\). It may carry a large decay rate even though the applied source \(g\) varies smoothly. Multiply by \(e^{-At}\) and use the product rule. The derivative of the exponential cancels the linear term:
Integrate from \(t_n\) to \(t_n+h\) and multiply back by the final exponential. The exact formula assigns each source contribution the propagation corresponding to its remaining time in the step:
Approximate only the source by its starting value. Integrating the exponential weight yields
For nonzero scalar \(z\) the series equals \((e^z-1)/z\); at zero it equals one. The series remains meaningful for a singular matrix. A constant source is integrated exactly by this step. Variation in the source, including its dependence on the moving state, determines its remaining time-discretisation error.
Derivation and applications
Evaluate exponential weights without subtracting almost equal numbers
The integrating-factor solution of \(y'=\lambda y+g(t)\) gives useful one-step formulas when \(\lambda\) is constant and the source changes slowly. Approximate the source within a step by the straight line from \(g_n\) to \(g_{n+1}\). Set \(z=h\lambda\) and use the unit interval variable \(\theta=(s-t_n)/h\). Variation of constants first yields
Integrate the constant and linear source terms separately. For \(z\ne0\) direct evaluation gives the two weights and their connection:
The apparent poles at zero are removable. Expanding the exponential before subtracting gives stable small-argument evaluations:
An accurate routine for \(e^z-1\) helps with the first weight, but subtracting \(z\) to compute the second can still lose digits. Use its own series near zero. At \(z=0\) the formula reduces to trapezoidal integration of the source, which is exact for the chosen linear interpolant. This limit tests simulated components whose linear decay is zero.
Worked example
Check a strongly damped ramp
Set \(\lambda=-2.00\) s⁻¹, \(h=0.500\) s, \(y_n=0\) and let the source rise from zero to \(4.00\) units/s over the step. Here \(z=-1\) and \(\varphi_2(-1)=e^{-1}=0.367879\). The calculated rise is \(0.500(4.00)(0.367879)=0.73576\) units. Without decay the same ramp deposits exactly \(1.00\) unit. Both predictions can be checked directly by integrating the specified source history.
Second application
Test a nearly neutral component
Take \(z=-10^{-8}\) and evaluate \(e^z-1-z\). Its correct numerator is about \(5.00\times10^{-17}\), comparable to the rounding uncertainty from subtracting one from a number near one. The series instead gives \(\varphi_2\approx0.499999998333\). For unit-duration integration of a ramp from zero to two units/s the response is about \(0.9999999967\) units. A returned zero here signals cancellation in weight evaluation, not suppression of the source by the model.
OneSimulation laboratory
Compare time-step error with conservation
Start from this chapter. Here the exponential propagator is available exactly. Use it as a reference and check how much accuracy the explicit methods achieve at equal physical time.
Compare recorded OneSimulation runs on this page. To test your own inputs, download the Python model and run the model in an activated OneSimulation environment.
Run the isothermal reaction from \(A=1,\ B=0\) mol/m³ to \(t=5/k=6.25\) s. Measure the maximum A error over all stored times and the drift of \(A+B\). First double the number of RK4 steps. Then restore 100 steps and change the time integrator. Download the Python experiment · OneSimulation first-order decay model.
python ordinary-differential-equations.py
python ordinary-differential-equations.py --steps 200
python ordinary-differential-equations.py --method euler
| Method, steps | Final A (mol/m³) | Max. A error (mol/m³) | Max. drift (mol/m³) |
|---|---|---|---|
| RK4, 100 | 0.00673794883 | 2.00 × 10⁻⁸ | 4.44 × 10⁻¹⁶ |
| RK4, 200 | 0.00673794711 | 1.22 × 10⁻⁹ | 3.33 × 10⁻¹⁶ |
| Euler, 100 | 0.00592052922 | 9.39 × 10⁻³ | 4.44 × 10⁻¹⁶ |

As a fourth run, try --method euler --steps 5. This lands exactly on the nonnegativity limit. Predict both the final concentration and the largest error before running it.
Change parameters · Change the laboratory inputs
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.
Problem sheet 1 · Predict when a conservative calculation loses all reactant
An exact decay factor can hide an aliased heat input
A thermal sensor obeys \(y'=-a y+B\cos(\omega t)\) with \(a>0\), \(y(0)=0\). The state is a temperature change; \(B\) has units of temperature per time. Multiplying by \(e^{at}\) yields an exact integral of the applied force over the step. In complex notation the real part of \(B(e^{i\omega t}-e^{-at})/(a+i\omega)\) gives the full continuous solution. This reference tests the response of the sensor, including the initial transient, at any requested time.
An exponential step advances the homogeneous decay by its exact factor \(e^{-ah}\). A tempting low-cost rule freezes the heat input at its sampled left endpoint. Integrating that constant force gives
The gain of the homogeneous part is smaller than one for every positive time step. That stability property does not guarantee that the sampled input represents the actual periodic heater.
Choose \(h=2\pi/\omega\). The solver now observes the heater only when its cosine equals one. It therefore predicts \(y_n=B(1-e^{-at_n})/a\), exactly the response to constant heating. At these very same times the analytic periodic experiment gives \(Ba(1-e^{-at_n})/(a^2+\omega^2)\). Neither numerical roundoff nor an approximation to the decay exponential is responsible for the difference. An entire period of alternating input was replaced by a single repeated measurement.
Open full-size diagramIn the notebook the default \(a=0.8\,\mathrm{s}^{-1}\), \(B=1\,\mathrm{K/s}\) and frequency one cycle per second make the long-time sampled responses approach 1.25 K and about 0.01994 K respectively. With more samples per period the frozen-input answer begins to recover the oscillation. For very short steps, evaluating its gain as \(-\mathrm{expm1}(-ah)/a\) also avoids cancellation between nearly equal floating-point numbers. The two numerical issues call for different remedies: a stable elementary function helps with arithmetic, while additional observations or a better forcing interpolant address temporal aliasing.
A useful alternative test changes the phase of the forcing while holding the step fixed. Starting a full-period sampler at a cosine maximum produces apparent constant heating. Starting at a zero crossing produces apparently zero heating at every step. The true steady response in either case has the same amplitude, shifted in time. Large sensitivity of the computed amplitude to this arbitrary shift provides another experimental diagnosis of inadequate forcing resolution.
A control engineer halves the step to half a forcing period and finds that successive heat readings alternate between \(+B\) and \(-B\). Does their zero mean guarantee the correct sampled temperatures?
Work through the solution
It restores the correct arithmetic mean of the samples for complete periods. The resulting heat input is still represented internally as a piecewise constant square wave. The convolution weights recent heat input more heavily than earlier input, so matching an unweighted mean leaves amplitude and phase errors. Compare the exact solution at several phases and refine again before reporting either observable.
Change parameters · Explore this notebook example
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.
Preserve the linear response
For \(y'=Ay+g(t,y)\) with constant \(A\), multiply by \(e^{-At}\). The derivative of the product is \(e^{-At}g(t,y(t))\), because the two terms containing \(A\) cancel. Integrating between \(t_n\) and \(t_n+h\), then multiplying back, gives the exact variation-of-constants formula
It separates the free propagation of the initial state from the accumulated, propagated forcing. Freezing \(g\) at the beginning of the step gives exponential Euler. Its forcing weight is \(h\varphi_1(hA)\), where \(\varphi_1(z)=(e^z-1)/z\) has the continuous value 1 at zero. The defining integral or power series applies to a singular matrix without computing an inverse.
Derive the leading defect
Assume along the solution that the change in \(g\) over \(s\) is bounded by \(Ls\), and that the linear propagator has norm at most \(M\) during the step. Subtract the frozen approximation from the exact integral. Its norm is bounded by \(ML\int_0^h s\,ds=MLh^2/2\). This is a local error bound. To obtain a global first-order estimate one must also bound propagation of earlier state errors, using the stability of the linear propagator and the Lipschitz dependence of the remaining forcing on the state. A stiff term hidden inside \(g\) can still impose a severe practical restriction.
Heat loss with a steady input
Let \(y'=-100y+20\) with \(y(0)=1\) and time in seconds. The equilibrium is 0.2. At \(h=0.1\), exponential Euler gives \(y_1=e^{-10}+0.2(1-e^{-10})\approx0.20003632\), exactly the analytical solution at that time. Forward Euler would give \(-7\), whereas backward Euler gives \(3/11\approx0.27273\). Both the exponential step and backward Euler damp the fast mode here, but the computed amount of transient that survives differs substantially. A large step that accurately reproduces its endpoint can still leave an output trace too sparse to measure the early decay time.
Change the input to \(20+10t\). During the first step exponential Euler ignores its linear growth. The missing term is
At 0.1 s it is approximately 0.00900005. Including a linear approximation to the forcing would recover this term exactly. This calculation isolates the effect of freezing a changing input while the stiff decay remains exact.
Evaluating the weights
For small \(|z|\), direct subtraction of one from \(e^z\) loses significant digits. A dedicated exponential-minus-one function or the series \(1+z/2+z^2/6+\cdots\) yields a reliable \(\varphi_1\). Large sparse systems often require only the action of an exponential on a vector. Computing this action introduces another error budget separate from time approximation; a constant-forcing test with a known equilibrium checks both parts together.
Constant states as an implementation test
If \(Ay_\ast+g_\ast=0\), an exponential step starting at \(y_\ast\) must reproduce the same state for every step length. The series identity \(h\varphi_1(hA)A=e^{hA}-I\) makes the cancellation exact mathematically. Test this condition with a matrix containing both a zero eigenvalue and a strongly negative eigenvalue: the zero mode should accumulate a constant source linearly, while the decaying mode approaches its forced equilibrium. This test distinguishes a faulty treatment of the zero limit from ordinary temporal error caused by a varying input.
Integrate a linearly varying source with a second exponential weight
For \(y'=Ay+b_0+(t-t_n)b_1\) during a step, the variation-of-constants integral contains a constant and a linear source. Expand \(e^{(h-s)A}\) in its power series inside \(\int_0^h e^{(h-s)A}s\,ds\). Integrating each term gives \(h^{j+2}A^j/(j+2)!\). Consequently the appropriate weight for the slope is \(h^2\varphi_2(hA)\) with \(\varphi_2(z)=\sum_{j\ge0}z^j/(j+2)!\). Together with the constant-source \(\varphi_1\) term this gives the exact step for a source genuinely affine in time.
The identity \(z\varphi_2(z)=\varphi_1(z)-1\) follows from the series. At zero the continuous value is one half, matching the elementary time integral of a ramp. Computing this weight by dividing two nearly equal numbers can lose digits near zero and fails as an ordinary matrix division when \(A\) is singular. The series definition and augmented-system formulations give well-defined actions in both cases. If the source depends on the unknown state, an independently known time slope is no longer enough: its total derivative includes the state sensitivity carried along the current trajectory.
Propagate a linearly increasing acceleration through a coupled system
Let \(y=(q,v)^T,\ q'=v,\ v'=2+6t\), with \(q(0)=1\) m and \(v(0)=0\). The matrix \(A=\left(\begin{smallmatrix}0&1\\0&0\end{smallmatrix}\right)\) is singular and satisfies \(A^2=0\). The matrix series therefore terminate:
with arguments \(hA\) understood. Multiplying \(b_0=(0,2)^T\) by the first source weight gives \((h^2,2h)^T\). Multiplying \(b_1=(0,6)^T\) by the ramp weight gives \((h^3,3h^2)^T\).
At \(h=0.5\) s, the freely propagated initial state is still \((1,0)^T\). The constant-source contribution is \((0.25,1)^T\) and the ramp contribution is \((0.125,0.75)^T\). The final state is therefore \((1.375,1.75)^T\). Independently integrating the acceleration gives \(v=2t+3t^2\) and \(q=1+t^2+t^3\), confirming both numbers. A method freezing acceleration at its initial value would report \(q=1.25,\ v=1\) despite propagating the coupling \(q'=v\) exactly.
This example shows how information travels through a seemingly empty entry of the source: the position receives a ramp contribution although the forcing acts directly only on velocity. Treating the exponential as an entrywise operation would miss it. For larger sparse systems the same action can be obtained by augmenting the state with coordinates representing the constant and linear input, then applying one matrix exponential to the augmented initial vector.
For the stated ramp, define two extra scalar coordinates \(c=1\) and \(s=t\) with \(c'=0,\ s'=c\). Then the augmented linear equation has \(y'=Ay+b_0 c+b_1 s\). Powers of this single augmented matrix transfer \(c\) successively into \(s\), acceleration, velocity and position. Its exponential is therefore a finite polynomial for the present nilpotent structure. This explicit construction supplies a second implementation that can test the individual \(\varphi\) actions without forming a matrix inverse. If the ramp is restarted at each step, initialise the auxiliary coordinate consistently with whether \(b_0\) is the original source or its value at the new time.
Initialising both auxiliary coordinates anew with their original values while retaining a global-time ramp would repeatedly turn off the accumulated increase of the input.
Change parameters · Variation of constants and exponential steps
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.