Numerical Differentiation

Learning Objectives

  • Understand the derivation of numerical differentiation from Taylor series expansions.
  • Apply forward, backward, and centered finite difference formulas to approximate derivatives.
  • Evaluate the impact of truncation and round-off errors in numerical differentiation.
  • Apply techniques such as Richardson extrapolation and data smoothing to improve accuracy.
  • Describe advanced methods like complex step differentiation and automatic differentiation.

Numerical differentiation deals with approximating the derivative of a mathematical function or discrete data. It is fundamentally derived from Taylor series expansions.

Numerical Differentiation

A collection of algorithms for estimating the derivative of a mathematical function or function data using values of the function and perhaps other knowledge about the function.

Truncation Error

The error created by approximating a mathematical procedure, such as using a finite number of terms in a Taylor series expansion to represent an infinite sum.

Round-off Error

The error introduced by the fact that computers can only represent numbers with a finite number of significant digits, becoming particularly problematic when subtracting two nearly equal numbers.

Derivation from Taylor Series

Taylor Series

An infinite sum of terms that are expressed in terms of the function's derivatives at a single point, used to approximate complex functions.

The mathematical foundation for numerical differentiation is the Taylor series. By expanding f(xi+1)f(x_{i+1}) around xix_i, we get:

Taylor Series Expansion

Expands a function into an infinite sum of terms calculated from the values of its derivatives at a single point.

f(xi+1)=f(xi)+f(xi)h+f(xi)2!h2+O(h3)f(x_{i+1}) = f(x_i) + f'(x_i)h + \frac{f''(x_i)}{2!}h^2 + O(h^3)

Variables

SymbolDescriptionUnit
f(xi+1)f(x_{i+1})Function value at the next point-
f(xi)f(x_i)Function value at the current point-
f(xi)f'(x_i)First derivative of the function at the current point-
f(xi)f''(x_i)Second derivative of the function at the current point-
hhStep size between points-
O(h3)O(h^3)Truncation error of the order of h3h^3-

Solving for f(xi)f'(x_i) yields the finite divided difference formulas. The truncated terms represent the theoretical truncation error of the approximation.

Finite Divided Differences for the First Derivative

The simplest approximations of the first derivative are based on truncating the Taylor series after the first derivative term.

Forward Difference Formula

Approximates the first derivative using the current and next points. Truncation Error Et=f(xi)2h=O(h)E_t = -\frac{f''(x_i)}{2}h = O(h)

f(xi)f(xi+1)f(xi)hf'(x_i) \approx \frac{f(x_{i+1}) - f(x_i)}{h}

Variables

SymbolDescriptionUnit
f(xi)f'(x_i)Approximate first derivative-
f(xi+1)f(x_{i+1})Function value at the next point-
f(xi)f(x_i)Function value at the current point-
hhStep size-

Backward Difference Formula

Approximates the first derivative using the current and previous points. Truncation Error Et=f(xi)2h=O(h)E_t = \frac{f''(x_i)}{2}h = O(h)

f(xi)f(xi)f(xi1)hf'(x_i) \approx \frac{f(x_i) - f(x_{i-1})}{h}

Variables

SymbolDescriptionUnit
f(xi)f'(x_i)Approximate first derivative-
f(xi)f(x_i)Function value at the current point-
f(xi1)f(x_{i-1})Function value at the previous point-
hhStep size-

Centered Difference Formula

