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 around , 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.
Variables
| Symbol | Description | Unit |
|---|---|---|
| Function value at the next point | - | |
| Function value at the current point | - | |
| First derivative of the function at the current point | - | |
| Second derivative of the function at the current point | - | |
| Step size between points | - | |
| Truncation error of the order of | - |
Solving for 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
Variables
| Symbol | Description | Unit |
|---|---|---|
| Approximate first derivative | - | |
| Function value at the next point | - | |
| Function value at the current point | - | |
| Step size | - |
Backward Difference Formula
Approximates the first derivative using the current and previous points. Truncation Error
Variables
| Symbol | Description | Unit |
|---|---|---|
| Approximate first derivative | - | |
| Function value at the current point | - | |
| Function value at the previous point | - | |
| Step size | - |
Centered Difference Formula
Approximates the first derivative using the next and previous points, offering higher accuracy. Truncation Error
Variables
| Symbol | Description | Unit |
|---|---|---|
| Approximate first derivative | - | |
| Function value at the next point | - | |
| Function value at the previous point | - | |
| Step size | - |
Centered Difference Accuracy
The centered difference formula is mathematically more accurate, possessing a truncation error of , compared to for the standard forward and backward differences. This is because the 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., ), 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 .
Variables
| Symbol | Description | Unit |
|---|---|---|
| Approximate second derivative | - | |
| Function value at next point | - | |
| Function value at current point | - | |
| Function value at previous point | - | |
| Step size | - |
Third Derivative Centered Difference
Approximates the third derivative with an error of .
Variables
| Symbol | Description | Unit |
|---|---|---|
| Approximate third derivative | - | |
| Function value at | - | |
| Function value at | - | |
| Function value at | - | |
| Function value at | - | |
| Step 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 , , and , achieving 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 is an approximation of order , we can combine estimates using step sizes and (where , typically ) to eliminate the leading error term and obtain a higher-order estimate .
Generalized Richardson Extrapolation
Increases the accuracy of a derivative estimate by combining estimates from different step sizes.
Variables
| Symbol | Description | Unit |
|---|---|---|
| Higher-order derivative estimate | - | |
| Lower-order derivative estimate with step size | - | |
| Lower-order derivative estimate with step size | - | |
| Ratio of step sizes () | - | |
| Order of the approximation error | - |
Classic Richardson Extrapolation for Centered Differences
Increases the accuracy from to when halving the step size () for a centered difference ().
Variables
| Symbol | Description | Unit |
|---|---|---|
| Higher-order derivative estimate () | - | |
| Lower-order centered difference estimate with step size | - | |
| Lower-order centered difference estimate with step size | - |
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.
Variables
| Symbol | Description | Unit |
|---|---|---|
| First derivative at the point | - | |
| Current data point | - | |
| Previous data point | - | |
| Next data point | - | |
| Spacing between previous and current point () | - | |
| Spacing between current and next point () | - |
When , 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 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 .
Loss of Significance
The numerical calculation of a derivative requires subtracting two nearly equal function values, , and dividing by a very small number, . This process is highly susceptible to loss of significance (subtractive cancellation). The condition number for numerical differentiation approaches infinity as , meaning the problem becomes infinitely sensitive to the finite precision limits of floating-point arithmetic. Thus, total error forms a "U-shaped" curve: decreasing 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 , 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
- Visual Inspection: Plot the data to identify the level of noise and potential outliers.
- 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.
- 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 without subtracting function values.
Variables
| Symbol | Description | Unit |
|---|---|---|
| Derivative of the function | - | |
| Function evaluated at a complex step | - | |
| Imaginary part of the complex number | - | |
| Very small step size (can be near machine epsilon) | - |
Since there is no subtraction in the numerator, can be chosen as small as machine epsilon (e.g., ) 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 decreases.
- Round-off Error: Increases rapidly as step size decreases due to loss of significance when subtracting two nearly equal numbers. Because of this inverse relationship, there is always an optimal step size that minimizes total error; making arbitrarily small will ruin the calculation.
- 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 () than forward or backward differences () due to the cancellation of the 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 . It suffers from severe loss of significance (subtractive cancellation) when 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.