Calculus & Analysis

Numerical Differentiation

Compare forward and central differences with the analytic derivative.

By Riano · Published · Updated · Report a correction

Numerical Differentiation

Concept Overview

Numerical differentiation estimates the derivative of a mathematical function using values from the function itself. When an analytical derivative is unavailable or difficult to compute, we use finite differences.

Worked Example: Finite Differences

Consider the cubic function f(x) = x3 at x = 1, with a step size of h = 0.5.

  1. Exact Derivative: The analytic derivative is f'(x) = 3x2. At x = 1, the exact derivative is 3(1)2 = 3.
  2. Forward Difference: The forward difference formula is (f(x + h) - f(x)) / h.
    f(1.5) = 3.375, f(1) = 1.
    (3.375 - 1) / 0.5 = 4.75.
    The signed error is 4.75 - 3 = 1.75.
  3. Central Difference: The central difference formula is (f(x + h) - f(x - h)) / (2h).
    f(1.5) = 3.375, f(0.5) = 0.125.
    (3.375 - 0.125) / 1 = 3.25.
    The signed error is 3.25 - 3 = 0.25.

In this example the central estimate is closer. For sufficiently smooth functions, the usual leading truncation terms are first order for the forward method and second order for the central method as h approaches zero. A leading coefficient can vanish, so this is not a universal finite-step ranking. The central chord joins (x − h, f(x − h)) to (x + h, f(x + h)); for this cubic example it does not pass through (x, f(x)).

Experiment: Compare Methods

Open the Numerical Differentiation module.

  1. Set Function to Cubic, Evaluation x to 1 and Step h to 0.5. Read derivative 3, forward 4.75 and central 3.25, with signed errors 1.75 and 0.25.
  2. Inspect the amber chord from x to x + h and the indigo chord between x − h and x + h. The tangent and forward chord pass through (x, f(x)); the central chord need not.
  3. Set Evaluation x to −1 and Step h to 0.25. Read forward 2.3125, central 3.0625 and derivative 3; the forward signed error is negative.
  4. Set Step h to 0. Its positive-step error replaces both finite estimates; the input is not clamped.
  5. Set Evaluation x to 0 and Step h to 1. Both finite estimates are 1 while the analytic derivative is 0.
  6. Set Evaluation x to 2, keeping Step h = 1. The stencil reaches x = 3; read forward 19, central 13 and derivative 12 without clipping that sampled input to 2.
  7. Choose Function = Sine, Evaluation x = 0 and Step h = 0.5. Both estimates are approximately 0.95885107721, below derivative 1. Angles are radians.
  8. Return to Cubic, Evaluation x = 1 and Step h = 0.001. Inspect the small signed errors. Pan and choose Reset view; inputs and results remain unchanged.

Limitations

While decreasing the step size h improves the theoretical truncation error, choosing an h that is too small leads to subtractive cancellation (round-off error) due to the limits of double-precision floating-point arithmetic. The module uses native double arithmetic, meaning that the error does not monotonically improve for arbitrarily small h values. Both error-order claims require sufficient smoothness near the complete stencil. All stencil points are evaluated even outside the main x control interval. Sine arguments use radians, readouts are rounded and the view rescales with the stencil; changing pixel angles alone is not an error comparison.

References

The following reference provides the mathematical foundation and error analysis for finite difference formulas used in this module.

Experience it interactively

Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Numerical Differentiation module.

Try Numerical Differentiation on Riano →

More in Calculus & Analysis