A1314 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

A13 · Lesson 14 · Numerical differentiation

A smaller step isn't always a better derivative

The derivative is a limit as the step shrinks to zero, so it seems the smaller the step, the better. On a computer that's only half true. A difference quotient makes two errors: truncation, from using a finite step, which shrinks with h, and rounding, from subtracting two nearly equal function values, which grows as 1/h. Their sum has a minimum, and with plain double precision the best forward difference gets only about 8 digits right. Knowing where that minimum is, and the tricks that move it (central differences, Richardson extrapolation, the complex step), matters every time a solver needs a Jacobian or firmware differentiates a sensor signal.

  • Each curve is the actual error of a derivative estimate of ex at x = 1, computed in double precision in your browser, against the step h.
  • The forward difference falls as h until about 10−8, then rises again: to the left, rounding wins. The central difference falls faster (as h2) and bottoms out near 10−5, with about 11 good digits.
  • The complex step, Imf(x+jh)/h, never subtracts, so it keeps improving down to the smallest h: it's exact to rounding from about 10−8 on.

Predict

A forward difference of ex at x = 1 uses h=10−12. About how accurate is it?

Name it

Difference formulas and their errors

With the rounding of each f value bounded by u|f| (u = 1.1 × 10⁻¹⁶ in double):

FormulaTruncationRoundingBest hBest error
forward f(x+h)−f(x)hh2|f′′|2u|f|h≈ε≈10−8≈10−8
central f(x+h)−f(x−h)2hh26|f′′′|u|f|h≈ε1/3≈6×10−6≈10−11
second f(x+h)−2f(x)+f(x−h)h2h212|f′′′′|4u|f|h2≈ε1/4≈10−4≈10−8

(for f and its derivatives of similar size; scale h with the size of x in general).

Richardson extrapolation

The central difference's error is c2h2+c4h4+⋯. Combining two steps cancels the h2 term:

D≈D(h/2)+D(h/2)−D(h)3

which is fourth-order accurate. Repeating the idea gives Romberg integration (lesson 19).

The complex step

For a real-analytic f, f(x+jh)=f(x)+jhf′(x)−h22f′′(x)+⋯, so

f′(x)≈Imf(x+jh)h

with error h26|f′′′| and no subtraction: take h=10−20 and get f′ to full precision. It needs code that accepts complex inputs.

Noisy data

Differencing samples with noise σ adds noise of about 2σ/h to the derivative: the finer the sampling, the worse. Filter before differentiating, or fit a smooth curve and differentiate that.

Worked example

Estimate ddxex at 1 with central differences at h = 0.1 and 0.05, then extrapolate.

Show the solution
  1. Central differences. D(0.1)=2.7228146D(0.05)=2.7194146 with errors 4.5×10−3 and 1.1×10−3: a quarter, as h2 predicts.
  2. Richardson extrapolation. D=2.7194146+2.7194146−2.72281463=2.7182813
  3. Compare with e. Against e = 2.7182818 the error is 5.7×10−7.

Answer extrapolation gives 2.7182813, within 5.7×10−7 of e.

Worked example

A 12-bit ADC sampled at 1 MHz has 0.5 mV of noise. How noisy is a forward-difference estimate of dV/dt?

Show the solution
  1. The noise in one difference. 2×0.5mV1µs=707V/s
  2. Compare with the signal. If the signal's slopes are a few V/ms (thousands of V/s), the estimate is mostly noise.
  3. Average. Averaging over 10 samples (h = 10 µs) cuts it to 71 V/s.

Answer about 707 V/s of noise, or 71 V/s when averaged over 10 samples.

Worked example

A position sensor is read every 1 ms with 1 µm of noise. How noisy is acceleration estimated by the second difference?

Show the solution
  1. The second difference. a≈xn+1−2xn+xn−1h2
  2. Noise in the numerator. With independent noise σ in each reading, it is σ1+4+1=σ6
  3. Divide by h2. 6×10−6(10−3)2=2.4m/s2
  4. What that means. That's a quarter of g, from a sensor good to a micrometer. Second derivatives amplify noise as 1/h2, so in practice one smooths first, fits a model, or measures acceleration directly.

Answer about 2.4 m/s² of noise, a quarter of g.

Check

What does the forward difference give for the derivative of x3 at x = 2 with h = 0.1?

Check

And the central difference for x3 at x = 2 with h = 0.1?

Check

In a format with 53 significant bits, about what step gives the most accurate forward difference (take h≈ε)?

Check

Take central differences of ex at x = 1 with h = 0.1 and h = 0.05. What's Richardson's correction, D(0.05)−D(0.1)3, which is added to D(0.05)? (Carry at least 8 digits in D.)

Check

Samples with 0.5 mV of noise, 1 µs apart, are differenced. About how much noise does the derivative estimate carry?

Write units the way you normally would: 1.5 V/s, 1500 mV/s and 0.0015 kV/s are the same answer.

Check

Why can the complex-step derivative use h=10−20 when a forward difference can't?

Check

A position is read every 1 ms with 1 µm of independent noise.

(a)

How noisy is the acceleration from the second difference?

Write units the way you normally would: 1.5 m/s^2, 1500 mm/s^2 and 0.0015 km/s^2 are the same answer.

(b)

How noisy is the velocity from the central difference xn+1−xn−12h?

Check

Apply the second difference to f=ex.

Step 1

What does f(x+h)−2f(x)+f(x−h)h2 give exactly?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does making the step smaller eventually make a difference quotient worse?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.