Loading [MathJax]/jax/element/mml/optable/GeneralPunctuation.js
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Physics LibreTexts

9.2: Trapezium Rule

( \newcommand{\kernel}{\mathrm{null}\,}\)

The trapezium rule is a simple extension of the mid-point integration rule. Instead of calculating the area of a sequence of rectangles, we calculate the area of a sequence of trapeziums, as shown in Fig. 9.2.1. As before, we divide the integration region into N equal segments; however, we now consider the end-points of these segments, rather than their mid-points. These is a set of N+1 positions {x0,xN}, such that

xn=a+nΔx,ΔxbaN.

clipboard_ebc0c6b20bd9d3dd2e25e2c4a225d1586.png
Figure 9.2.1: Computing a definite integral using the trapezium rule.

Note that a=x0 and b=xN. By using the familiar formula for the area of each trapezium, we can approximate the integral as

Itrapz=N1n=0Δx(f(xn)+f(xn+1)2)=Δx[(f(x0)+f(x1)2)+(f(x1)+f(x2)2)++(f(xN1)+f(xN)2)]=Δx[f(x0)2+(N1n=1f(xn))+f(xN)2].

9.2.1 Python Implementation of the Trapezium Rule

In Scipy, the trapezium rule is implemented by the trapz function. It usually takes two array arguments, y and x; then the function call trapz(y,x) returns the trapezium rule estimate for ydx, using the elements of x as the discretization points, and the elements of y as the values of the integrand at those points.

Note

Matlab compatibility note: Be careful of the order of inputs! For the Scipy function, it's trapz(y,x). For the Matlab function of the same name, the inputs are supplied in the opposite order, as trapz(x,y).

Note that the discretization points in x need not be equally-spaced. Alternatively, you can call the function as trapz(y,dx=s); this performs the numerical integration assuming equally-spaced discretization points, with spacing s.

Here is an example of using trapz to evaluate the integral 0exp(x2)dx=1:

>>> from scipy import *
>>> x = linspace(0,10,25)
>>> y = exp(-x)
>>> t = trapz(y,x)
>>> print(t)
1.01437984777

9.2.2 Numerical Error for the Trapezium Rule

From a visual comparison of Fig. 9.1.1 (for the mid-point rule) and Fig. 9.2.1 (for the trapezium rule), we might be tempted to conclude that the trapezium rule should be more accurate. Before jumping to that conclusion, however, let's do the actual calculation of the numerical error. This is similar to our above calculation of the mid-point rule's numerical error. For the trapezium rule, however, it's convenient to consider a pair of adjacent segments, which lie between the three discretization points {xn1,xn,xn+1}.

As before, we perform a Taylor expansion of f(x) in the vicinity of xn:

f(x)=f(xn)+f(xn)(xxn)+f

If we integrate f(x) from x_{n-1} to x_{n+1}, the result is

\begin{align} \mathcal{I}_n & \equiv \int_{x_n - \Delta x}^{x_n + \Delta x} f(x) dx \\ &= \; 2 f(x_n) \Delta x \;\;+\; f'(x_n) \int_{x_n - \Delta x}^{x_n + \Delta x} (x-x_n) dx \; +\; \frac{f''(x_n)}{2} \int_{x_n - \Delta x}^{x_n + \Delta x} (x-x_n)^2 dx \; +\; \cdots \end{align}

As before, every other term on the right-hand side integrates to zero. We are left with

\mathcal{I}_n \; = \; 2 f(x_n) \Delta x \;+\; \frac{f''(x_n)}{3} \Delta x^3 + O(\Delta x^5) \;+\; \cdots

This has to be compared to the estimate produced by the trapezium rule, which is

\mathcal{I}_n^{\mathrm{(trapz)}} = \Delta x \left[ \frac{f(x_{n-1})}{2} + f(x_n) + \frac{f(x_{n+1})}{2}\right].

If we Taylor expand the first and last terms of the above expression around x_{n}, the result is:

\begin{align} \mathcal{I}_n^{\mathrm{(trapz)}} & = \frac{\Delta x}{2} \left[f(x_n) - f'(x_n) \Delta x + \frac{f''(x_n)}{2} \Delta x^2 - \frac{f'''(x_n)}{6} \Delta x^3 + \frac{f''''(x_n)}{24} \Delta x^4 + O(\Delta x^4)\right] \\ &\quad + \;\;\Delta x \; f(x_n) \\ &\quad + \frac{\Delta x}{2} \left[f(x_n) + f'(x_n) \Delta x + \frac{f''(x_n)}{2} \Delta x^2 + \frac{f'''(x_n)}{6} \Delta x^3 + \frac{f''''(x_n)}{24} \Delta x^4 + O(\Delta x^4)\right] \\ & = 2 f(x_n) \Delta x + \frac{f''(x_n)}{2} \Delta x^3 + O(\Delta x^5). \end{align}

Hence, the numerical error for integrating over this pair of segments is

\mathcal{E}_n \;\equiv\; \left|\mathcal{I}_n - \mathcal{I}_n^{\mathrm{trapz}}\right| \;=\; \frac{|f''(x_n)|}{6} \Delta x^3 \;\sim\; O\left(\frac{1}{N^3}\right).

And the total numerical error goes as

\mathcal{E}_{\mathrm{total}} \sim O\left(\frac{1}{N^2}\right),

which is the same scaling as the mid-point rule! Despite our expectations, the trapezium rule isn't actually an improvement over the mid-point rule, as far as numerical accuracy is concerned.


This page titled 9.2: Trapezium Rule is shared under a CC BY-SA 4.0 license and was authored, remixed, and/or curated by Y. D. Chong via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?