In calculus, the trapezoidal rule (informally trapezoid rule; or in British English trapezium rule) is a technique for numerical integration, i.e. approximating the definite integral: ∫abf(x)dx.
The trapezoidal rule works by approximating the region under the graph of the function f(x) as a trapezoid and calculating its area. This is easily calculated by noting that the area of the region is made up of a rectangle with width (b−a) and height f(a), and a triangle of width (b−a) and height f(b)−f(a). Therefore, ∫abf(x)dx≈(b−a)⋅f(a)⏟Area of rectangle+12(b−a)⋅[f(b)−f(a)]⏟Area of triangle=(b−a)⋅(f(a)+12f(b)−12f(a))=(b−a)⋅(12f(a)+12f(b))=12(b−a)[f(a)+f(b)]. The rule can also be derived by replacing the integrand with the equation of the line joining points (a,f(a)) and (b,f(b)), which using the two point form of the equation of a line, is y=(x−a)f(b)−f(a)b−a+f(a). Therefore, ∫abf(x)dx≈∫ab(x−a)f(b)−f(a)b−a+f(a)dx=[12(x−a)2f(b)−f(a)b−a+xf(a)]x=ax=b=[12(b−a)2f(b)−f(a)b−a+bf(a)]−af(a)=12(b−a)[f(b)−f(a)]+(b−a)f(a)=12(b−a)[f(a)+f(b)], as before.
The integral can be even better approximated by partitioning the integration interval, applying the trapezoidal rule to each subinterval and summing the results. In practice, this "chained" (or "composite") trapezoidal rule is usually what is meant by "integrating with the trapezoidal rule". Let {xk} be a partition of [a,b] such that a=x0<x1<⋯<xN−1<xN=b, and Δxk be the length of the k-th subinterval (that is, Δxk=xk−xk−1), then ∫abf(x)dx≈∑k=1Nf(xk−1)+f(xk)2Δxk. The trapezoidal rule may be viewed as the result obtained by averaging the left and right Riemann sums and is sometimes defined this way. The approximation becomes more accurate as the resolution of the partition increases (that is, for larger N, all Δxk decrease). When all the Δxk have the same value Δx, as is often the case, the formula can be simplified for calculation efficiency by factoring Δx out: ∫abf(x)dx≈Δx(f(x0)+f(xN)2+∑k=1N−1f(xk)). As discussed below, it is also possible to place error bounds on the accuracy of the value of a definite integral estimated using a trapezoidal rule.
History Evidence discovered in Babylonian cuneiform tablets circa 200 BCE predicting the motion of Jupiter along the ecliptic suggests that the trapezoid rule was in use for astronomical prediction long before calculus itself was invented.
Numerical implementation
Non-uniform grid When the grid spacing is non-uniform, one can use the formula ∫abf(x)dx≈∑k=1Nf(xk−1)+f(xk)2Δxk, where Δxk=xk−xk−1, or more a computationally efficient formula ∫abf(x)dx≈12(f(x0)Δ+1x0+f(xN)Δ−1xN+∑k=1N−1f(xk)Δ±1xk), where Δ+1x0=x1−x0, Δ−1xN=xN−xN−1, Δ±1xk=xk+1−xk−1 are the corresponding forward, backward, and central differences.
Uniform grid For a domain partitioned by N+1 equally spaced points, considerable simplification may occur. Let Δx=b−aN and xk=a+kΔx for k=0,1,…,N. The approximation to the integral becomes ∫abf(x)dx≈Δx2∑k=1N[f(xk−1)+f(xk)]=Δx(12f(x0)+12f(xN)+∑k=1N−1f(xk)). Sometimes this expression is written as Δx ∑)′k=0Nf(xk), where the symbol ∑ ′ indicates that the first and last terms are halved.
Error analysis
The error of the composite trapezoidal rule is the difference between the value of the integral and the numerical result: E=∫abf(x)dx−b−aN[f(a)+f(b)2+∑k=1N−1f(a+kb−aN)] There exists a number ξ between a and b, such that E=−(b−a)312N2f″(ξ) It follows that if the integrand is concave up (and thus has a positive second derivative), then the error is negative and the trapezoidal rule overestimates the true value. This can also be seen from the geometric picture: the trapezoids include all of the area under the curve and extend over it. Similarly, a concave-down function yields an underestimate because area is unaccounted for under the curve, but none is counted above. If the interval of the integral being approximated includes an inflection point, the sign of the error is harder to identify. An asymptotic error estimate for N → ∞ is given by E=−(b−a)212N2[f′(b)−f′(a)]+O(N−3). Further terms in this error estimate are given by the Euler–Maclaurin summation formula. Several techniques can be used to analyze the error, including:
Fourier series Residue calculus Euler–Maclaurin summation formula Polynomial interpolation It is argued that the speed of convergence of the trapezoidal rule reflects and can be used as a definition of classes of smoothness of the functions.
Proof First suppose that h=b−aN and ak=a+(k−1)h. Let gk(t)=12t[f(ak)+f(ak+t)]−∫akak+tf(x)dx be the function such that |gk(h)| is the error of the trapezoidal rule on one of the intervals, [ak,ak+h]. Then dgkdt=12[f(ak)+f(ak+t)]+12t⋅f′(ak+t)−f(ak+t), and d2gkdt2=12t⋅f″(ak+t). Now suppose that |f″(x)|≤|f″(ξ)|, which holds if f is sufficiently smooth. It then follows that |f″(ak+t)|≤f″(ξ) which is equivalent to −f″(ξ)≤f″(ak+t)≤f″(ξ), or −f″(ξ)t2≤gk″(t)≤f″(ξ)t2. Since gk′(0)=0 and gk(0)=0, ∫0tgk″(x)dx=gk′(t) and ∫0tgk′(x)dx=gk(t). Using these results, we find −f″(ξ)t24≤gk′(t)≤f″(ξ)t24 and −f″(ξ)t312≤gk(t)≤f″(ξ)t312 Letting t=h we find −f″(ξ)h312≤gk(h)≤f″(ξ)h312. Summing all of the local error terms we find ∑k=1Ngk(h)=b−aN[f(a)+f(b)2+∑k=1N−1f(a+kb−aN)]−∫abf(x)dx. But we also have −∑k=1Nf″(ξ)h312≤∑k=1Ngk(h)≤∑k=1Nf″(ξ)h312 and ∑k=1Nf″(ξ)h312=f″(ξ)h3N12, so that −f″(ξ)h3N12≤b−aN[f(a)+f(b)2+∑k=1N−1f(a+kb−aN)]−∫abf(x)dx≤f″(ξ)h3N12. Therefore the total error is bounded by error=|∫abf(x)dx−b−aN[f(a)+f(b)2+∑k=1N−1f(a+kb−aN)]|≤f″(ξ)h3N12=f″(ξ)(b−a)312N2.
Periodic and peak functions The trapezoidal rule converges rapidly for periodic functions. This is an easy consequence of the Euler–Maclaurin summation formula, which says that if f is p times continuously differentiable with period T, then ∑k=0N−1f(kh)h=∫0Tf(x)dx+∑k=1⌊p/2⌋B2k(2k)!(f(2k−1)(T)−f(2k−1)(0))−(−1)php∫0TB~p(x/T)f(p)(x)dx, where h:=T/N, and B~p is the periodic extension of the p-th Bernoulli polynomial. Due to the periodicity, the derivatives at the endpoint cancel, and we see that the error is O(hp). A similar effect is available for peak-like functions, such as Gaussian, Exponentially modified Gaussian and other functions with derivatives at integration limits that can be neglected. The evaluation of the full integral of a Gaussian function by trapezoidal rule with 1% accuracy can be made using just 4 points. Simpson's rule requires 1.8 times more points to achieve the same accuracy.
… excerpt ends here. Continue reading the full article.



![Trapezoidal rule: Illustration of "chained trapezoidal rule" used on an irregularly spaced partition of [a,b]](https://thumb.wikimedia.org/wikipedia/commons/thumb/d/d1/Integration_num_trapezes_notation.svg/500px-Integration_num_trapezes_notation.svg.png?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)
