Numerical Differentiation
Numerical differentiation estimates a derivative from function values instead of a formula. The forward difference \dfrac{f(x+h)-f(x)}{h} and the backward difference \dfrac{f(x)-f(x-h)}{h} have error proportional to h, while the central difference \dfrac{f(x+h)-f(x-h)}{2h} has error proportional to h², so it is usually far more accurate.
Before this lesson: Definition of the Derivative, Taylor Polynomials and Floating-Point Arithmetic
The idea: a secant slope stands in for the tangent slope
The definition of the derivative says f′(x) is the limit of the difference quotient as h goes to 0. A computer cannot take that limit, but it can stop at a small h. The slope of a secant line through two nearby points is then an estimate of the slope of the tangent line.
This is what you do whenever the function is known only through data, such as positions from a GPS or readings from a sensor, or when the formula is too messy to differentiate by hand. The question is which two points to use and how small to make h.
The graph shows three choices for f(x) = x³ at x = 2 with a large step, h = 0.5. The forward secant looks ahead, the backward secant looks behind, and the central secant uses one point on each side. The central secant is almost parallel to the tangent line, even with this large step.
Numerical differentiation formulas
Let h > 0 be the step size. The three basic estimates of f′(x) are:
| Formula | Estimate of f′(x) | Error | Order |
|---|---|---|---|
| Forward difference | \dfrac{f(x+h)-f(x)}{h} | -\dfrac{h}{2}f''(\xi) | O(h) |
| Backward difference | \dfrac{f(x)-f(x-h)}{h} | \dfrac{h}{2}f''(\xi) | O(h) |
| Central difference | \dfrac{f(x+h)-f(x-h)}{2h} | -\dfrac{h^2}{6}f'''(\xi) | O(h^2) |
Here ξ is some number between the points used, and the error is the exact value minus the estimate. A method is order p, written O(hᵖ), if its error shrinks like hᵖ: halving h divides the error by about 2ᵖ. So halving h halves the error of a forward difference but cuts the error of a central difference to about one fourth.
The same idea gives the central difference for the second derivative:
f''(x) \approx \dfrac{f(x+h) - 2f(x) + f(x-h)}{h^2}, \qquad \text{error } -\dfrac{h^2}{12}f^{(4)}(\xi)Where the errors come from
Write the Taylor polynomials of f about x for a step forward and a step back:
f(x \pm h) = f(x) \pm hf'(x) + \dfrac{h^2}{2}f''(x) \pm \dfrac{h^3}{6}f'''(x) + \dfrac{h^4}{24}f^{(4)}(x) \pm \cdotsSolving the plus version for f′(x) leaves an h/2 · f″ term, which is why the forward difference is first order. Subtracting the minus version from the plus version cancels the f and f″ terms, so the central difference error starts with h²/6 · f‴. Adding the two versions cancels the odd terms, which gives the second-derivative formula.
How to estimate a derivative numerically
- Pick a formula: central difference at interior points, forward or backward difference at the ends of the data.
- Find the needed function values at x, x + h and x − h.
- Subtract, then divide by h, 2h or h².
- If you can, repeat with h/2 and compare. The change shows the size of the error.
- Do not make h too small: round-off error grows as h shrinks (Example 5).
Numerical differentiation examples
Example 1: Three estimates of one derivative
Estimate f′(2) for f(x) = x³ with h = 0.1 using the forward, backward and central differences, and find each error. The exact value is f′(2) = 3(2)² = 12.
f(2.1) = 9.261,\quad f(2) = 8,\quad f(1.9) = 6.859
Find the function values.
\dfrac{9.261 - 8}{0.1} = \dfrac{1.261}{0.1} = 12.61Forward difference.
\dfrac{8 - 6.859}{0.1} = \dfrac{1.141}{0.1} = 11.41Backward difference.
\dfrac{9.261 - 6.859}{0.2} = \dfrac{2.402}{0.2} = 12.01Central difference.
12 - 12.61 = -0.61,\quad 12 - 11.41 = 0.59,\quad 12 - 12.01 = -0.01
Errors: exact minus estimate.
-\dfrac{(0.1)^2}{6}\cdot 6 = -0.01Check the central error with the formula; f‴(x) = 6 for every x.
Answer: forward 12.61, backward 11.41, central 12.01. The central difference is about 60 times more accurate, and for a cubic its error formula is exact.
Example 2: Halving the step shows the order
For f(x) = eˣ at x = 0, where f′(0) = 1, compare the forward and central differences with h = 0.2 and h = 0.1. Use function values rounded to 6 decimal places.
e^{0.2} = 1.221403,\quad e^{-0.2} = 0.818731Values for h = 0.2.
e^{0.1} = 1.105171,\quad e^{-0.1} = 0.904837Values for h = 0.1.
\dfrac{1.221403 - 1}{0.2} = 1.107015Forward, h = 0.2: error −0.107015.
\dfrac{1.105171 - 1}{0.1} = 1.05171Forward, h = 0.1: error −0.05171.
\dfrac{1.221403 - 0.818731}{0.4} = \dfrac{0.402672}{0.4} = 1.00668Central, h = 0.2: error −0.00668.
\dfrac{1.105171 - 0.904837}{0.2} = \dfrac{0.200334}{0.2} = 1.00167Central, h = 0.1: error −0.00167.
\dfrac{0.107015}{0.05171} \approx 2.07,\quad \dfrac{0.00668}{0.00167} = 4Divide each error at h = 0.2 by the error at h = 0.1.
Answer: halving h cut the forward error by a factor of about 2 (first order) and the central error by a factor of 4 (second order), just as the error table predicts. Compare order of convergence, which measures speed the same way for iterations.
Example 3: A second derivative from three values
Estimate f′(1) and f″(1) for f(x) = 1/x with h = 0.1. The exact values are f′(1) = −1 and f″(1) = 2.
f(1.1) = \dfrac{1}{1.1} \approx 0.909091,\quad f(0.9) = \dfrac{1}{0.9} \approx 1.111111Find the function values; f(1) = 1.
\dfrac{0.909091 - 1.111111}{0.2} = \dfrac{-0.20202}{0.2} = -1.0101Central difference for f′(1).
0.909091 - 2(1) + 1.111111 = 0.020202
Numerator of the second-derivative formula.
\dfrac{0.020202}{(0.1)^2} = \dfrac{0.020202}{0.01} = 2.0202Divide by h² = 0.01.
-1 - (-1.0101) = 0.0101,\quad 2 - 2.0202 = -0.0202
Errors.
Answer: f′(1) ≈ −1.0101 and f″(1) ≈ 2.0202, both off by about 1%. The error formulas predict about −(0.01/6)(−6) = 0.01 and −(0.01/12)(24) = −0.02, since f‴(1) = −6 and f⁽⁴⁾(1) = 24.
Example 4: Velocity and acceleration from data (word problem)
A drone climbs straight up. Its altitude is logged every 0.5 seconds:
| t (s) | 0 | 0.5 | 1.0 | 1.5 | 2.0 |
|---|---|---|---|---|---|
| altitude (m) | 0 | 1.8 | 4.1 | 6.9 | 10.2 |
Estimate the velocity at t = 0, 1.0 and 2.0 s and the acceleration at t = 1.0 s. Velocity is the derivative of position and acceleration is the second derivative (see position, velocity and acceleration).
v(1.0) \approx \dfrac{6.9 - 1.8}{2(0.5)} = \dfrac{5.1}{1} = 5.1Central difference at an interior time, h = 0.5.
v(0) \approx \dfrac{1.8 - 0}{0.5} = 3.6Forward difference at the first time.
v(2.0) \approx \dfrac{10.2 - 6.9}{0.5} = \dfrac{3.3}{0.5} = 6.6Backward difference at the last time.
6.9 - 2(4.1) + 1.8 = 0.5
Numerator for the second derivative at t = 1.0.
a(1.0) \approx \dfrac{0.5}{(0.5)^2} = \dfrac{0.5}{0.25} = 2Divide by h² = 0.25.
Answer: about 3.6 m/s at the start, 5.1 m/s at 1 second and 6.6 m/s at 2 seconds, with an acceleration of about 2 m/s² at 1 second. The end values are only first-order accurate; the three-point formulas in Related methods do better there.
Example 5: When a smaller step makes things worse
The values of f(x) = sin x below are rounded to 4 decimal places. Estimate f′(1) = cos 1 ≈ 0.540302 with the central difference for h = 0.1 and h = 0.001, and explain the result.
\sin 1.1 \approx 0.8912,\quad \sin 0.9 \approx 0.7833
Rounded values for h = 0.1.
\dfrac{0.8912 - 0.7833}{0.2} = \dfrac{0.1079}{0.2} = 0.5395Central difference: error about 0.0008.
\sin 1.001 \approx 0.8420,\quad \sin 0.999 \approx 0.8409
Rounded values for h = 0.001.
\dfrac{0.8420 - 0.8409}{0.002} = \dfrac{0.0011}{0.002} = 0.55Central difference: error about 0.0097.
\dfrac{2(0.00005)}{0.002} = 0.05Largest round-off effect: each value can be off by 0.00005, and the difference is divided by 2h.
Answer: the smaller step gave the worse estimate. Subtracting two nearly equal rounded values is a loss of significance: the round-off error ε in each value is divided by 2h, so it grows as h shrinks, while the truncation error shrinks.
The total error of the central difference is bounded by about E(h) = Mh²/6 + ε/h, where M bounds |f‴|. Setting E′(h) = Mh/3 − ε/h² = 0 gives the best step h = (3ε/M)^(1/3). Here ε = 0.00005 and M = 1, so the best step is about 0.053.
Common mistakes
| Mistake | Example of the error | Fix |
|---|---|---|
| Dividing the central difference by h | \dfrac{9.261 - 6.859}{0.1} = 24.02 | The two points are 2h apart, so divide by 2h: 12.01. |
| Forgetting to square h in the second derivative | \dfrac{0.020202}{0.1} = 0.20202 | Divide by h² = 0.01 to get 2.0202. |
| Making h as small as possible | Using h = 0.001 with 4-decimal data | Round-off grows like ε/h; use h near (3ε/M)^(1/3). |
| Using a central difference at the end of the data | Needing a value at t = 2.5 s that was never measured | Use a forward or backward (or three-point one-sided) formula at the ends. |
| Mixing up the sign of the backward difference | \dfrac{f(x-h) - f(x)}{h} | The later value comes first: f(x) − f(x − h). |
Related methods
These difference formulas are the building blocks of the finite difference method, which turns a boundary value problem into a system of linear equations.
Three-point one-sided formulas
At the ends of a data set, these second-order formulas use three points on one side:
f'(x) \approx \dfrac{-3f(x) + 4f(x+h) - f(x+2h)}{2h}, \qquad f'(x) \approx \dfrac{3f(x) - 4f(x-h) + f(x-2h)}{2h}For the drone in Example 4, the forward three-point formula gives v(0) ≈ (−0 + 4(1.8) − 4.1)/1 = 3.1 m/s, and the backward one gives v(2.0) ≈ (3(10.2) − 4(6.9) + 4.1)/1 = 7.1 m/s.
Better accuracy without a tiny h
Because the central difference error is about Ch², two estimates with steps h and h/2 can be combined to cancel that term. This is the next lesson, Richardson Extrapolation. For noisy data, it often works better to fit a smooth curve first, such as cubic splines or a regression curve, and then differentiate the fit.
Practice problems
- Estimate f′(3) for f(x) = x² with the forward difference and h = 0.1. What is the error?
- Repeat Problem 1 with the central difference. Why is the answer exact?
- Estimate f′(4) for f(x) = √x with the central difference and h = 0.1, using values rounded to 6 decimal places.
- Estimate f′(2) for f(x) = ln x with the backward difference and h = 0.1, using values rounded to 6 decimal places.
- Estimate f″(1) for f(x) = x⁴ with h = 0.1, and check the error with the error formula.
- A table gives f(1.0) = 2.00, f(1.2) = 2.50 and f(1.4) = 3.12. Estimate f′(1.2) and f″(1.2).
- A forward difference has error 0.08 with h = 0.2, and a central difference has error 0.012 with h = 0.2. Predict each error with h = 0.1.
- A thermometer reads 20 °C, 26 °C and 30 °C at t = 0, 1 and 2 hours. Estimate the rate of change at t = 2 with the backward difference and with the three-point backward formula.
- Function values carry round-off errors up to ε = 10⁻⁶, and |f‴(x)| ≤ 3. Find the best step h for the central difference.
- A tank holds 800 L, 740 L and 690 L at t = 0, 5 and 10 minutes. Estimate how fast the volume is changing at t = 5 minutes.
Show answers
1.
f(3.1) = 9.61,\quad f(3) = 9
Find the function values.
\dfrac{9.61 - 9}{0.1} = \dfrac{0.61}{0.1} = 6.1Forward difference.
6 - 6.1 = -0.1
Error, since f′(3) = 6.
Answer: 6.1, with error −0.1
2.
f(2.9) = 8.41
Find the value one step back.
\dfrac{9.61 - 8.41}{0.2} = \dfrac{1.2}{0.2} = 6Central difference.
Answer: 6, exact, because the error depends on f‴, and f‴(x) = 0 for a quadratic
3.
\sqrt{4.1} \approx 2.024846,\quad \sqrt{3.9} \approx 1.974842Rounded values.
\dfrac{2.024846 - 1.974842}{0.2} = \dfrac{0.050004}{0.2} = 0.25002Central difference.
Answer: about 0.25002 (exact value 1/4)
4.
\ln 2 \approx 0.693147,\quad \ln 1.9 \approx 0.641854
Rounded values.
\dfrac{0.693147 - 0.641854}{0.1} = \dfrac{0.051293}{0.1} = 0.51293Backward difference.
Answer: about 0.51293 (exact value 0.5)
5.
f(1.1) = 1.4641,\quad f(0.9) = 0.6561
Find the function values; f(1) = 1.
1.4641 - 2(1) + 0.6561 = 0.1202
Numerator.
\dfrac{0.1202}{0.01} = 12.02Divide by h² = 0.01.
-\dfrac{(0.1)^2}{12}\cdot 24 = -0.02Error formula, with f⁽⁴⁾(x) = 24.
12 - 12.02 = -0.02
Actual error, since f″(1) = 12.
Answer: 12.02; the error −0.02 matches the formula exactly
6.
\dfrac{3.12 - 2.00}{0.4} = \dfrac{1.12}{0.4} = 2.8Central difference with h = 0.2.
3.12 - 2(2.50) + 2.00 = 0.12
Second-derivative numerator.
\dfrac{0.12}{0.04} = 3Divide by h² = 0.04.
Answer: f′(1.2) ≈ 2.8 and f″(1.2) ≈ 3
7.
\dfrac{0.08}{2} = 0.04Forward difference is first order: halve the error.
\dfrac{0.012}{4} = 0.003Central difference is second order: divide by 4.
Answer: about 0.04 and about 0.003
8.
\dfrac{30 - 26}{1} = 4Backward difference.
\dfrac{3(30) - 4(26) + 20}{2(1)} = \dfrac{90 - 104 + 20}{2} = 3Three-point backward formula.
Answer: 4 °C per hour (first order) and 3 °C per hour (second order)
9.
h = \left(\dfrac{3\varepsilon}{M}\right)^{1/3} = \left(\dfrac{3 \times 10^{-6}}{3}\right)^{1/3}Substitute ε = 10⁻⁶ and M = 3.
= (10^{-6})^{1/3} = 0.01Simplify.
Answer: h ≈ 0.01
10.
\dfrac{690 - 800}{2(5)} = \dfrac{-110}{10} = -11Central difference with h = 5.
Answer: about −11 L per minute; the tank is draining at about 11 L per minute
Numerical differentiation FAQ
What is numerical differentiation?
It is estimating a derivative from function values at nearby points, using difference formulas such as the forward, backward and central differences. It is used when f is known only through data or is hard to differentiate by hand.
Why is the central difference more accurate?
Its points sit symmetrically around x, so the h² terms of the two Taylor expansions cancel. Its error is proportional to h² instead of h.
How do I choose the step size h?
Make h small enough that the truncation error is small, but not so small that round-off takes over. For the central difference, h ≈ (3ε/M)^(1/3) balances the two; in double precision that is often around 10⁻⁵.
What is the difference between forward, backward and central differences?
A forward difference uses x and x + h, a backward difference uses x − h and x, and a central difference uses x − h and x + h. The first two are first order, and the central difference is second order.
Summary
- Forward and backward differences, [f(x + h) − f(x)]/h and [f(x) − f(x − h)]/h, are first order: error about proportional to h.
- The central difference [f(x + h) − f(x − h)]/(2h) is second order: error about proportional to h².
- f″(x) ≈ [f(x + h) − 2f(x) + f(x − h)]/h², also second order.
- Use central differences inside a data set and one-sided formulas at the ends.
- Round-off error grows like ε/h, so the best h balances truncation and round-off.
Previous lesson: Cubic Splines
Next lesson: Richardson Extrapolation
Back to the Numerical Methods Index