MathsCalculus

Numerical Differentiation
calculator.

Estimate a derivative from nearby function values, and see the true error of each.

How it's worked out

A derivative is a limit of slopes, so a slope across a small step h makes a good estimate. You use this when f comes from a simulation or a lookup and you have no formula to differentiate. Taylor's theorem tells you how large each estimate's error is.

Dforward\displaystyle D_{\text{forward}}=f(x+h)−f(x)h\displaystyle {}= \frac{f(x + h) - f(x)}{h}
Dcentral\displaystyle D_{\text{central}}=f(x+h)−f(x−h)2h\displaystyle {}= \frac{f(x + h) - f(x - h)}{2h}

The forward difference is off by about h2f′′(x)\frac{h}{2} f''(x). That is first order: halve h, and the error halves. In the central difference, the even terms cancel, so the error shrinks with h2h^2. That is second order, and it costs just one more evaluation of f.

N\displaystyle N=f(x−2h)\displaystyle {}= f(x - 2h)−8f(x−h)\displaystyle {}- 8 f(x - h)+8f(x+h)\displaystyle {}+ 8 f(x + h)−f(x+2h)\displaystyle {}- f(x + 2h)
Dfive\displaystyle D_{\text{five}}=N12h\displaystyle {}= \frac{N}{12 h}

N is the top of the five-point formula. Richardson extrapolation combines central differences at h and half of h so their main error terms cancel. It and the five-point formula are both fourth order.

DR\displaystyle D_{\text{R}}=4D(h/2)−D(h)3\displaystyle {}= \frac{4 D(h/2) - D(h)}{3}
f′′(x)\displaystyle f''(x)≈f(x+h)−2f(x)+f(x−h)h2\displaystyle {}\approx \frac{f(x + h) - 2 f(x) + f(x - h)}{h^2}

You enter f as a formula, so the tool also finds the exact derivative with its own rules, and it subtracts to give you the true error of each estimate.

Worked examples

Ines compares formulas on ln x at 1.8

The exact derivative is 11.8\frac{1}{1.8}, or 0.5555556. Ines tries three step sizes and reads the errors.

ln x at x = 1.8
hForwardForward errorCentral error
0.10.54067221.5 × 10⁻²5.7 × 10⁻⁴
0.010.5540181.5 × 10⁻³5.7 × 10⁻⁶
0.0010.55540131.5 × 10⁻⁴5.7 × 10⁻⁸

Each cut of h by 10 cuts the forward error by about 10 and the central error by about 100, just as their orders predict.

Open this example in the tool

Tom watches the order show up on sin x

Tom has written his own solver and wants to be sure his formula has the order he claims. He tests sin x at 1, halving h from 0.1.

Errors for sin x at x = 1
hForwardCentralFive-point
0.14.3 × 10⁻²9 × 10⁻⁴1.8 × 10⁻⁶
0.052.1 × 10⁻²2.3 × 10⁻⁴1.1 × 10⁻⁷
0.0251.1 × 10⁻²5.6 × 10⁻⁵7 × 10⁻⁹
0.01255.3 × 10⁻³1.4 × 10⁻⁵4.4 × 10⁻¹⁰

From the first row to the second, the forward error shrinks 2.02 times, the central 4 times and the five-point 15.99 times. Those sit close to the 2, 4 and 16 that first, second and fourth order predict.

Open this example in the tool

Kai looks for the best step size for eˣ at 1

Kai expects smaller h to always win. The chart shows otherwise. The forward difference is best near h = 1 × 10⁻⁸, the central near 1 × 10⁻⁵ and the five-point near 1 × 10⁻³, each among the powers of 10 tested. Below those, the errors climb again.

A better formula also wants a larger h, since its own error drops so quickly that rounding catches up sooner.

Open this example in the tool

Reading your result

Why the error curve has a V shape. Two errors compete. The formula's own cutoff, called truncation error, falls as h shrinks. Rounding error rises, because f(x + h) and f(x) become nearly equal and subtracting them loses digits, and then you divide by a tiny h. Too large a step and truncation wins. Too small and rounding wins.

Where the bottom of the V sits. With machine precision ε, about 2.2 × 10⁻¹⁶, the best h is of order ε1/2\varepsilon^{1/2} for the forward difference and ε1/3\varepsilon^{1/3} for the central. Alexanderian's note works out why. That is about 10⁻⁸ and 10⁻⁵ to 10⁻⁶ when x and f have a size near 1. Scale h with x when x is much larger or smaller than that.

The second derivative is more delicate. It divides by h², so rounding hits it harder. For eˣ at 1, h = 0.01 gives an error of 2.3 × 10⁻⁵, and h = 10⁻⁶ gives 4.6 × 10⁻⁴. Stay with a larger h for f″ than you would use for f′.

Common mistakes

  • Making h as small as you can. Below the best step, each cut makes the estimate worse, and at h = 10⁻¹² a forward difference may keep only a few correct digits.
  • Trusting a difference at a corner. At x = 0, |x| has no derivative. The central difference still prints 0, since the two sides cancel, while forward says 1 and backward says −1. Disagreement like that is your warning sign.
  • Using one-sided differences when you could use both sides. Forward and backward are for the edge of a domain or data set, where one side is missing. Everywhere else, the central difference is far more accurate for almost the same work.
  • Using a tiny h on measured data. Noise plays the part of rounding error here, and it is far larger, so differences across close points mostly measure the noise.

Questions people ask

Central, in most cases, because it is second order and averages out some noise. It takes two points around each time, so you lose the first and last positions, and it slightly blurs sudden changes. A plain difference keeps more detail, but it also keeps more noise and sits half a step off in time.

Sources