Welcome to ornacle.com on July 6 2009.
This is an internet experiment running to monitor browsing habbits of individuals through wikipedia contents.

Trapezoidal rule

From Wikipedia, the free encyclopedia

Jump to: navigation, search
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).

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) by 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

[edit] 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 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

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

[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

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

[edit] References


[edit] External links

Personal tools

Visit joltnews for the latest headlines
Visit bloit.com for company information
Geed Media does computer consulting on long island.
This page viewed times. See Logs