Mechanical Engineering, Robotics & Workplace Automation

Numerical Integration and Differentiation

Derivatives and integrals from samples: forward and central differences and why shrinking h too far backfires, the trapezoidal and Simpson's rules and their error orders, and SciPy's quad, trapezoid, and simpson.

  • 6 min
  • 4 steps
  • 3 questions
  • Lesson 29 of 78

In this lesson

  1. From calculus to the computer
  2. Numerical differentiation with finite differences
  3. Numerical integration (quadrature)
  4. Choosing a method
Numerical Calculus

From calculus to the computer

Calculus gives clean rules for derivatives and definite integrals, but those rules need a formula to differentiate or integrate. On a computer you often have something less tidy: a function you can only evaluate at points, or a set of measured samples with no formula at all. Numerical methods recover the derivative and the integral from those evaluations alone. The price is that every result is an approximation, and the central skill is understanding how large that approximation error is.

Numerical differentiation with finite differences

The derivative is a limit of slopes, so the simplest estimate just stops taking the limit and uses a small but finite step \(h\). The forward difference is

\[f'(x) \approx \frac{f(x+h) - f(x)}{h}.\]

A Taylor expansion shows its error shrinks in proportion to \(h\): it is first-order accurate, written \(O(h)\). Halving \(h\) only halves the error, which is slow. A smarter choice samples symmetrically on both sides — the central difference:

\[f'(x) \approx \frac{f(x+h) - f(x-h)}{2h} \quad (O(h^2)).\]

Here the first-order terms cancel, leaving error proportional to \(h^2\): halving \(h\) cuts the error by four. For the same effort, the central difference is far more accurate, so prefer it whenever you can evaluate \(f\) on both sides.

It is tempting to drive \(h\) toward zero, but that fails. As shown in the floating-point lesson, subtracting two nearly equal numbers like \(f(x+h)\) and \(f(x-h)\) loses significant digits to round-off error, and dividing by a tiny \(h\) amplifies it. So two errors pull in opposite directions: truncation error falls as \(h\) shrinks, while round-off error grows. Their sum is minimized at an intermediate optimal \(h\) — for central differences, typically near the cube root of machine epsilon (roughly \(10^{-5}\) in double precision), not the smallest \(h\) you can represent.

For sampled data, NumPy’s numpy.gradient applies central differences in the interior and one-sided differences at the endpoints, returning a derivative array the same length as the input:

import numpy as np

x = np.linspace(0, np.pi, 100)
y = np.sin(x)
dydx = np.gradient(y, x)   # approximates cos(x)
Log-log plot of the error in the finite-difference derivative of sin x at x = 1 versus step size: noisy round-off error falls as h grows from 1e-15, reaching a minimum near 1e-8 for the forward difference and near 6e-6 for the central difference, then truncation error rises along straight lines.
Truncation error falls with h while round-off rises; the best step sits in between. Credit: StudyCorner chart · CC BY 4.0 · Source

Quick check

Halving h in a central difference does what to the truncation error?

Quick check

Why not use h = 1e-15 for a finite-difference derivative?

Numerical integration (quadrature)

Estimating a definite integral from samples is called quadrature. The idea is to replace the curve with shapes whose area is easy to compute. Split \([a, b]\) into \(n\) equal panels of width \(h\), with sample values \(f_0, f_1, \dots, f_n\).

The trapezoidal rule connects neighboring samples with straight lines and sums the trapezoids:

\[\int_a^b f(x)\,dx \approx h\left[\tfrac12 f_0 + f_1 + \cdots + f_{n-1} + \tfrac12 f_n\right].\]

Its error is \(O(h^2)\), and it is the safe default — robust, and it makes no assumption beyond having the samples. Simpson’s rule does better by fitting a parabola through each pair of panels instead of a line. That extra curvature captures smooth functions far more accurately, with error \(O(h^4)\), so on a smooth integrand it reaches a given accuracy with many fewer points. (Simpson’s rule needs an even number of panels.) Use Simpson’s rule when the function is smooth and you want accuracy; fall back to the trapezoidal rule for noisy data or when smoothness is not guaranteed.

Why does adding up little areas recover the integral at all? The Fundamental Theorem of Calculus says the definite integral is the net signed area under the curve 1, and that area is exactly what these rules approximate.

SciPy’s scipy.integrate module covers both situations 2. When you have a callable function and want a single accurate number, quad performs adaptive quadrature, placing evaluations where the function is most curved and returning both the estimate and an error bound. When you only have sampled data, use trapezoid or simpson, which apply the rules above to your arrays:

import numpy as np
from scipy.integrate import quad, trapezoid, simpson

# A function we can evaluate anywhere: integral of sin from 0 to pi is 2.
value, err = quad(np.sin, 0, np.pi)     # value ~ 2.0, err is a bound

# The same integral from fixed samples (no formula needed downstream):
x = np.linspace(0, np.pi, 101)
y = np.sin(x)
approx_trap = trapezoid(y, x)           # ~ 1.9998
approx_simp = simpson(y, x)             # ~ 2.0000, more accurate

For the smooth sine curve, Simpson’s result lands closer to the exact value of 2 than the trapezoidal one at the same sample count — the higher-order rule earning its keep.

Log-log plot of the error in integrating sin x from 0 to pi versus number of panels: the trapezoidal rule falls with slope 2 and Simpson's rule with slope 4, reaching about 1e-11 at 512 panels.
Doubling the panels: trapezoid error ÷ 4, Simpson error ÷ 16. Credit: StudyCorner chart · CC BY 4.0 · Source

Quick check

On a smooth function, doubling the panels in Simpson’s rule cuts the error by about what?

Choosing a method

Reach for quad when you can evaluate the function and want one trustworthy number with an error estimate. Use trapezoid or simpson when your data arrives as samples — for example, sensor readings or the output of another computation. For derivatives, prefer central differences and numpy.gradient, and resist shrinking \(h\) past the point where round-off takes over. In every case the discipline is the same: pick the rule that matches your data, then keep an eye on the error you are willing to accept.

Lesson complete

Nice work.

1day streak
0/1today's goal
–correct

Up next · 3 min

Solving ODEs Numerically

Next lesson
Sources for this lesson
  1. 1
    Calculus, Volumes 1–3. OpenStax (Rice University). verifiedOpen (CC BY-NC-SA) calculus text. Vol 1 functions/limits/derivatives/integrals; Vol 2 integration/series; Vol 3 multivariable and vector calculus. Cited at: Vol 1.
  2. 2
    SciPy Documentation. SciPy Developers. verifiedReference for numerical routines — root finding, integration, ODE solvers. Cited at: integrate.