Trapezoidal Rule
Trapezoidal Rule
Trapezoidal Rule
Trapezoidal rule
In numerical analysis, the trapezoidal rule (also known as the trapezoid rule or trapezium rule) is an approximate technique for calculating the definite integral
The trapezoidal rule works by approximating the region under the graph of the function as a trapezoid and calculating its area. It follows that
The function f(x) (in blue) is approximated by a linear function (in red).
Contents
1 Applicability and alternatives 2 Numerical Implementation 2.1 Uniform Grid 2.2 Non-uniform Grid 3 Error analysis 3.1 Periodic functions 3.2 "Rough" functions 4 Sample implementations 4.1 Excel 4.2 Python 4.3 MATLAB and GNU Octave 4.4 C++ 5 See also 6 Notes 7 References 8 External links
6/28/12
The trapezoidal rule is one of a family of formulas for numerical integration called NewtonCotes formulas, of which the midpoint rule is similar to the trapezoid rule. Simpson's rule is another member of the same family, and in general has faster convergence than the trapezoidal rule for functions which are twice continuously differentiable, though not in all specific cases. However for various classes of rougher functions (ones with weaker smoothness conditions), the trapezoidal rule has faster convergence in general than Simpson's rule.[1] Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions are integrated over their periods, which can be analyzed in various ways.[2][3] For non-periodic functions, however, methods with unequally spaced points such as Gaussian quadrature and ClenshawCurtis quadrature are generally far more accurate; ClenshawCurtis 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.
Numerical Implementation
Uniform Grid
For a domain discretized into "N" equally spaced panels, or "N+1" grid points (1, 2, ..., N+1), where the grid spacing is "h=(b-a)/N", the approximation to the integral becomes
Non-uniform Grid
When the grid spacing is non-uniform, one can use the formula
Error analysis
The error of the composite trapezoidal rule is the difference between the value of the integral and the numerical result:
6/28/12
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 concavedown 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. In general, three techniques are used in the analysis of error:[5] 1. Fourier series 2. Residue calculus 3. EulerMaclaurin summation formula:[6][7] An asymptotic error estimate for N is given by
Further terms in this error estimate are given by the EulerMaclaurin summation formula. 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.[2]
Periodic functions
The trapezoidal rule often converges very quickly for periodic functions.[3] This can be explained intuitively as: "When the function is periodic and one integrates over one full period, there are about as many sections of the graph that are concave up as concave down, so the errors cancel."[5] More detailed analysis can be found in.[2][3]
"Rough" functions
For various classes of functions that are not twice-differentiable, the trapezoidal rule has sharper bounds than Simpson's rule.[1]
Sample implementations
Excel
The trapezoidal rule is easily implemented in Excel.
en.wikipedia.org/wiki/Trapezoidal_rule#Excel 3/6
6/28/12
Python
The (composite) trapezoidal rule can be implemented in Python as follows:
# ! / u s r / b i n / e n vp y t h o n f r o m_ _ f u t u r e _ _i m p o r td i v i s i o n d e ft r a p e z o i d a l _ r u l e ( f ,a ,b ,n ) : " " " A p p r o x i m a t e st h ed e f i n i t ei n t e g r a lo fff r o mat obb y t h ec o m p o s i t et r a p e z o i d a lr u l e ,u s i n gns u b i n t e r v a l s " " " h=( b-a )/n s=f ( a )+f ( b ) f o rii nx r a n g e ( 1 ,n ) : s+ =2*f ( a+i*h ) r e t u r ns*h/2 p r i n tt r a p e z o i d a l _ r u l e ( l a m b d ax : x * * 9 ,0 . 0 ,1 0 . 0 ,1 0 0 0 0 0 ) #d i s p l a y s1 0 0 0 0 0 0 0 0 0 . 7 5
h = ( b a ) / M ; s = 0 ;
en.wikipedia.org/wiki/Trapezoidal_rule#Excel 4/6
6/28/12
f o rk = 1 : ( M 1 ) x = a + h * k ; s = s + f e v a l ( f , x ) ; e n d s = h * ( f e v a l ( f , a ) + f e v a l ( f , b ) ) / 2 + h * s ;
h = ( b a ) / M ; x = a : h : b ; f v a l = f e v a l ( f , x ) ; s = h * s u m ( f v a l ( 1 : e n d 1 ) + f v a l ( 2 : e n d ) ) / 2 ;
C++
In C++, one can implement the trapezoidal rule as follows.
t e m p l a t e< c l a s sC o n t a i n e r A ,c l a s sC o n t a i n e r B > d o u b l et r a p e z o i d _ i n t e g r a t e ( c o n s tC o n t a i n e r A& x , c o n s tC o n t a i n e r B& y ){ i f( x . s i z e ( )! =y . s i z e ( ) ){ t h r o ws t d : : l o g i c _ e r r o r ( " xa n dym u s tb et h es a m es i z e " ) ; } d o u b l es u m=0 . 0 ; f o r( i n ti=1 ;i<x . s i z e ( ) ;i + + ){ s u m+ =( x [ i ]-x [ i 1 ] )*( y [ i ]+y [ i 1 ] ) ; } r e t u r ns u m*0 . 5 ; }
See also
Rectangle method Simpson's rule Romberg's method NewtonCotes formulas Gaussian quadrature
Notes
1. 2. 3. 4. ^ a b (Cruz-Uribe & Neugebauer 2002) ^ a b c (Rahman & Schmeisser 1990) ^ a b c (Weideman 2002) ^ Atkinson (1989, equation (5.1.7))
5/6
en.wikipedia.org/wiki/Trapezoidal_rule#Excel
6/28/12
5. ^ a b (Weideman 2002, p. 23, section 2) 6. ^ Atkinson (1989, equation (5.1.9)) 7. ^ Atkinson (1989, p. 285)
References
Atkinson, Kendall E. (1989), An Introduction to Numerical Analysis (2nd ed.), New York: John Wiley & Sons, ISBN 978-0-471-50023-0. Rahman, Qazi I.; Schmeisser, Gerhard (December 1990), "Characterization of the speed of convergence of the trapezoidal rule", Numerische Mathematik 57 (1): 123138, DOI:10.1007/BF01386402 (http://dx.doi.org/10.1007%2FBF01386402) , ISSN 0945-3245 (//www.worldcat.org/issn/0945-3245) Burden, Richard L.; J. Douglas Faires (2000), Numerical Analysis (7th ed.), Brooks/Cole, ISBN 0-534-382169. Weideman, J. A. C. (January 2002), "Numerical Integration of Periodic Functions: A Few Examples", The American Mathematical Monthly 109 (1): 2136, DOI:10.2307/2695765 (http://dx.doi.org/10.2307%2F2695765) , JSTOR 2695765 (http://www.jstor.org/stable/2695765) Cruz-Uribe, D.; Neugebauer, C.J. (2002), "Sharp Error Bounds for the Trapezoidal Rule and Simpson's Rule" (http://www.emis.de/journals/JIPAM/images/031_02_JIPAM/031_02.pdf) , Journal of Inequalities in Pure and Applied Mathematics (http://jipam.vu.edu.au/) 3 (4), http://www.emis.de/journals/JIPAM/images/031_02_JIPAM/031_02.pdf
External links
Trapezoidal Rule for Numerical Integration (http://math.fullerton.edu/mathews/n2003/TrapezoidalRuleMod.html) Notes on the convergence of trapezoidal-rule quadrature (http://dedekind.mit.edu/~stevenj/trapezoidal.pdf) Retrieved from "http://en.wikipedia.org/w/index.php?title=Trapezoidal_rule&oldid=496226430" Categories: Numerical integration (quadrature) This page was last modified on 6 June 2012 at 05:14. Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of use for details. Wikipedia is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.
en.wikipedia.org/wiki/Trapezoidal_rule#Excel
6/6