---
title: Numerical differentiation
heading: A smaller step isn't always a better derivative
eyebrow: A13 · Lesson 14 · Numerical differentiation
xp: 40
---

::: hook
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.
:::

::: play
- Each curve is the actual error of a derivative estimate of $e^x$ 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 $h^2$) and bottoms out near $10^{-5}$, with about 11 good digits.
- The complex step, $\text{Im}\,f(x + jh)/h$, never subtracts, so it keeps improving down to the smallest h: it's exact to rounding from about $10^{-8}$ on.
:::
:::

```sim plot
view: graph
x: h = 1e-16..1 log
y: error = 1e-17..1 log
plot: abs((exp(1 + h) - exp(1))/h - exp(1)) + 1e-17 | samples: 1600 | label: forward difference
plot: abs((exp(1 + h) - exp(1 - h))/(2*h) - exp(1)) + 1e-17 | samples: 1600 | color: ch2 | label: central difference
plot: abs(Im(exp(1 + j*h))/h - exp(1)) + 1e-17 | samples: 1600 | color: ch3 | label: complex step
readout: best forward step, about √ε = 2^(-26)
readout: best central step, about ε^(1/3) = 2^(-52/3)
```

::: predict
## A forward difference of $e^x$ at x = 1 uses $h = 10^{-12}$. About how accurate is it?

- [ ] Accurate to about 12 digits
- [ ] Accurate to about 16 digits
- [x] Wrong in the fourth digit
- [ ] Exactly right

::: reveal
Wrong in the fourth digit: the error is $4.3 \times 10^{-4}$. The two values $e^{1 + 10^{-12}}$ and $e^1$ agree in their first 12 digits, so their difference keeps only about 4, and dividing by h doesn't restore them. At $h = 10^{-8}$ the error is $6.6 \times 10^{-9}$, close to the best possible.
:::
:::

::: nameit
## Difference formulas and their errors

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

| Formula | Truncation | Rounding | Best h | Best error |
|---|---|---|---|---|
| forward $\frac{f(x + h) - f(x)}{h}$ | $\frac h2\lvert f''\rvert$ | $\frac{2u\lvert f\rvert}{h}$ | $\approx\sqrt\varepsilon \approx 10^{-8}$ | $\approx 10^{-8}$ |
| central $\frac{f(x + h) - f(x - h)}{2h}$ | $\frac{h^2}{6}\lvert f'''\rvert$ | $\frac{u\lvert f\rvert}{h}$ | $\approx\varepsilon^{1/3} \approx 6 \times 10^{-6}$ | $\approx 10^{-11}$ |
| second $\frac{f(x + h) - 2f(x) + f(x - h)}{h^2}$ | $\frac{h^2}{12}\lvert f''''\rvert$ | $\frac{4u\lvert f\rvert}{h^2}$ | $\approx\varepsilon^{1/4} \approx 10^{-4}$ | $\approx 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 $c_2h^2 + c_4h^4 + \cdots$. Combining two steps cancels the $h^2$ term:

$$D \approx D(h/2) + \frac{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) - \frac{h^2}{2}f''(x) + \cdots$, so

$$f'(x) \approx \frac{\text{Im}\,f(x + jh)}{h}$$

