Home / Articles / University Mathematics
University Mathematics · 5 min read

Numerical Integration: Trapezoidal and Simpson's Rules

Learn the trapezoidal rule and Simpson's 1/3 rule for numerical integration, with derivations, error bounds and fully worked examples using functions and data.

Many integrals cannot be evaluated with a neat formula, and in practice we often have only a table of measured values rather than a function at all. Numerical integration solves both problems by approximating the area under a curve with simple shapes. The trapezoidal rule and Simpson's rule are the two methods every university student in mathematics, engineering and physics needs to know.

Why We Need Numerical Methods

Consider \( \int_0^1 e^{-x^2}\, dx \). This integral appears constantly in probability, yet \( e^{-x^2} \) has no antiderivative made of elementary functions. Similarly, an engineer measuring the velocity of a vehicle every two seconds has data points, not a formula. In both cases we approximate

$$ I = \int_a^b f(x)\, dx $$

by dividing \([a, b]\) into \( n \) equal strips of width

$$ h = \frac{b - a}{n} $$

and evaluating \( f \) at the points \( x_0 = a, x_1, \dots, x_n = b \). We write \( f_i = f(x_i) \).

The Trapezoidal Rule

The Idea

Join neighbouring points with straight lines. Each strip becomes a trapezium with area \( \frac{h}{2}(f_i + f_{i+1}) \). Adding all the strips, each interior value is counted twice:

$$ T_n = \frac{h}{2}\Big[ f_0 + 2(f_1 + f_2 + \dots + f_{n-1}) + f_n \Big] $$

A simple memory aid: "first plus last, plus twice the rest, times half the width".

Error Bound

If \( |f''(x)| \le M \) on \([a, b]\), the error satisfies

$$ |E_T| \le \frac{(b - a)h^2}{12} M $$

The error is proportional to \( h^2 \), so halving the strip width roughly divides the error by 4. The rule is exact for straight lines, since then \( f'' = 0 \). For a curve that bends upwards (concave up), the trapeziums lie above the curve and overestimate the area.

Simpson's 1/3 Rule

The Idea

Instead of straight lines, fit a parabola through each group of three consecutive points. This needs an even number of strips. Integrating the parabolas gives

$$ S_n = \frac{h}{3}\Big[ f_0 + 4(f_1 + f_3 + \dots + f_{n-1}) + 2(f_2 + f_4 + \dots + f_{n-2}) + f_n \Big] $$

The weights follow the pattern 1, 4, 2, 4, 2, ..., 4, 1: odd-numbered points get weight 4 and interior even-numbered points get weight 2.

Error Bound

If \( |f^{(4)}(x)| \le K \) on \([a, b]\), then

$$ |E_S| \le \frac{(b - a)h^4}{180} K $$

Because the error depends on \( h^4 \), halving \( h \) cuts the error by about 16. Surprisingly, Simpson's rule is exact not only for quadratics but also for cubics, since a cubic has \( f^{(4)} = 0 \).

Worked Example 1: Testing Against a Known Answer

Approximate \( \int_1^2 \frac{1}{x}\, dx \) using \( n = 4 \) with both rules, and compare with the exact value \( \ln 2 = 0.693147 \).

Step 1. Strip width: \( h = \frac{2 - 1}{4} = 0.25 \).

Step 2. Table of values (6 decimal places):

\( i \) \( x_i \) \( f_i = 1/x_i \)
0 1.00 1.000000
1 1.25 0.800000
2 1.50 0.666667
3 1.75 0.571429
4 2.00 0.500000

Step 3. Trapezoidal rule:

$$ T_4 = \frac{0.25}{2}\Big[ 1 + 0.5 + 2(0.8 + 0.666667 + 0.571429) \Big] = 0.125 \times 5.576190 = 0.697024 $$

Step 4. Simpson's rule:

$$ S_4 = \frac{0.25}{3}\Big[ 1 + 0.5 + 4(0.8 + 0.571429) + 2(0.666667) \Big] $$

$$ S_4 = 0.083333 \times \big[ 1.5 + 5.485714 + 1.333333 \big] = 0.083333 \times 8.319048 = 0.693254 $$

Step 5. Compare errors:

  • Trapezoidal error: \( 0.697024 - 0.693147 = 0.003877 \).
  • Simpson error: \( 0.693254 - 0.693147 = 0.000107 \).

Simpson's rule is about 36 times more accurate here with exactly the same function evaluations.

