Trapezoidal rule
From Wikipedia, the free encyclopedia
In mathematics, the trapezoidal rule (also known as the trapezoid rule, or the trapezium rule in British English) is a way to approximately calculate the definite integral
The trapezoidal rule works by approximating the region under the graph of the function f(x) by a trapezoid and calculating its area. It follows that
To calculate this integral more accurately, one first splits the interval of integration [a,b] into n smaller subintervals, and then applies the trapezoidal rule on each of them. One obtains the composite trapezoidal rule:
This can alternatively be written as:
where
(one can also use a non-uniform grid).
The trapezoidal rule is one of a family of formulas for numerical integration called Newton–Cotes formulas. Simpson's rule is another, often more accurate, member of the same family. Simpson's rule and other like methods can be expected to improve on the trapezoidal rule for functions which are twice continuously differentiable; however for rougher functions the trapezoidal rule is likely to prove preferable. Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions are integrated over their periods, a fact best understood in connection with the Euler–Maclaurin summation formula. For non-periodic functions, however, methods with unequally spaced points such as Gaussian quadrature and Clenshaw–Curtis quadrature are generally far more accurate; Clenshaw-Curtis quadrature can be viewed as a change of variables to express arbitrary integrals in terms of periodic integrals, at which point the trapezoidal rule can be applied accurately.
Contents |
[edit] Error analysis
The error of the composite trapezoidal rule is the difference between the value of the integral and the numerical result:
This error can be written as
where ξ is some number between a and b.[1]
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 been 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, then the error is harder to identify.
An asymptotic error estimate for n → ∞ is given by
Further terms in this error estimate are given by the Euler–Maclaurin summation formula.
[edit] Python implementation
#!/usr/bin/env python def trapezoidal_rule(f, a, b): "Approximate the definite integral of f from a to b by Trapezoidal rule." return (b - a) * ((f(a) + f(b))/2) def trapezoidal_plus(f, a, b, n): inc = (b-a)/n sum = 0.0 fa = f(a) for i in range (n): b = a + inc fb = f(b) sum += fa + fb a = b ; fa = fb return sum * 0.5 * inc if __name__ == '__main__': from math import sin from math import pi n = input ('Number of iteration?') print 'BEGIN' print '\nWith', n, ' iteration' print '\nIntegral of sin(x) from 0 to 2*pi(ref from Simpson):\t 2.5648942583e-16' print '\nIntegral of sin(x) from 0 to 2*pi(trapezoidal_plus):\t' , trapezoidal_plus(sin, 0, 2*pi, n) print '\nEND'
[edit] Alternative Python implementation
#!/usr/bin/env python def trapezoidal_rule(f, a, b, N): """Approximate the definite integral of f from a to b by Trapezoidal rule, implemented as a Python one-liner""" return (b-a) * ( f(a)/2 + f(b)/2 + sum([f(a + (b-a)*k/N) for k in range(1,N)]) ) / N #test print trapezoidal_rule(lambda x:x**9, 0.0, 10.0, 100000)
[edit] See also
[edit] Notes
- ^ Atkinson (1989), equation (5.1.7)
- ^ Atkinson (1989), equation (5.1.9)
[edit] References
| The Wikibook A-level_Mathematics/C2/Integration#Trapezium_Rule has a page on the topic of |
- Atkinson, Kendall A. (1989), An Introduction to Numerical Analysis (2nd ed.), New York: John Wiley & Sons, ISBN 978-0-471-50023-0.
- Burden, Richard L.; J. Douglas Faires (2000), Numerical Analysis (7th Ed. ed.), Brooks/Cole, ISBN 0-534-38216-9.
[edit] External links
- Trapezoidal Rule for Numerical Integration
- Notes on the convergence of trapezoidal-rule quadrature
- Trapezoidal Rule of Integration - Notes, PPT, Videos, Mathcad, Matlab, Mathematica, Maple, Multiple Choice Tests at Holistic Numerical Methods Institute


![\int_a^b f(x)\,dx \approx \frac{b-a}{n} \left[ {f(a) + f(b) \over 2} + \sum_{k=1}^{n-1} f \left( a+k \frac{b-a}{n} \right) \right].](http://upload.wikimedia.org/math/2/3/d/23d5bd7f90bbfbf2e6876bf8d444e2b2.png)


![\text{error} = \int_a^b f(x)\,dx - \frac{b-a}{n} \left[ {f(a) + f(b) \over 2} + \sum_{k=1}^{n-1} f \left( a+k \frac{b-a}{n} \right) \right].](http://upload.wikimedia.org/math/3/b/0/3b0841ed81e940711ed7531cb14bd381.png)