with error $\frac{h^2}{6}\lvert f'''\rvert$ 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 $\sqrt2\,\sigma/h$ to the derivative: the finer the sampling, the worse. Filter before differentiating, or fit a smooth curve and differentiate that.
:::

::: example
## Estimate $\frac{d}{dx}e^x$ at 1 with central differences at h = 0.1 and 0.05, then extrapolate.

::: solution
1. **Central differences.**
   $$\begin{aligned} D(0.1) &= 2.7228146 \\ D(0.05) &= 2.7194146 \end{aligned}$$
   with errors $4.5 \times 10^{-3}$ and $1.1 \times 10^{-3}$: a quarter, as $h^2$ predicts.
2. **Richardson extrapolation.**
   $$\begin{aligned} D &= 2.7194146 \\ &\quad + \frac{2.7194146 - 2.7228146}{3} \\ &= 2.7182813 \end{aligned}$$
3. **Compare with e.** Against e = 2.7182818 the error is $5.7 \times 10^{-7}$.

**Answer:** extrapolation gives 2.7182813, within $5.7 \times 10^{-7}$ of e.
:::
:::

::: 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?

::: solution
1. **The noise in one difference.**
   $$\frac{\sqrt2 \times 0.5\ \text{mV}}{1\ \text{µs}} = 707\ \text{V/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.
:::
:::

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

::: solution
1. **The second difference.**
   $$a \approx \frac{x_{n+1} - 2x_n + x_{n-1}}{h^2}$$
2. **Noise in the numerator.** With independent noise σ in each reading, it is
   $$\sigma\sqrt{1 + 4 + 1} = \sigma\sqrt6$$
3. **Divide by $h^2$.**
   $$\frac{\sqrt6 \times 10^{-6}}{(10^{-3})^2} = 2.4\ \text{m/s}^2$$
4. **What that means.** That's a quarter of g, from a sensor good to a micrometer. Second derivatives amplify noise as $1/h^2$, 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
given: x = 1 | 2 | 3; h = 0.1 | 0.01
answer: 3*x^2 + 3*x*h + h^2
tolerance: 0.01%

## What does the forward difference give for the derivative of $x^3$ at x = {x} with h = {h}?

::: right
Right. $\frac{(x + h)^3 - x^3}{h} = 3x^2 + 3xh + h^2$ = {answer}.
:::

::: misses
- [3*x^2] That's the exact derivative; the forward difference adds $3xh + h^2$.
- [any] Not yet. Expand $(x + h)^3$.
:::
:::

::: check
given: x = 1 | 2 | 3; h = 0.1 | 0.2
answer: 3*x^2 + h^2
tolerance: 0.001%

## And the central difference for $x^3$ at x = {x} with h = {h}?

::: right
Right. $3x^2 + h^2$ = {answer}: the error is $h^2$, much smaller.
:::

::: misses
- [3*x^2] That's exact; the central difference adds $h^2$ (from $\frac{h^2}{6}f''' = h^2$).
- [any] Not yet. $\frac{(x + h)^3 - (x - h)^3}{2h}$.
:::
:::

::: check
given: p = 24 | 53 | 64
answer: sqrt(2^(1 - p))
tolerance: 1%

## In a format with {p} significant bits, about what step gives the most accurate forward difference (take $h \approx \sqrt\varepsilon$)?

::: right
Right. $\sqrt{2^{1-p}}$ = {answer}.
:::

::: misses
- [2^(1 - p)] Too small: rounding error would be about 1. Balance the two errors at $\sqrt\varepsilon$.
- [(2^(1 - p))^(1/3)] That's the best step for a central difference.
- [any] Not yet. $h \approx \sqrt\varepsilon$.
:::
:::

::: check
given: x = 0.5 | 1 | 2
answer: (exp(x)*sinh(0.05)/0.05 - exp(x)*sinh(0.1)/0.1)/3
tolerance: 0.5%

## Take central differences of $e^x$ at x = {x} with h = 0.1 and h = 0.05. What's Richardson's correction, $\frac{D(0.05) - D(0.1)}{3}$, which is added to D(0.05)? (Carry at least 8 digits in D.)

::: right
Right. {answer}: it removes almost all of D(0.05)'s error.
:::

::: misses
- [exp(x)*sinh(0.05)/0.05 - exp(x)*sinh(0.1)/0.1] Divide the difference by $2^2 - 1 = 3$, because the error goes as $h^2$.
- [any] Not yet. $D(h) = \frac{e^{x + h} - e^{x - h}}{2h}$; compute both, subtract and divide by 3.
:::
:::

::: check
given: s = 0.5 | 2; h = 1 | 10
answer: sqrt(2)*s/h*1000
unit: V/s
tolerance: 0.5%