Step 6. Check against the error bounds. For \( f = \frac{1}{x} \), \( f'' = \frac{2}{x^3} \), so \( M = 2 \) at \( x = 1 \). The trapezoidal bound is \( \frac{1 \times 0.0625}{12} \times 2 = 0.0104 \), and our actual error 0.0039 is within it. For Simpson, \( f^{(4)} = \frac{24}{x^5} \), so \( K = 24 \) and the bound is \( \frac{1 \times 0.00390625}{180} \times 24 = 0.00052 \). The actual error 0.000107 is safely inside. The trapezoidal rule overestimated, as expected, because \( \frac{1}{x} \) is concave up.

Worked Example 2: An Integral With No Formula

Approximate \( \int_0^1 e^{-x^2}\, dx \) with \( n = 4 \).

Step 1. \( h = 0.25 \). The function values are:

  • \( f_0 = e^{0} = 1.000000 \)
  • \( f_1 = e^{-0.0625} = 0.939413 \)
  • \( f_2 = e^{-0.25} = 0.778801 \)
  • \( f_3 = e^{-0.5625} = 0.569783 \)
  • \( f_4 = e^{-1} = 0.367879 \)

Step 2. Trapezoidal rule:

$$ T_4 = 0.125\Big[ 1 + 0.367879 + 2(0.939413 + 0.778801 + 0.569783) \Big] = 0.125 \times 5.943873 = 0.742984 $$

Step 3. Simpson's rule:

$$ S_4 = 0.083333\Big[ 1.367879 + 4(1.509196) + 2(0.778801) \Big] = 0.083333 \times 8.962265 = 0.746855 $$

The true value, found using the error function, is 0.746824. Simpson's answer is correct to four decimal places, while the trapezoidal estimate is off in the third.

Worked Example 3: Distance From Velocity Data

A car starts from rest and its velocity is recorded every 2 seconds. At \( t = 0, 2, 4, 6, 8 \) s the velocity is \( 0, 6, 10, 12, 13 \) m s\(^{-1}\). Estimate the distance travelled.

Step 1. Distance is \( \int_0^8 v\, dt \), with \( h = 2 \) s and \( n = 4 \).

Step 2. Trapezoidal rule:

$$ T = \frac{2}{2}\Big[ 0 + 13 + 2(6 + 10 + 12) \Big] = 1 \times 69 = 69 \text{ m} $$

Step 3. Simpson's rule:

$$ S = \frac{2}{3}\Big[ 0 + 13 + 4(6 + 12) + 2(10) \Big] = \frac{2}{3} \times 105 = 70 \text{ m} $$

Sanity check: if the car had travelled at its final speed of 13 m s\(^{-1}\) for the full 8 s it would cover 104 m, and at its average reading of about 8 m s\(^{-1}\) it would cover about 66 m. An answer near 70 m is reasonable. The velocity curve bends downwards (the car's acceleration is falling), so the trapezoidal rule underestimates and Simpson's larger value is more trustworthy.

Choosing a Method

  • Use Simpson's rule whenever you have an even number of strips and a smooth function; it is far more accurate for the same effort.
  • Use the trapezoidal rule for an odd number of strips, for rough or noisy data, or for periodic functions over a full period, where it is remarkably accurate.
  • In software, scipy.integrate.trapezoid and scipy.integrate.simpson in Python, or trapz in MATLAB, apply these rules to data arrays.

Did You Know?

  • Simpson's rule is named after Thomas Simpson, who published it in 1743, but a similar rule had been used by Johannes Kepler over a century earlier to estimate the volumes of wine barrels.
  • Richardson extrapolation combines two trapezoidal estimates with different \( h \) to cancel the leading error term, and doing so reproduces Simpson's rule exactly.
  • Modern software often uses adaptive methods that automatically use smaller strips where the curve bends sharply.

Key Takeaways

  • Numerical integration approximates \( \int_a^b f(x)\, dx \) from function values at equally spaced points.
  • The trapezoidal rule has error proportional to \( h^2 \) and is exact for straight lines.
  • Simpson's rule needs an even number of strips, has error proportional to \( h^4 \), and is exact for cubics.
  • Error bounds use \( \max|f''| \) for the trapezoidal rule and \( \max|f^{(4)}| \) for Simpson's rule.
  • The concavity of the curve tells you whether the trapezoidal rule over- or underestimates.

If you need help with numerical methods assignments or step-by-step solutions, Amathify's subject experts and PhD scholars are ready to assist. Reach us on WhatsApp or through the Ask a Question form.

Stuck on a problem or assignment?

Our experts and PhD scholars solve maths, physics and chemistry — step by step.

WhatsApp usAsk a question

Related articles