From first principles
Differentiate the trajectory that your instrument measures
Let a scalar parameter \(\theta\) enter both a smooth rate law \(y'=f(t,y,\theta)\) and the initial data \(y(0)=g(\theta)\). Compare two solutions at the same physical time. Their difference divided by a small parameter increment tends to the sensitivity \(S=\partial y/\partial\theta\). Differentiating the rate law at fixed time supplies a term from the moving state and a direct parameter term:
For a scalar terminal measurement \(J=\psi(y(T),\theta)\) the chain rule gives
When many parameters enter a single output, a backward equation can reuse their common output weighting. Choose \(\lambda(T)=\psi_y^T\) and \(\dot\lambda=-f_y^T\lambda\). Then the terms containing the unknown sensitivity cancel in the derivative of their scalar product:
Evaluate all derivatives on the original trajectory. For \(y'=-\theta y\) with fixed \(y(0)\), direct differentiation gives \(S(T)=-Ty(T)\), the sign expected when increasing the decay rate. A parameter-dependent observation time would add an endpoint term.
Derivation and applications
Differentiate the numerical update that produced the observed output
A gradient used in fitting a parameter should agree with the discretized objective actually evaluated. Consider explicit Euler on a scalar equation \(y'=f(y,\theta,t)\) with fixed, parameter-independent time steps and initial state. A single final observation defines \(J=\Phi(y_N,\theta)\). Treat the computed states as a chain of differentiable maps rather than replacing their dependence by that of the exact differential equation.
For the step \(F_n(y,\theta)=y+h f(y,\theta,t_n)\) a small perturbation satisfies
with derivatives evaluated on the stored numerical trajectory. Start from \(\delta J=\Phi_y\delta y_N+\Phi_\theta\delta\theta\) and substitute the last step. Repeating this substitution backward defines a weight on each earlier state:
Each stage contributes its local parameter derivative weighted by the future effect of that stage. For a fixed initial condition the total derivative is
With vector states the reverse update uses the transpose Jacobian. An initial condition depending on \(\theta\) adds \(\lambda_0\,\partial_\theta y_0\). The resulting gradient is exact for the discrete map up to arithmetic precision, though it can differ from the gradient of the continuous solution. Adaptive control decisions need special care if differentiating through changes in the time grid.
Worked example
Differentiate a two-step fitted decay
Let \(y'=-\theta y\), \(y_0=2.00\), \(h=0.250\), \(\theta=1.00\), and \(J=(y_2-1)^2/2\). The Euler states are \(1.50\) and \(1.125\), giving \(\lambda_2=0.125\), \(\lambda_1=0.09375\). The local parameter derivatives are \(-0.500\) and \(-0.375\). Thus
Increasing the decay rate locally reduces the positive final residual.
Second application
Compare with the continuous sensitivity at the same time
At \(t=0.500\) the exact value is \(2e^{-0.5}=1.21306\) and its derivative is \(-0.500(1.21306)=-0.60653\). The continuous objective therefore has derivative \(-0.1292\). That difference is expected at this coarse step. A centred parameter perturbation of the two-step numerical objective should approach \(-0.09375\), providing a direct implementation check without assuming that the time integrator is already accurate.
Differentiate the evolution
Let \(y'=f(t,y,\alpha)\) with smooth \(f\) and \(y(0)=y_0(\alpha)\). Assume that the solution exists and depends differentiably on \(\alpha\) throughout the observation window. Differentiating the equation along the solution gives
where \(S=\partial y/\partial\alpha\). The first term propagates state changes through the dynamics and the second injects the direct parameter effect. Losing the initial derivative gives the wrong sensitivity whenever a parameter also changes the initial preparation.
For an output \(Q=\phi(y(T),\alpha)\), the chain rule gives
Each parameter requires its own tangent column. Many observations can then reuse the propagated columns. Scaling matters because a derivative with respect to milliseconds is numerically different from the derivative with respect to seconds. Multiplying by a stated parameter perturbation converts the derivative back into a predicted observation change that can be checked directly.
A rate parameter and a final measurement
Take \(y'=-\alpha y,\ y(0)=2\). Then
Multiplying by \(e^{\alpha t}\) leaves \((e^{\alpha t}S)'=-2\), hence \(S=-2te^{-\alpha t}\). At \(\alpha=0.5\) per second and \(T=2\) s the sensitivity of final amplitude is \(-4/e\approx-1.47152\) amplitude units per inverse second. Raising the decay rate by 0.01 per second predicts a decrease of 0.014715. The exact change is \(2e^{-1}(e^{-0.02}-1)\approx-0.01457\); the discrepancy is quadratic in the parameter perturbation.
Testing a tangent code by differences requires controlling both the ODE integration error and subtraction error. Use several perturbation sizes. With a forward difference the truncation error normally decreases linearly at first, until numerical noise in subtracting two nearly equal outputs dominates.
Run one dual equation for a scalar output
Define a column vector \(z\) backward in time by \(-z'=f_y^Tz,\ z(T)=\nabla_y\phi\). Differentiating \(z^TS\) cancels the two terms containing \(f_y\). Integrating what remains gives
For the preceding final-amplitude observation \(z=e^{-\alpha(T-t)}\). Its product with \(f_\alpha=-y\) is the constant \(-2e^{-\alpha T}\), whose integral reproduces the tangent result. One adjoint trajectory can provide gradients with respect to many parameters for this single output. Its coefficients require the forward trajectory at earlier times, so storing or reconstructing that trajectory is part of the numerical cost. If the desired derivative is of the discrete numerical update itself, differentiating its specific steps and interpolation choices can matter at finite time resolution.
A cancellation at a particular observation time
Compare observing the final amplitude with observing the lost amplitude \(Q=y(0)-y(T)\). If the rate is the only parameter, changing the observation reverses the sign of the computed rate sensitivity. If instead the unknown is the common initial amplitude, the direct derivative of \(y(0)\) is one and the propagated derivative is \(e^{-\alpha T}\). The final derivative of the loss is \(1-e^{-\alpha T}\), tending to zero as the observation window shrinks. Omitting the direct term would predict a large negative initial sensitivity for a loss that is identically zero at zero time.
Differentiate the time at which a model switches
If a trajectory switches dynamics at a threshold, the event time itself depends on the parameter. Let a pre-event solution \(y^-(t,p)\) meet \(G(t,y,p)=0\) at \(\tau(p)\). Denote by \(S^-\) its parameter derivative at fixed time, evaluated on the nominal event. Differentiating the event equation gives
The denominator is the rate at which the trajectory crosses the threshold. This formula requires a transversal crossing and a locally unchanged event sequence. At a grazing contact the derivative may diverge or cease to exist, and a perturbed trajectory can encounter a different number of events.
Assume the state itself is continuous while the right-hand side changes from \(f^-\) to \(f^+\). The total parameter change of the event state is \(S^-+f^-\tau_p\). But the post-event tangent at a fixed clock time must remove the amount of post-event evolution gained or lost by shifting the start time, giving \(S^+=S^-+(f^--f^+)\tau_p\). This jump in a derivative of a continuous state is sometimes surprising. Comparing trajectories taken at the same physical clock time is different from comparing their states at their respective event times.
Estimate the final position after a velocity switch
Let a cart start at zero and travel at constant positive speed \(p\) until it reaches position one. It then travels at fixed speed 0.5. Observe its position at \(T=2\), and examine \(p=1\), so the event occurs at \(\tau=1\). The pre-event position is \(pt\) and its tangent at fixed time is
Using \(G=y-1\) gives
Since \(f^-=1\) and \(f^+=0.5\), the tangent immediately after the switch becomes
Subsequent evolution at the fixed speed adds no further parameter source.
Check directly: as long as the event precedes the observation, \(y(T,p)=1+0.5(2-1/p)\). Its derivative at one is indeed 0.5. At \(p=1.01\) the exact final position is approximately 1.5049505 instead of 1.5. The first-order prediction is 1.505. Carrying the pre-event tangent unchanged through the switch would predict 1.51, nearly doubling the response. Resetting it to zero because the event position is fixed would miss the change entirely.
At \(p=0.5\) the switch is exactly at the observation endpoint. Parameters below this value give \(y(T)=2p\), whereas parameters above it use the switched expression. Both one-sided derivatives happen to equal two here because the two velocities match at this specific threshold value. Their second derivatives differ. Changing the post-event speed to 0.25 makes even the first derivatives disagree at the boundary. An apparent smooth result in one carefully chosen switching case cannot justify ignoring event timing throughout a parameter study.
A finite-difference sensitivity test should consequently check which events occurred on both perturbed trajectories. Agreement in total simulated duration and integrator tolerance is insufficient if the jump sequence changed. Recording event times together with the reported output makes it possible to distinguish an error in the reset law from a derivative that has ceased to exist across the selected parameter interval.
Propagate a reset depending on the event location
If a switching event resets the state by \(y^+=R(t,y^-,p)\), differentiate the reset along the moving event before returning to a fixed clock time. The result is
This reduces to the continuous-state formula when \(R\) is the identity. The occurrence time must still come from the pre-event trajectory, since the post-event state generally does not satisfy the original trigger condition.
Modify the cart example so the trigger position is \(H(p)=1+0.2(p-1)\), and the indicated position is reset to \(R=0.3y^-+0.2\) before it increases at speed 0.5. At \(p=1\), \(\tau=H/p=1\) and \(\tau_p=0.2-1=-0.8\). The pre-event fixed-time tangent is one. The reset gives
The indicated final position at time two is \(0.3H+0.2+0.5(2-H/p)\), exactly one at the nominal parameter. Differentiating this expression gives \(0.3(0.2)-0.5(0.2-1)=0.46\), independently verifying the sign and the event-time correction. Simply differentiating the reset map at the nominal crossing position would give 0.3 and lose the varying length of the post-event trajectory.
Change parameters · Explore the worked calculation
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.