6
6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia 1/6 en.wikipedia.org/wiki/Trapezoidal_rule#Excel

Trapezoidal Rule

Embed Size (px)

DESCRIPTION

Numerical Methods

Citation preview

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

1/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

The function f(x) (in blue) isapproximated by a linear function (inred).

Trapezoidal ruleFrom Wikipedia, the free encyclopedia

In numerical analysis, the trapezoidal rule (also known as thetrapezoid rule or trapezium rule) is an approximate technique forcalculating the definite integral

The trapezoidal rule works by approximating the region underthe graph of the function as a trapezoid and calculating itsarea. It follows that

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

Applicability and alternatives

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

2/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

The trapezoidal rule is one of a family of formulas for numerical integration called Newton–Cotesformulas, of which the midpoint rule is similar to the trapezoid rule. Simpson's rule is another memberof the same family, and in general has faster convergence than the trapezoidal rule for functions whichare twice continuously differentiable, though not in all specific cases. However for various classes ofrougher functions (ones with weaker smoothness conditions), the trapezoidal rule has fasterconvergence in general than Simpson's rule.[1]

Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions areintegrated over their periods, which can be analyzed in various ways.[2][3]

For non-periodic functions, however, methods with unequally spaced points such as Gaussianquadrature and Clenshaw–Curtis quadrature are generally far more accurate; Clenshaw–Curtisquadrature can be viewed as a change of variables to express arbitrary integrals in terms of periodicintegrals, 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 thegrid 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 thenumerical result:

There exists a number ξ between a and b, such that[4]

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

3/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

It follows that if the integrand is concave up (and thus has a positive second derivative), then the error isnegative and the trapezoidal rule overestimates the true value. This can also be seen from the geometricpicture: 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 iscounted above. If the interval of the integral being approximated includes an inflection point, then theerror is harder to identify.

In general, three techniques are used in the analysis of error:[5]

1. Fourier series

2. Residue calculus

3. Euler–Maclaurin summation formula:[6][7]

An asymptotic error estimate for N → ¬ is given by

Further terms in this error estimate are given by the Euler–Maclaurin summation formula.

It is argued that the speed of convergence of the trapezoidal rule reflects and can be used as a definitionof classes of smoothness of the functions.[2]

Periodic functions

The trapezoidal rule often converges very quickly for periodic functions.[3] This can be explainedintuitively 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 boundsthan Simpson's rule.[1]

Sample implementations

Excel

The trapezoidal rule is easily implemented in Excel.

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

4/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

As an example, we show the integral of .

Python

The (composite) trapezoidal rule can be implemented in Python as follows:

#!/usr/bin/env pythonfrom __future__ import division def trapezoidal_rule(f, a, b, n): """Approximates the definite integral of f from a to b by the composite trapezoidal rule, using n subintervals""" h = (b - a) / n s = f(a) + f(b) for i in xrange(1, n): s += 2 * f(a + i * h) return s * h / 2 print trapezoidal_rule(lambda x:x**9, 0.0, 10.0, 100000)# displays 1000000000.75

MATLAB and GNU Octave

The (composite) trapezoidal rule can be implemented in MATLAB as follows:

function s=Traq(f,a,b,M) %Input - f is the integrand input as a string 'f'% - a and b are upper and lower limits of integration% - M is the number of subintervals%Output - s is the trapezoidal rule sum h=(b-a)/M;s=0;

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

5/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

for k=1:(M-1) x=a+h*k; s=s+feval(f,x);end s=h*(feval(f,a)+feval(f,b))/2+h*s;

An alternative implementation that uses the MATLAB's vectorization is

function s=Trapezoid(f,a,b,M) %Input - f is the integrand input as a string 'f', which must accept vector inputs% - a and b are upper and lower limits of integration% - M is the number of subintervals%Output - s is the trapezoidal rule sum h=(b-a)/M;x=a:h:b;fval=feval(f,x);s=h*sum(fval(1:end-1)+fval(2:end))/2;

C++

In C++, one can implement the trapezoidal rule as follows.

template <class ContainerA, class ContainerB>double trapezoid_integrate(const ContainerA &x, const ContainerB &y) { if (x.size() != y.size()) { throw std::logic_error("x and y must be the same size"); } double sum = 0.0; for (int i = 1; i < x.size(); i++) { sum += (x[i] - x[i-1]) * (y[i] + y[i-1]); } return sum * 0.5;}

Here, x and y can be any object of a class implementing operator[] and size().

See also

Rectangle method

Simpson's rule

Romberg's method

Newton–Cotes formulas

Gaussian quadrature

Notes

1. ^ a b (Cruz-Uribe & Neugebauer 2002)

2. ^ a b c (Rahman & Schmeisser 1990)

3. ^ a b c (Weideman 2002)

4. ^ Atkinson (1989, equation (5.1.7))

6/28/12 Trapezoidal rule - Wikipedia, the free encyclopedia

6/6en.wikipedia.org/wiki/Trapezoidal_rule#Excel

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): 123–138, 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-38216-

9.

Weideman, J. A. C. (January 2002), "Numerical Integration of Periodic Functions: A Few Examples", The

American Mathematical Monthly 109 (1): 21–36, 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.