## Samples with {s} mV of noise, {h} µs apart, are differenced. About how much noise does the derivative estimate carry?

::: right
Right. $\sqrt2\,\sigma/h$ = {answer} V/s.
:::

::: misses
- [s/h*1000] Two noisy samples are subtracted, so the noise grows by $\sqrt2$.
- [any] Not yet. $\sqrt2\,\sigma/h$.
:::
:::

::: check
kind: choice

## Why can the complex-step derivative use $h = 10^{-20}$ when a forward difference can't?

- [ ] Complex numbers have more bits
- [x] It reads $f'$ from the imaginary part directly, without subtracting nearly equal values
- [ ] Its truncation error is zero
- [ ] Small h is never a problem

::: misses
- [1] A complex double is two ordinary doubles.
- [3] Its truncation error is $\frac{h^2}{6}f'''$, just tiny for tiny h.
- [4] For a forward difference, tiny h is the whole problem.
:::
:::

::: check
kind: parts
given: s = 1 | 5; h = 1 | 2

## A position is read every {h} ms with {s} µm of independent noise.

::: part
answer: s*1e-6*sqrt(6)/(h*1e-3)^2
unit: m/s^2
tolerance: 0.5%

### How noisy is the acceleration from the second difference?

::: misses
- [s*1e-6*2/(h*1e-3)^2] The noise adds in squares: $\sqrt{1 + 4 + 1}$.
- [any] $\frac{\sigma\sqrt6}{h^2}$.
:::
:::

::: part
answer: s*1e-6/(sqrt(2)*h*1e-3)*1000
unit: mm/s
tolerance: 0.5%

### How noisy is the velocity from the central difference $\frac{x_{n+1} - x_{n-1}}{2h}$?

::: misses
- [s*1e-6*sqrt(2)/(h*1e-3)*1000] Divide by 2h: $\frac{\sigma\sqrt2}{2h}$.
- [any] $\frac{\sigma\sqrt2}{2h}$.
:::
:::
:::

::: check
kind: derivation
vars: x = 0..1, h = 0.01..0.5

## Apply the second difference to $f = e^x$.

::: step
answer: exp(x)*(2*cosh(h) - 2)/h^2

### What does $\frac{f(x + h) - 2f(x) + f(x - h)}{h^2}$ give exactly?
:::

::: step
answer: exp(x)*h^2/12

### Expanding $2\cosh h - 2 = h^2 + \frac{h^4}{12} + \cdots$, what is the leading error?
:::

::: right
Right. The error is $\frac{h^2}{12}f''''$, second order, as for the central first difference.
:::
:::

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

::: model
A difference quotient has two errors. The truncation error, from replacing the limit by a finite step, shrinks as h shrinks: as h for a forward difference, as $h^2$ for a central one. The rounding error comes from subtracting f(x + h) and f(x), which agree in more and more leading digits as h shrinks; each carries a rounding error of about $u\lvert f\rvert$, which the subtraction leaves untouched and the division by h magnifies, so it grows as $1/h$. The total is smallest where the two balance, near $h \approx \sqrt\varepsilon$ for a forward difference, and below that the rounding term takes over and the answer gets worse.
:::
:::

::: practice

::: check
given: x = 2 | 4; h = 0.5 | 0.05
answer: 3*x^2 + 3*x*h + h^2
tolerance: 0.01%

## Forward difference of $x^3$ at {x}, h = {h}?

::: misses
- [any] $3x^2 + 3xh + h^2$.
:::
:::

::: check
given: p = 11 | 24 | 53 | 64 | 113
answer: (2^(1 - p))^(1/3)
tolerance: 1%

## Best central-difference step for {p} bits?

::: misses
- [any] $\varepsilon^{1/3}$.
:::
:::

::: check
given: s = 1 | 5; h = 2 | 50
answer: sqrt(2)*s/h*1000
unit: V/s
tolerance: 0.5%

## Derivative noise, {s} mV noise, {h} µs spacing?

::: misses
- [any] $\sqrt2\,\sigma/h$.
:::
:::
:::
