Numerical Differentiation

Written by

in

Numerical differentiation estimates a rate of change from sampled values. It is useful when an engineering measurement gives positions, temperatures, or voltages at discrete times rather than a complete formula.

Three first-derivative formulas

For equally spaced samples with spacing h:

  • Forward: f′(x) ≈ [f(x + h) − f(x)] / h.
  • Backward: f′(x) ≈ [f(x) − f(x − h)] / h.
  • Central: f′(x) ≈ [f(x + h) − f(x − h)] / (2h).

Forward and backward differences have truncation error of order O(h), assuming a sufficiently smooth function. Central differences have order O(h²). The central formula needs a sample on both sides, so the first and last points often need a different approximation.

Worked example: compare estimates

Let f(x) = x², x = 2, and h = 0.1. The exact derivative is f′(2) = 4. The three samples are f(1.9) = 3.61, f(2) = 4, and f(2.1) = 4.41.

  1. Forward: (4.41 − 4) / 0.1 = 4.1.
  2. Backward: (4 − 3.61) / 0.1 = 3.9.
  3. Central: (4.41 − 3.61) / 0.2 = 4.0.

The central answer is exact here because the function is quadratic and higher odd derivatives vanish. It is not exact for every function.

Worked example: velocity from position samples

A position sensor records 1.21 m at 1.1 s, 1.44 m at 1.2 s, and 1.69 m at 1.3 s. The central estimate at 1.2 s is (1.69 − 1.21) / (1.3 − 1.1) = 2.4 m/s. The denominator is 0.2 s, not the single-step spacing of 0.1 s.

For the second derivative, use [f(x + h) − 2f(x) + f(x − h)] / h². These samples give (1.69 − 2 × 1.44 + 1.21) / 0.1² = 2 m/s².

Why smaller spacing is not always better

Reducing h can reduce truncation error for smooth exact data. But differentiation subtracts nearby values and divides by h; measurement noise and floating-point roundoff can then dominate. Second derivatives divide by h² and are especially sensitive.

Check sensor resolution, repeatability, and sampling intervals before trusting an estimate. A smoothing method changes the data and may hide fast behaviour, so document its settings and check that it suits the signal.

A small Python check

def central_difference(f_minus, f_plus, h):
    if h <= 0:
        raise ValueError("h must be positive")
    return (f_plus - f_minus) / (2 * h)

velocity = central_difference(1.21, 1.69, 0.1)
print(velocity)  # approximately 2.4

This function assumes equally spaced samples and a positive spacing. Do not use it unchanged for uneven timestamps. Retain full numerical precision during the calculation and round the final result to a precision justified by the measurements.

Practice with an answer

For f(x) = x³, estimate f′(2) using h = 0.1. The samples are 6.859, 8, and 9.261.

Show the worked answer

Forward = 12.61; backward = 11.41; central = 12.01. The exact derivative is 3x² = 12. The central error is 0.01; it decreases to 0.0025 when h is halved to 0.05, consistent with second-order truncation error for this example.

Choose the method for the available data

  • Use a one-sided estimate when only one neighbouring sample is available.
  • Use the central estimate for an interior point with uniform spacing.
  • Check timestamps and units before applying a formula.
  • Compare against a known function or controlled measurement before using the result in an engineering decision.

Continue with difference approximations, Numerical Integration, or the differential-equation worked examples.

Report an issue with this lesson or article