Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Numerical Integration: Trapezoidal and Simpson’s Rule | Core Numerical Algorithms
Numerical Methods for Scientific Computing with Python

bookNumerical Integration: Trapezoidal and Simpson’s Rule

When you need to approximate the definite integral of a function — especially when the function is too complex for an exact analytical solution — numerical integration provides practical tools. Two of the most widely used basic methods are the trapezoidal rule and Simpson's rule.

The trapezoidal rule approximates the area under a curve by dividing the integration interval into smaller subintervals, then estimating the area under the curve as a series of adjacent trapezoids. For a function f(x)f(x) over the interval [a,b][a, b] split into nn equal subintervals of width hh, the rule gives:

Integral(h/2)[f(a)+2f(x1)+2f(x2)+...+2f(xn1)+f(b)]\text{Integral} ≈ (h/2) * [f(a) + 2*f(x1) + 2*f(x2) + ... + 2*f(x_{n-1}) + f(b)]

where x1,x2,...,xn1x1, x2, ..., x_{n-1} are the interior points.

Simpson's rule provides a more accurate approximation by fitting parabolas to the function over pairs of subintervals. For an even number of subintervals nn, Simpson's rule is:

Integral(h/3)[f(a)+4f(x1)+2f(x2)+4f(x3)+...+4f(xn1)+f(b)]\text{Integral} ≈ (h/3) * [f(a) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 4*f(x_{n-1}) + f(b)]

Here, weights alternate between 4 and 2 for the odd and even-indexed interior points, respectively. Both methods require the function to be sampled at regular intervals, but Simpson's rule generally achieves higher accuracy for smooth functions.

123456789101112131415161718192021222324252627282930
# Implement the trapezoidal rule and Simpson's rule in Python import numpy as np def f(x): # Example function: f(x) = sin(x) return np.sin(x) def trapezoidal_rule(f, a, b, n): h = (b - a) / n x = np.linspace(a, b, n + 1) y = f(x) return (h / 2) * (y[0] + 2 * np.sum(y[1:-1]) + y[-1]) def simpsons_rule(f, a, b, n): if n % 2 == 1: raise ValueError("Simpson's rule requires an even number of intervals.") h = (b - a) / n x = np.linspace(a, b, n + 1) y = f(x) return (h / 3) * (y[0] + 4 * np.sum(y[1:-1:2]) + 2 * np.sum(y[2:-2:2]) + y[-1]) # Estimate the integral of sin(x) from 0 to pi a, b = 0, np.pi n = 10 # Number of subintervals trap_result = trapezoidal_rule(f, a, b, n) simp_result = simpsons_rule(f, a, b, n) print(f"Trapezoidal Rule Result: {trap_result:.6f}") print(f"Simpson's Rule Result: {simp_result:.6f}")
copy

Error analysis is crucial when choosing between the trapezoidal and Simpson's rules. The trapezoidal rule is simple and robust, but its error decreases proportionally to the square of the subinterval width (O(h2)O(h^2)), making it less accurate for functions with significant curvature. Simpson's rule, on the other hand, has error that decreases with the fourth power of the subinterval width (O(h4)O(h^4)), offering much greater accuracy for smooth, well-behaved functions. However, Simpson's rule requires an even number of subintervals and may not provide a significant advantage for functions with sharp discontinuities or high-frequency oscillations. When computational cost is a concern and the function is reasonably smooth, Simpson's rule is often preferred; for rougher functions or variable grid spacing, the trapezoidal rule may be more practical.

Note
Study More

Adaptive quadrature techniques automatically adjust the subinterval widths to achieve a desired accuracy, and higher-order integration methods can provide even better performance for smooth functions. Explore methods like Romberg integration and Gaussian quadrature for advanced applications.

question mark

Which statement best describes when Simpson's rule is preferred over the trapezoidal rule?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookNumerical Integration: Trapezoidal and Simpson’s Rule

Swipe to show menu

When you need to approximate the definite integral of a function — especially when the function is too complex for an exact analytical solution — numerical integration provides practical tools. Two of the most widely used basic methods are the trapezoidal rule and Simpson's rule.

The trapezoidal rule approximates the area under a curve by dividing the integration interval into smaller subintervals, then estimating the area under the curve as a series of adjacent trapezoids. For a function f(x)f(x) over the interval [a,b][a, b] split into nn equal subintervals of width hh, the rule gives:

Integral(h/2)[f(a)+2f(x1)+2f(x2)+...+2f(xn1)+f(b)]\text{Integral} ≈ (h/2) * [f(a) + 2*f(x1) + 2*f(x2) + ... + 2*f(x_{n-1}) + f(b)]

where x1,x2,...,xn1x1, x2, ..., x_{n-1} are the interior points.

Simpson's rule provides a more accurate approximation by fitting parabolas to the function over pairs of subintervals. For an even number of subintervals nn, Simpson's rule is:

Integral(h/3)[f(a)+4f(x1)+2f(x2)+4f(x3)+...+4f(xn1)+f(b)]\text{Integral} ≈ (h/3) * [f(a) + 4*f(x1) + 2*f(x2) + 4*f(x3) + ... + 4*f(x_{n-1}) + f(b)]

Here, weights alternate between 4 and 2 for the odd and even-indexed interior points, respectively. Both methods require the function to be sampled at regular intervals, but Simpson's rule generally achieves higher accuracy for smooth functions.

123456789101112131415161718192021222324252627282930
# Implement the trapezoidal rule and Simpson's rule in Python import numpy as np def f(x): # Example function: f(x) = sin(x) return np.sin(x) def trapezoidal_rule(f, a, b, n): h = (b - a) / n x = np.linspace(a, b, n + 1) y = f(x) return (h / 2) * (y[0] + 2 * np.sum(y[1:-1]) + y[-1]) def simpsons_rule(f, a, b, n): if n % 2 == 1: raise ValueError("Simpson's rule requires an even number of intervals.") h = (b - a) / n x = np.linspace(a, b, n + 1) y = f(x) return (h / 3) * (y[0] + 4 * np.sum(y[1:-1:2]) + 2 * np.sum(y[2:-2:2]) + y[-1]) # Estimate the integral of sin(x) from 0 to pi a, b = 0, np.pi n = 10 # Number of subintervals trap_result = trapezoidal_rule(f, a, b, n) simp_result = simpsons_rule(f, a, b, n) print(f"Trapezoidal Rule Result: {trap_result:.6f}") print(f"Simpson's Rule Result: {simp_result:.6f}")
copy

Error analysis is crucial when choosing between the trapezoidal and Simpson's rules. The trapezoidal rule is simple and robust, but its error decreases proportionally to the square of the subinterval width (O(h2)O(h^2)), making it less accurate for functions with significant curvature. Simpson's rule, on the other hand, has error that decreases with the fourth power of the subinterval width (O(h4)O(h^4)), offering much greater accuracy for smooth, well-behaved functions. However, Simpson's rule requires an even number of subintervals and may not provide a significant advantage for functions with sharp discontinuities or high-frequency oscillations. When computational cost is a concern and the function is reasonably smooth, Simpson's rule is often preferred; for rougher functions or variable grid spacing, the trapezoidal rule may be more practical.

Note
Study More

Adaptive quadrature techniques automatically adjust the subinterval widths to achieve a desired accuracy, and higher-order integration methods can provide even better performance for smooth functions. Explore methods like Romberg integration and Gaussian quadrature for advanced applications.

question mark

Which statement best describes when Simpson's rule is preferred over the trapezoidal rule?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 2
some-alt