Trapezoidal rule

This MedLibrary.org supplementary page on Trapezoidal rule is provided directly from the open source Wikipedia as a service to our readers. Please see the note below on authorship of this content, as well as the Wikipedia usage guidelines. To search for other content from our encyclopedia supplement, please use the form below:

The function f(x) (in blue) is approximated by a linear function (in red).
Illustration of the composite trapezium rule (with a non-uniform grid).

In mathematics, the trapezium rule (the British term) or trapezoidal rule (the American term) is a way to approximately calculate the definite integral

 \int_{a}^{b} f(x)\,dx.

The trapezium rule works by approximating the region under the graph of the function f(x) by a trapezium 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 trapezium rule on each of them. One obtains the composite trapezium 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}, for k=0, 1, \dots, n (one can also use a non-uniform grid).

The trapezium 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 trapezium rule for functions which are twice continuously differentiable; however for rougher functions the trapezium rule is likely to prove preferable. Moreover, the trapezium 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.

An advantage of the trapezium rule is that the sign of the error of the approximation is easily known. An integral approximated with this rule on a concave-up function will be an overestimate because the trapezoids include all of the area under the curve and extend over it. Using this method on 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.

Contents

Python implementation of the Trapezium rule

#!/usr/bin/env python 
 
def trapezium_rule(f, a, b):
  "Approximate the definite integral of f from a to b by Trapezium rule." 
  return (b - a) * ((f(a) + f(b))/2)
 
"Approximate the definite integral of f from a to b(subdivided) by Trapezium rule. n is the numbers of partitions "  
def trapezium_plus(f,a,b,n):	
  z = (b-a)/2*n 
  res = z 
  x = {0:0}
 
  for i in range(1, n+1):
  	xi= a + i*((b-a)/n)
 
  res = f(x1) + f(xn)
 
  for j in range(2, n):
  	xj = 2 * f(xj)	
 
  for k in range(1, n):
  	 res += xk	
 
  return res * z
 
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(trapezium_plus):\t' , trapezium_plus(sin, 0, 2*pi, n)
print '\nEND'

See also

References

External links

Wikipedia content modification information:

  • This page was last modified on 28 November 2008, at 22:54.

Wikipedia Authorship and Review

Wikipedia content provided here is not reviewed directly by MedLibrary.org. Wikipedia content is authored by an open community of volunteers and is not produced by or in any way affiliated with MedLibrary.org.

Wikipedia Usage Guidelines

This article is licensed under the GNU Free Documentation License. It uses material from the Wikipedia article on "Trapezoidal rule".

The URL for this specific entry is:

All Wikipedia text is available under the terms of the GNU Free Documentation License. (See Copyrights for details). Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.