Derive competing contributions to error
Suppose \(f\) is smooth and you need its slope at \(x\) to form a Newton correction. A forward difference \(D_h=[f(x+h)-f(x)]/h\) has Taylor expansion \(f'(x)+hf''(x)/2+O(h^2)\). A centred difference \([f(x+h)-f(x-h)]/(2h)\) instead cancels the even terms, leaving \(f'(x)+h^2 f'''(x)/6+O(h^4)\). Both limits are exact in real arithmetic as \(h\to0\). The program, however, returns approximate residual values, and a subtraction of nearby values can lose significant digits. If each absolute evaluation error is at most \(\delta\), the derivative error from those perturbations is at most \(2\delta/h\) for forward difference and \(\delta/h\) for the centred one.
Thus an illustrative forward error bound has shape \(a h+b/h\); minimisation gives
The centred bound \(a h^2+b/h\) has minimiser \(h_*=(b/(2a))^{1/3}\). Floating-point roundoff acting on \(O(1)\) residual values often gives the familiar square-root and cube-root machine-precision scales, but the physical units of \(x\), the scale of the derivative and noise from the residual solver must be included. A nonlinear function evaluated by an iterative PDE solve can have a much larger \(\delta\) than roundoff. Repeating the inner solve with tighter tolerance can shift the optimal step considerably.
For \(f(x)=e^x\) at zero, the forward leading error is \(h/2\), centred error \(h^2/6\). If \(\delta=10^{-10}\) as an absolute per-value uncertainty, a forward worst-case bound gives optimal \(h=2\times10^{-5}\), while the centred model gives
The centred step can be much larger yet retain lower truncation error. This is a scale estimate using worst-case errors, not a guarantee about the phase or stochastic correlation of actual errors. If queries share common random numbers or a warm-started solver, errors can partly cancel or acquire a systematic derivative bias.
Obtain the slope from an imaginary perturbation
For a real-valued function with an analytic complex extension near the real argument, expand \(f(x+ih)=f(x)+ihf'(x)-h^2f''(x)/2-ih^3f'''(x)/6+\cdots\). Taking its imaginary part and dividing by \(h\) gives second-order accuracy with no explicit subtraction of two nearly equal real numbers. For \(e^x\) at zero, the result is \(\sin h/h\). At very small representable \(h\) the imaginary signal is itself proportional to \(h\) and may still be carried accurately by analytic elementary operations. This advantage disappears if the computation discards imaginary parts, uses nonanalytic absolute values, branches on the perturbation, or applies a black-box routine using complex arithmetic for unrelated purposes. Automatic differentiation supplies another route through differentiable implemented operations, but it too needs the program's mathematical definition to match the derivative being requested.
Audit a Jacobian action before assembling a matrix
For \(F:\mathbb R^n\to\mathbb R^m\), pick direction \(v\) and examine \(R(h)=\|F(x+hv)-F(x)-hJv\|\). If a proposed Jacobian action is correct and \(F\) is twice differentiable, \(R(h)\) initially scales like \(h^2\). A wrong first derivative generally produces a leading \(O(h)\) term. The test checks orientation, units, signs and chain rules without constructing a dense finite-difference matrix. Test several directions and scales; a wrong matrix can annihilate the one chosen direction. At very small \(h\) a roundoff or inner-solver floor eventually hides both ideal slopes. When working with constraints use perturbations remaining inside the physical domain or give derivatives in a local feasible parametrisation.
Find the useful step before a Newton update
The experiment evaluates the forward, centred and complex-step slopes of a smooth elementary residual in floating-point arithmetic. It plots absolute errors on logarithmic scales and compares them with the exact derivative. Move the evaluation point and note that occasional accidental zeros are rounded at the plotting floor. Their exact location is not a robust guarantee for a different residual. Compare the range of steps with a reproducible error trend before interpreting one apparently perfect result.
Change the evaluation before changing the step
For the exponential the forward numerator has an exact factorisation \(e^{x+h}-e^x=e^x(e^h-1)\). A numerical library's expm1(h) evaluates the last factor accurately even for very small \(h\), using a local series or a reduced-argument algorithm. Form the quotient as \(e^x\,\mathrm{expm1}(h)/h\). Its mathematical truncation error is still \(e^xh/2+O(h^2)\). Its rounding behaviour differs because the two terms near \(e^x\) are never computed separately. At \(x=1\) and \(h=10^{-18}\), adding \(h\) to a binary64 value of one rounds back to one. The direct forward quotient then returns zero. The factored evaluation gives the derivative \(e\) to working precision.
This experiment diagnoses the source of an error curve. If the algebraic form is inaccessible, a finite-difference checker sees black-box function values and cannot apply the factorisation. If the entire residual is available, stable elementary functions can change the derivative accuracy of a Newton step without changing its formal order.
Tutorial · design tests for smoothness and implementation errors
Diagnose a forward-difference Newton iteration
For \(f(x)=x^2-2\) at \(x=1.4\), obtain the exact dependence of a forward derivative estimate on \(h\). Compare one Newton step using \(h=0.1\) with the exact Jacobian.
Work through the solution
The difference quotient is
The residual is \(-0.04\). The exact step gives
using 0.1 gives \(1.413793\). Centred differences evaluate this polynomial's derivative exactly in real arithmetic for every nonzero \(h\), but roundoff can spoil very small steps. The discrepancy at large \(h\) is a calculable systematic truncation, so merely tightening the outer Newton stopping tolerance cannot remove it.
Catch a direction missed by one test
A proposed Jacobian differs from the correct one by \(E=\left(\begin{smallmatrix}1&-1\\0&0\end{smallmatrix}\right)\). Compare tests along \((1,1)\) and \((1,0)\) and show which observed slope would be misleading.
Work through the solution
The first direction lies in the nullspace of \(E\), so its Taylor remainder can show the expected \(h^2\) slope despite an incorrect Jacobian. The second has \(Ev=(1,0)\), producing an \(O(h)\) defect. Random directions augment a set of unit- or physics-informed probes, particularly when an error affects a single column or constraint block. One successful slope does not establish correctness of an entire matrix.
Determine whether complex step applies to a norm
A residual returns \(f(x)=|x|\) using a complex modulus routine. At \(x=1\), what does a complex-step call produce? Is this evidence that the real function has zero derivative there?
Work through the solution
The routine computes \(|1+ih|=\sqrt{1+h^2}\), purely real, so the reported complex-step slope is zero. The real derivative at \(x=1\) equals one. The implementation used a nonanalytic map of a complex variable. At a known positive branch the analytic local extension \(f(z)=z\) gives the correct derivative, while the real function at the kink \(x=0\) has no classical derivative. Check both the real regularity and what complex arithmetic actually executes before applying this technique.