Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Numerical Differentiation and Integration

Numerical Integration: Trapezoid and Simpson Rules

Compute definite integrals with the midpoint, trapezoid and Simpson rules. Learn composite forms, error orders and how to test convergence in Python.

Many integrals have no closed form: integral of exp(-x^2), integral of sin(x)/x, or any integral of tabulated data. Numerical integration, also called quadrature, approximates a definite integral I = integral from a to b of f(x) dx by a weighted sum of function values.

The Newton-Cotes rules do this by interpolating f with a polynomial on equally spaced nodes and integrating the polynomial exactly. A degree-0 polynomial gives the midpoint rule, degree 1 the trapezoid rule, degree 2 Simpson's rule.

Midpoint and trapezoid rules

Split [a, b] into n panels of width h = (b - a) / n.

  • Midpoint: M = h * sum of f(midpoint of each panel). Error O(h^2).
  • Trapezoid: each panel becomes a trapezoid, T = h * (f(a)/2 + f(x1) + ... + f(x(n-1)) + f(b)/2). Error O(h^2).

The two errors have opposite signs and the midpoint error is about half the trapezoid error. For a convex function, the trapezoid rule overestimates and the midpoint rule underestimates.

Python Midpoint and trapezoid rules

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The trapezoid error is positive (overestimate, exp is convex), the midpoint error is negative and about half as large. Doubling n cuts both by roughly 4, confirming order two.

Simpson's rule

Simpson's rule fits a parabola through three consecutive nodes. On a double panel [x0, x2] with spacing h:

integral ~ h/3 * (f(x0) + 4 f(x1) + f(x2))

The composite rule needs an even number of panels n:

S = h/3 * (f0 + 4 f1 + 2 f2 + 4 f3 + ... + 4 f(n-1) + fn)

Its error is O(h^4), and a bonus: it is exact for cubic polynomials, not just quadratics, because the cubic error terms cancel by symmetry. Equivalently S = (4 T(h/2) - T(h)) / 3, which is the first step of Romberg extrapolation from the previous lesson.

flowchart LR
  A["Midpoint<br/>degree 0, O(h^2)"] --> B["Trapezoid<br/>degree 1, O(h^2)"]
  B --> C["Simpson 1/3<br/>degree 2, O(h^4)"]
  C --> D["Simpson 3/8, Boole<br/>higher degree"]
Python Simpson's rule and its order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The error ratio tends to 16, the signature of a fourth-order method. This ratio test is how you check any quadrature code: halve h and look at 2^p.

Exactness and degree

A rule has degree of precision d if it integrates every polynomial up to degree d exactly: midpoint and trapezoid have d = 1, Simpson's has d = 3. Higher-order Newton-Cotes rules (Simpson 3/8, Boole's rule) exist, but beyond a certain size their weights turn negative, which amplifies errors, for the same reason that high-degree interpolation misbehaves (see the Runge phenomenon). The practical recipe is to use a low-order rule composite over many panels.

Integrating tabulated data

When all you have is measurements at given x values, np.trapezoid (alias np.trapz in older NumPy) and scipy.integrate.simpson work directly on arrays and handle unequal spacing.

Python Integrating a table of values

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Estimating the error without the answer

You rarely know the exact integral. Compute T(h) and T(h/2); for a second-order rule the error of the finer one is about |T(h/2) - T(h)| / 3. For Simpson's rule use / 15. Keep halving until the estimated error is below your tolerance. This is also how adaptive methods decide where to refine, which we cover in Gaussian and adaptive quadrature.

Common mistakes

  • Using Simpson's rule with an odd number of panels.
  • Applying these rules across a discontinuity or singularity; split the interval at the trouble spot, or change variables.
  • Trusting a rule with too few panels because it "looks smooth".
  • Forgetting that the first and last points of the trapezoid rule get weight h/2.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the order of accuracy of the composite trapezoid rule?

    The error shrinks like h^2; doubling the number of panels reduces it by about 4.
  2. 2. Simpson's rule requires which of the following?

    Each parabola spans two panels, so the number of panels must be even.
  3. 3. If halving h divides the error of a rule by about 16, what is its order?

    Error ~ h^p implies ratio 2^p = 16, so p = 4.
  4. 4. Why are very high-order Newton-Cotes rules avoided?

    Negative weights cause cancellation and instability, like high-degree equispaced interpolation.

Summary

  • Newton-Cotes rules integrate an interpolating polynomial on equally spaced nodes.
  • Midpoint and trapezoid rules are O(h^2); Simpson's rule is O(h^4) and exact for cubics.
  • Test convergence by halving h and checking the error ratio 2^p.
  • Use composite low-order rules, not one high-order rule; split intervals at singularities.

Next: Gaussian and adaptive quadrature