Approximates the first derivative using the next and previous points, offering higher accuracy. Truncation Error Et=f(xi)6h2=O(h2)E_t = -\frac{f'''(x_i)}{6}h^2 = O(h^2)

f(xi)f(xi+1)f(xi1)2hf'(x_i) \approx \frac{f(x_{i+1}) - f(x_{i-1})}{2h}

Variables

SymbolDescriptionUnit
f(xi)f'(x_i)Approximate first derivative-
f(xi+1)f(x_{i+1})Function value at the next point-
f(xi1)f(x_{i-1})Function value at the previous point-
hhStep size-

Centered Difference Accuracy

The centered difference formula is mathematically more accurate, possessing a truncation error of O(h2)O(h^2), compared to O(h)O(h) for the standard forward and backward differences. This is because the O(h2)O(h^2) term in the Taylor series exactly cancels out when subtracting the forward and backward expansions.

Formulas for Higher-Order Derivatives

By manipulating Taylor series expansions for multiple points (e.g., xi+1,xi1,xi+2x_{i+1}, x_{i-1}, x_{i+2}), we can derive formulas for higher-order derivatives. The centered difference formulas for the second and third derivatives are:

Second Derivative Centered Difference

Approximates the second derivative with an error of O(h2)O(h^2).

f(xi)f(xi+1)2f(xi)+f(xi1)h2f''(x_i) \approx \frac{f(x_{i+1}) - 2f(x_i) + f(x_{i-1})}{h^2}

Variables

SymbolDescriptionUnit
f(xi)f''(x_i)Approximate second derivative-
f(xi+1)f(x_{i+1})Function value at next point-
f(xi)f(x_i)Function value at current point-
f(xi1)f(x_{i-1})Function value at previous point-
hhStep size-

Third Derivative Centered Difference

Approximates the third derivative with an error of O(h2)O(h^2).

f(xi)f(xi+2)2f(xi+1)+2f(xi1)f(xi2)2h3f'''(x_i) \approx \frac{f(x_{i+2}) - 2f(x_{i+1}) + 2f(x_{i-1}) - f(x_{i-2})}{2h^3}

Variables

SymbolDescriptionUnit
f(xi)f'''(x_i)Approximate third derivative-
f(xi+2)f(x_{i+2})Function value at xi+2hx_i + 2h-
f(xi+1)f(x_{i+1})Function value at xi+hx_i + h-
f(xi1)f(x_{i-1})Function value at xihx_i - h-
f(xi2)f(x_{i-2})Function value at xi2hx_i - 2h-
hhStep size-

High-Accuracy Differentiation Formulas

Higher-accuracy formulas can be generated by including more terms from the Taylor series expansion. For example, a more accurate forward difference formula requires points xix_i, xi+1x_{i+1}, and xi+2x_{i+2}, achieving O(h2)O(h^2) without using a centered span.

Richardson Extrapolation

Richardson extrapolation is an elegant method to improve the accuracy of a derivative estimate by combining two less accurate estimates computed with different step sizes.

Generalized Richardson Extrapolation Concept

If D(h)D(h) is an approximation of order O(hp)O(h^p), we can combine estimates using step sizes h1h_1 and h2h_2 (where h2=h1/rh_2 = h_1/r, typically r=2r=2) to eliminate the leading error term and obtain a higher-order estimate O(hp+1)O(h^{p+1}).

Generalized Richardson Extrapolation

Increases the accuracy of a derivative estimate by combining estimates from different step sizes.

DD(h2)+D(h2)D(h1)rp1D \approx D(h_2) + \frac{D(h_2) - D(h_1)}{r^p - 1}

Variables

SymbolDescriptionUnit
DDHigher-order derivative estimate-
D(h1)D(h_1)Lower-order derivative estimate with step size h1h_1-
D(h2)D(h_2)Lower-order derivative estimate with step size h2h_2-
rrRatio of step sizes (h1/h2h_1 / h_2)-
ppOrder of the approximation error-

Classic Richardson Extrapolation for Centered Differences

Increases the accuracy from O(h2)O(h^2) to O(h4)O(h^4) when halving the step size (r=2r=2) for a centered difference (p=2p=2).

D43D(h2)13D(h1)D \approx \frac{4}{3}D(h_2) - \frac{1}{3}D(h_1)

Variables

SymbolDescriptionUnit
DDHigher-order derivative estimate (O(h4)O(h^4))-
D(h1)D(h_1)Lower-order centered difference estimate with step size h1h_1-
D(h2)D(h_2)Lower-order centered difference estimate with step size h2=h1/2h_2 = h_1 / 2-

Formulas for Unequally Spaced Data

In practice, experimental data points are often not evenly spaced. In this case, standard finite difference formulas cannot be applied. Instead, a Lagrange interpolating polynomial is fit through the adjacent points and then differentiated.

Unequally Spaced Differences

Computes the derivative at a point for three unequally spaced data points by differentiating a Lagrange interpolating polynomial.

f(xi)=hihi1(hi1+hi)f(xi1)+hihi1hi1hif(xi)+hi1hi(hi1+hi)f(xi+1)f'(x_i) = \frac{-h_i}{h_{i-1}(h_{i-1} + h_i)}f(x_{i-1}) + \frac{h_i - h_{i-1}}{h_{i-1}h_i}f(x_i) + \frac{h_{i-1}}{h_i(h_{i-1} + h_i)}f(x_{i+1})

Variables

SymbolDescriptionUnit
f(xi)f'(x_i)First derivative at the point xix_i-
xix_iCurrent data point-
xi1x_{i-1}Previous data point-
xi+1x_{i+1}Next data point-
hi1h_{i-1}Spacing between previous and current point (xixi1x_i - x_{i-1})-
hih_iSpacing between current and next point (xi+1xix_{i+1} - x_i)-

When hi1=hi=hh_{i-1} = h_i = h, this formula correctly collapses to the standard centered difference.

Errors in Numerical Differentiation and Condition Number

Numerical differentiation is inherently unstable and represents an ill-conditioned problem. While reducing the step size hh initially decreases the truncation error, it significantly magnifies the round-off error.

Condition Number

A measure of how sensitive a function's output is to small changes or errors in its input. In numerical differentiation, it scales proportionally to 1/h1/h.

Loss of Significance

The numerical calculation of a derivative requires subtracting two nearly equal function values, f(x+h)f(x)f(x+h) - f(x), and dividing by a very small number, hh. This process is highly susceptible to loss of significance (subtractive cancellation). The condition number for numerical differentiation approaches infinity as h0h \to 0, meaning the problem becomes infinitely sensitive to the finite precision limits of floating-point arithmetic. Thus, total error forms a "U-shaped" curve: decreasing hh beyond an optimal point causes the round-off error to dominate, drastically degrading accuracy.

Differentiating Raw Data

Because numerical differentiation amplifies error by a factor proportional to 1/h1/h, differentiating raw, scattered data directly often leads to useless results. The noise completely masks the true derivative.

Data Smoothing Before Differentiation

Because of the ill-conditioned nature of numerical differentiation, raw experimental data must almost always be smoothed before derivatives are taken.

Smoothing and Differentiating Data

  1. Visual Inspection: Plot the data to identify the level of noise and potential outliers.
  2. Smoothing or Regression: Apply a low-pass filter (like a moving average) or fit a smooth curve (like a low-order polynomial regression or a smoothing spline) to the data.
  3. Differentiation: Differentiate the fitted curve analytically, or apply numerical differentiation to the smoothed data points.

Partial Derivatives

For functions of multiple variables, partial derivatives are approximated by holding all other variables constant and applying the standard finite difference formulas to the variable of interest.

Advanced Differentiation Techniques

Beyond finite differences, modern engineering and machine learning heavily rely on computational differentiation techniques to avoid round-off errors and the need for analytical derivations.

Complex Step Differentiation

A method to compute exact derivatives of analytic functions using complex arithmetic, avoiding subtractive cancellation errors.

Complex Step Differentiation

Computes the derivative using a very small complex step ihih without subtracting function values.

f(x)Im(f(x+ih))hf'(x) \approx \frac{\text{Im}(f(x + ih))}{h}

Variables

SymbolDescriptionUnit
f(x)f'(x)Derivative of the function-
f(x+ih)f(x + ih)Function evaluated at a complex step-
Im()\text{Im}(\dots)Imaginary part of the complex number-
hhVery small step size (can be near machine epsilon)-

Since there is no subtraction in the numerator, hh can be chosen as small as machine epsilon (e.g., 101610^{-16}) to achieve near-exact precision without catastrophic cancellation.

Automatic Differentiation (AD)

A set of techniques to numerically evaluate the derivative of a function specified by a computer program by repeatedly applying the chain rule to elementary operations.

Automatic Differentiation (AD)

AD computes exact derivatives by systematically applying the chain rule to the elementary operations (addition, multiplication, trigonometric functions) that make up a computer program. It is neither symbolic differentiation (which produces massive equations) nor numerical differentiation (which suffers from truncation and round-off error). AD allows for exact gradient evaluations at the cost of one forward evaluation, which is the foundational technology behind modern deep learning frameworks.

Civil Engineering Applications

In civil engineering, numerical differentiation is often used when analyzing experimental or field data:

  • Structural Health Monitoring: Calculating velocity and acceleration by taking the first and second numerical derivatives of discrete position data collected from displacement sensors on a bridge.
  • Geotechnical Engineering: Estimating hydraulic gradients from discrete piezometer head readings across an earth dam to analyze seepage forces.

Truncation vs. Round-off Errors

The total error in numerical differentiation is the sum of truncation error (from ignoring higher-order Taylor series terms) and round-off error (from floating-point precision limits in computers).

  • Truncation Error: Decreases as step size hh decreases.
  • Round-off Error: Increases rapidly as step size hh decreases due to loss of significance when subtracting two nearly equal numbers. Because of this inverse relationship, there is always an optimal step size hh that minimizes total error; making hh arbitrarily small will ruin the calculation.
Key Takeaways
  • Finite difference formulas are directly derived from the Taylor series expansion.
  • Derivatives can be approximated using forward, backward, or centered finite divided differences. Truncation errors are derived from the unused Taylor series terms.
  • For unequally spaced data, the derivative is derived from differentiating a Lagrange interpolating polynomial over adjacent points.
  • Centered differences are generally more accurate (O(h2)O(h^2)) than forward or backward differences (O(h)O(h)) due to the cancellation of the O(h2)O(h^2) terms in the Taylor series.
  • Formulas for higher-order derivatives (2nd, 3rd) and high-accuracy formulas involve more neighboring data points.
  • Richardson extrapolation combines two estimates of lower accuracy to produce one of higher accuracy, with the generalized formula eliminating the leading error term.
  • Numerical differentiation is an inherently ill-conditioned problem because the condition number scales with 1/h1/h. It suffers from severe loss of significance (subtractive cancellation) when hh becomes too small.
  • Because of noise amplification, data smoothing or regression must almost always precede the numerical differentiation of experimental data.
  • Complex step differentiation avoids subtractive cancellation entirely, allowing extremely small step sizes.
  • Automatic Differentiation (AD) provides exact analytical derivatives numerically by programmatically applying the chain rule to atomic operations.