Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 39 · Applications of Integration

Numerical Integration

Approximate integrals that have no simple antiderivative using the trapezoid rule and Simpson's rule, compare their errors and test them in code.

The Fundamental Theorem of Calculus needs an antiderivative. But many important functions do not have one that can be written with ordinary functions. The bell-curve integral ∫ e^(-x²) dx is a famous example. Sometimes you only have a table of measured data, not a formula. In these cases we approximate the integral numerically, and with good methods we can make the error as small as we like.

From rectangles to trapezoids

In the Riemann-sum lesson you used rectangles. A better idea is to join the curve's points on each piece with a straight line, forming a trapezoid. Divide [a, b] into n pieces of width h = (b - a)/n, with points x₀ = a, x₁ = a + h, ..., xₙ = b. The trapezoid rule is:

Text
T = (h/2) · [ f(x₀) + 2f(x₁) + 2f(x₂) + ... + 2f(xₙ₋₁) + f(xₙ) ]

Each trapezoid has area (h/2)(f(xᵢ) + f(xᵢ₊₁)). Interior points are shared by two trapezoids, so they get a factor of 2; the two endpoints are used once.

Simpson's rule

Simpson's rule fits a parabola through each group of three points, which follows a curve much more closely than a straight line. It needs an even number n of pieces:

Text
S = (h/3) · [ f(x₀) + 4f(x₁) + 2f(x₂) + 4f(x₃) + ... + 4f(xₙ₋₁) + f(xₙ) ]

The weights alternate 4, 2, 4, 2, ..., 4 between the endpoints, which have weight 1.

Simpson's rule is exact for polynomials up to degree 3, even though it uses only parabolas. That is a lucky cancellation.

Python Trapezoid and Simpson from scratch

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.

Run it and compare the columns. Doubling n makes the trapezoid error shrink by about a factor of 4, while Simpson's error shrinks by about a factor of 16. In general the trapezoid error is proportional to h² and Simpson's is proportional to h⁴ (for smooth functions). That is why Simpson's rule is so effective.

An integral with no formula

Let's approximate ∫ from 0 to 1 of e^(-x²) dx. This is related to the normal distribution used in statistics and has no elementary antiderivative.

Python The integral of exp(-x^2)

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 numbers settle to many digits quickly. Notice that the function erf (the "error function") is exactly this integral given a name: some integrals are so useful that they define new functions.

Integrating data tables

Often you have measurements instead of a formula. Say a car's speed was recorded every 10 s. You can apply the same rules to the table, as long as the time steps are equal. The trapezoid rule works even with unequal steps, since each trapezoid can have its own width.

Python Distance from a speed table

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.

Choosing a method

Method Needs Error shrinks like Best for
Left/right sum any n h quick teaching examples
Midpoint any n h² simple and fairly accurate
Trapezoid any n h² tables of data, unequal spacing
Simpson even n h⁴ smooth functions

Numerical methods can fail on functions with sharp spikes, jumps or infinite values. Plot the function first, and compare two different values of n: if the answers barely change, you can trust the result.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why use numerical integration?

    When no formula for an antiderivative is available, or only measured values exist, we approximate the area.
  2. 2. Simpson's rule requires

    It fits one parabola per pair of subintervals, so n must be even.
  3. 3. If you double n, the trapezoid rule's error shrinks by about a factor of

    The trapezoid error is proportional to h², so halving h cuts it to a quarter. (Simpson's rule gains a factor of about 16.)
  4. 4. In the trapezoid rule, the interior points have weight

    Each interior point belongs to two trapezoids, so it is counted twice.

Summary

  • Numerical integration approximates definite integrals when no convenient antiderivative exists or when data is tabulated.
  • Trapezoid rule: error ~ h². Simpson's rule: error ~ h⁴, with an even n.
  • Compare results for different n to judge accuracy.
  • Software such as SciPy's quad uses adaptive versions of these ideas.

Next: Differential Equations and Slope Fields