Misplaced Pages

Trapezoidal rule: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content addedVisualWikitext
Revision as of 12:53, 17 October 2013 editD.Lazard (talk | contribs)Extended confirmed users33,954 edits Reverted 1 edit by 170.185.167.19 (talk). (TW)← Previous edit Revision as of 12:48, 16 December 2013 edit undo200.30.223.19 (talk) Sample implementations: this is not a how-to guide, it's an encyclopaedia.Tag: section blankingNext edit →
Line 61: Line 61:
{{Expand section|date=January 2010}} {{Expand section|date=January 2010}}
For various classes of functions that are not twice-differentiable, the trapezoidal rule has sharper bounds than ].<ref name="cun02" /> For various classes of functions that are not twice-differentiable, the trapezoidal rule has sharper bounds than ].<ref name="cun02" />

==Sample implementations==
===Spreadsheets===
The trapezoidal rule is easily implemented in a spreadsheet. As an example, we show the integral of <math>f(x) = x^2</math>.
]

=== Python ===

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

<source lang="python">
#!/usr/bin/env python
from __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
</source>

=== ] and ]===

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

<source lang="MATLAB">
function s=trap(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;

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;
</source>

An alternative implementation that uses the MATLAB's vectorization is

<source lang="MATLAB">
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;
</source>

=== C++ ===

In ], one can implement the trapezoidal rule as follows.
<source lang="cpp">
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 - x) * (y + y);
}
return sum * 0.5;
}
</source>
Here, <code>x</code> and <code>y</code> can be any object of a class implementing <code>operator</code> and <code>size()</code>.


==See also== ==See also==

Revision as of 12:48, 16 December 2013

This article is about the quadrature rule for approximating integrals. For the implicit trapezoidal rule for solving initial value problems, see Trapezoidal rule (differential equations). For the explicit trapezoidal rule for solving initial value problems, see Heun's method.
The function f(x) (in blue) is approximated by a linear function (in red).

In numerical analysis, the trapezoidal rule (also known as the trapezoid rule or trapezium rule) is a technique for approximating the definite integral

a b f ( x ) d x . {\displaystyle \int _{a}^{b}f(x)\,dx.}

The trapezoidal rule works by approximating the region under the graph of the function f ( x ) {\displaystyle f(x)} as a trapezoid and calculating its area. It follows that

a b f ( x ) d x ( b a ) [ f ( a ) + f ( b ) 2 ] . {\displaystyle \int _{a}^{b}f(x)\,dx\approx (b-a)\left.}

Applicability and alternatives

The trapezoidal rule is one of a family of formulas for numerical integration called Newton–Cotes 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.

Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions are integrated over their periods, which can be analyzed in various ways.

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.

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

a b f ( x ) d x h 2 k = 1 N ( f ( x k + 1 ) + f ( x k ) ) {\displaystyle \int _{a}^{b}f(x)\,dx\approx {\frac {h}{2}}\sum _{k=1}^{N}\left(f(x_{k+1})+f(x_{k})\right)} = b a 2 N ( f ( x 1 ) + 2 f ( x 2 ) + 2 f ( x 3 ) + + 2 f ( x N ) + f ( x N + 1 ) ) . {\displaystyle {}={\frac {b-a}{2N}}(f(x_{1})+2f(x_{2})+2f(x_{3})+\ldots +2f(x_{N})+f(x_{N+1})).}

Non-uniform grid

When the grid spacing is non-uniform, one can use the formula

a b f ( x ) d x 1 2 k = 1 N ( x k + 1 x k ) ( f ( x k + 1 ) + f ( x k ) ) . {\displaystyle \int _{a}^{b}f(x)\,dx\approx {\frac {1}{2}}\sum _{k=1}^{N}\left(x_{k+1}-x_{k}\right)\left(f(x_{k+1})+f(x_{k})\right).}

Error analysis

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

error = a b f ( x ) d x b a N [ f ( a ) + f ( b ) 2 + k = 1 N 1 f ( a + k b a N ) ] {\displaystyle {\text{error}}=\int _{a}^{b}f(x)\,dx-{\frac {b-a}{N}}\left}

There exists a number ξ between a and b, such that

error = ( b a ) 3 12 N 2 f ( ξ ) {\displaystyle {\text{error}}=-{\frac {(b-a)^{3}}{12N^{2}}}f''(\xi )}

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

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

  1. Fourier series
  2. Residue calculus
  3. Euler–Maclaurin summation formula:

An asymptotic error estimate for N → ∞ is given by

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

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 definition of classes of smoothness of the functions.

Periodic functions

The trapezoidal rule often converges very quickly for periodic functions. 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.

More detailed analysis can be found in.

"Rough" functions

This section needs expansion. You can help by adding to it. (January 2010)

For various classes of functions that are not twice-differentiable, the trapezoidal rule has sharper bounds than Simpson's rule.

See also

Notes

  1. ^ (Cruz-Uribe & Neugebauer 2002)
  2. ^ (Rahman & Schmeisser 1990)
  3. ^ (Weideman 2002)
  4. Atkinson (1989, equation (5.1.7))
  5. ^ (Weideman 2002, p. 23, section 2)
  6. Atkinson (1989, equation (5.1.9))
  7. Atkinson (1989, p. 285)

References

External links

Category:
Trapezoidal rule: Difference between revisions Add topic