Share on Facebook Share on Twitter Email
Answers.com

Trapezoidal rule

 
Statistics Dictionary: trapezium rule

A method of finding an approximate value for an integral, based on finding the sum of the areas of trapezia. Suppose we wish to find an approximate value for ∫ab f(x)dx. The interval axb is divided up into n sub-intervals, each of length h=(ba)/n, and the integral is approximated by ½h(y0+2y1+2y2+...+2yn−1+yn),where yr=f(a+rh). This is the sum of the areas of the individual trapezia, one of which is shown in the diagram. The error in using the trapezium rule is approximately proportional to 1/n2, so that if the number of sub-intervals is doubled, the error is reduced by a factor of 4. A more accurate method for approximating an integral is Simpson's rule.




Trapezium rule. A method of approximate integration. A slight underestimate (as shown) will often be cancelled by a similar slight overestimate from another trapezium. Using narrower intervals will improve accuracy.



Search unanswered questions...
Enter a question here...
Search: All sources Community Q&A Reference topics
Wikipedia: Trapezoidal rule
Top
The function f(x) (in blue) is approximated by a linear function (in red).
Illustration of the composite trapezoidal rule (with a non-uniform grid).
Illustration of the composite trapezoidal rule (with a uniform grid).

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

 \int_{a}^{b} f(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. It follows that

 \int_{a}^{b} f(x)\, dx \approx (b-a)\frac{f(a) + f(b)}{2}.

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:

\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].

This can alternatively be written as:

\int_a^b f(x)\,dx \approx \frac{b-a}{2n} \left(f(x_0) + 2f(x_1) + 2f(x_2)+\cdots+2f(x_{n-1}) + f(x_n) \right)

where

x_k=a+k \frac{b-a}{n},\text{ for }k=0, 1, \dots, n

(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

Error analysis

The error of the composite trapezoidal rule is the difference between the value of the integral and the numerical result:

 \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].

This error can be written as

 \text{error} = -\frac{(b-a)^3}{12n^2} f''(\xi),

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 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, then the error is harder to identify.

An asymptotic error estimate for n → ∞ is given by

 \text{error} = -\frac{(b-a)^2}{12n^2} \big( f'(b)-f'(a) \big) + O(n^{-3}). [2]

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

Python implementation

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

#!/usr/bin/env python 
def trapezoidal_rule(f, a, b, N):
    """Approximate the definite integral of f from a to b by the
    composite trapezoidal rule, using N subintervals"""
    return (b-a) * ( f(a)/2 + f(b)/2 + sum([f(a + (b-a)*k/N) for k in xrange(1,N)]) ) / N
 
#test
print trapezoidal_rule(lambda x:x**9, 0.0, 10.0, 100000)

MATLAB implementation

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

function [F] = trap(f,a,b,n)
%f=name of function, a=start value, b=end value, n=number of 
%iterations
%made by Carolina, Niklas, Benjamin and Damiano
h=(b-a)./n;
S=f(a);
i=1:1:n-1;
x=a+h.*i;
y=f(x);
S=S+2.*sum(y);
S=S+f(b);
F=h.*S./2;
F;
end

See also

Notes

  1. ^ Atkinson (1989), equation (5.1.7)
  2. ^ Atkinson (1989), equation (5.1.9)

References


External links


Best of the Web: Trapezoidal rule
Top

Some good "Trapezoidal rule" pages on the web:


Math
mathworld.wolfram.com
 
 
 

 

Copyrights:

Statistics Dictionary. A Dictionary of Statistics. Second edition revised. Copyright © Oxford University Press, 2008. All rights reserved.  Read more
Wikipedia. This article is licensed under the Creative Commons Attribution/Share-Alike License. It uses material from the Wikipedia article "Trapezoidal rule" Read more