Trapezoidal rule

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

Template:Short description Script error: No such module "about".

File:Trapezoidal rule illustration.svg
The function f(x) (in blue) is approximated by a linear function (in red).

In calculus, the trapezoidal rule (informally trapezoid rule; or in British English trapezium rule)Template:Efn is a technique for numerical integration, i.e. approximating the definite integral: abf(x)dx.

File:WikiTrap.gif
An animation that shows what the trapezoidal rule is and how the error in approximation decreases as the step size decreases

The trapezoidal rule works by approximating the region under the graph of the function f(x) as a trapezoid and calculating its area. This is easily calculated by noting that the area of the region is made up of a rectangle with width (ba) and height f(a), and a triangle of width (ba) and height f(b)f(a).

Therefore, abf(x)dx(ba)f(a)Area of rectangle+12(ba)[f(b)f(a)]Area of triangle=(ba)(f(a)+12f(b)12f(a))=(ba)(12f(a)+12f(b))=12(ba)[f(a)+f(b)].

The rule can also be derived by replacing the integrand with the equation of the line joining points (a,f(a)) and (b,f(b)), which using the two point form of the equation of a line, is y=(xa)f(b)f(a)ba+f(a).

Therefore, abf(x)dxab(xa)f(b)f(a)ba+f(a)dx=[12(xa)2f(b)f(a)ba+xf(a)]x=ax=b=[12(ba)2f(b)f(a)ba+bf(a)]af(a)=12(ba)[f(b)f(a)]+(ba)f(a)=12(ba)[f(a)+f(b)], as before.

File:Integration num trapezes notation.svg
Illustration of "chained trapezoidal rule" used on an irregularly spaced partition of [a,b]

The integral can be even better approximated by partitioning the integration interval, applying the trapezoidal rule to each subinterval and summing the results. In practice, this "chained" (or "composite") trapezoidal rule is usually what is meant by "integrating with the trapezoidal rule". Let {xk} be a partition of [a,b] such that a=x0<x1<<xN1<xN=b, and Δxk be the length of the k-th subinterval (that is, Δxk=xkxk1), then abf(x)dxk=1Nf(xk1)+f(xk)2Δxk. The trapezoidal rule may be viewed as the result obtained by averaging the left and right Riemann sums and is sometimes defined this way.

The approximation becomes more accurate as the resolution of the partition increases (that is, for larger N, all Δxk decrease).

When the partition has a regular spacing, as is often the case, that is, when all the Δxk have the same value Δx, the formula can be simplified for calculation efficiency by factoring Δx out: abf(x)dxΔx(f(x0)+f(xN)2+k=1N1f(xk)).

As discussed below, it is also possible to place error bounds on the accuracy of the value of a definite integral estimated using a trapezoidal rule.

History

A 2016 Science paper reports that the trapezoid rule was in use in Babylon before 50 BCE for integrating the velocity of Jupiter along the ecliptic.[1][2]

Numerical implementation

Non-uniform grid

When the grid spacing is non-uniform, one can use the formula abf(x)dxk=1Nf(xk1)+f(xk)2Δxk, where Δxk=xkxk1, or more a computationally efficient formula abf(x)dx12(f(x0)Δ+1x0+f(xN)Δ1xN+k=1N1f(xk)Δ±1xk), where Δ+1x0=x1x0, Δ1xN=xNxN1, Δ±1xk=xk+1xk1 are the corresponding forward, backward, and central differences.

Uniform grid

For a domain partitioned by N equally spaced points, considerable simplification may occur.

Let Δx=baN and xk=a+kΔx for k=0,1,,N. The approximation to the integral becomes abf(x)dxΔx2k=1N[f(xk1)+f(xk)]=Δx(12f(x0)+12f(xN)+k=1N1f(xk)).

Sometimes this expression is written as Δx )k=0Nf(xk),

where the symbol Template:Tmath indicates that the first and last terms are halved.

Error analysis

File:Trapezium2.gif
An animation showing how the trapezoidal rule approximation improves with more strips for an interval with a=2 and b=8. As the number of intervals N increases, so too does the accuracy of the result.

The error of the composite trapezoidal rule is the difference between the value of the integral and the numerical result: E=abf(x)dxbaN[f(a)+f(b)2+k=1N1f(a+kbaN)]

There exists a number ξ between a and b, such thatTemplate:Sfn E=(ba)312N2f(ξ)

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

An asymptotic error estimate for N → ∞ is given by E=(ba)212N2[f(b)f(a)]+O(N3). Further terms in this error estimate are given by the Euler–Maclaurin summation formula.

Several techniques can be used to analyze the error, including:Template:Sfn

  1. Fourier series
  2. Residue calculus
  3. Euler–Maclaurin summation formulaTemplate:SfnTemplate:Sfn
  4. Polynomial interpolationTemplate:Sfn

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.Template:Sfn

Proof

First suppose that h=baN and ak=a+(k1)h. Let gk(t)=12t[f(ak)+f(ak+t)]akak+tf(x)dx be the function such that |gk(h)| is the error of the trapezoidal rule on one of the intervals, [ak,ak+h]. Then dgkdt=12[f(ak)+f(ak+t)]+12tf(ak+t)f(ak+t), and d2gkdt2=12tf(ak+t).

Now suppose that |f(x)||f(ξ)|, which holds if f is sufficiently smooth. It then follows that |f(ak+t)|f(ξ) which is equivalent to f(ξ)f(ak+t)f(ξ), or f(ξ)t2gk(t)f(ξ)t2.

Since gk(0)=0 and gk(0)=0, 0tgk(x)dx=gk(t) and 0tgk(x)dx=gk(t).

Using these results, we find f(ξ)t24gk(t)f(ξ)t24 and f(ξ)t312gk(t)f(ξ)t312

Letting t=h we find f(ξ)h312gk(h)f(ξ)h312.

Summing all of the local error terms we find k=1Ngk(h)=baN[f(a)+f(b)2+k=1N1f(a+kbaN)]abf(x)dx.

But we also have k=1Nf(ξ)h312k=1Ngk(h)k=1Nf(ξ)h312 and k=1Nf(ξ)h312=f(ξ)h3N12,

so that

f(ξ)h3N12baN[f(a)+f(b)2+k=1N1f(a+kbaN)]abf(x)dxf(ξ)h3N12.

Therefore the total error is bounded by

error=abf(x)dxbaN[f(a)+f(b)2+k=1N1f(a+kbaN)]=f(ξ)h3N12=f(ξ)(ba)312N2.

Periodic and peak functions

The trapezoidal rule converges rapidly for periodic functions. This is an easy consequence of the Euler–Maclaurin summation formula, which says that if f is p times continuously differentiable with period T, then k=0N1f(kh)h=0Tf(x)dx+k=1p/2B2k(2k)!(f(2k1)(T)f(2k1)(0))(1)php0TB~p(x/T)f(p)(x)dx, where h:=T/N, and B~p is the periodic extension of the p-th Bernoulli polynomial.[3] Due to the periodicity, the derivatives at the endpoint cancel, and we see that the error is O(hp).

A similar effect is available for peak-like functions, such as Gaussian, Exponentially modified Gaussian and other functions with derivatives at integration limits that can be neglected.[4] The evaluation of the full integral of a Gaussian function by trapezoidal rule with 1% accuracy can be made using just 4 points.[5] Simpson's rule requires 1.8 times more points to achieve the same accuracy.[5]Template:Sfn

"Rough" functions

For functions that are not in C2, the error bound given above is not applicable. Still, error bounds for such rough functions can be derived, which typically show a slower convergence with the number of function evaluations N than the O(N2) behaviour given above. Interestingly, in this case the trapezoidal rule often has sharper bounds than Simpson's rule for the same number of function evaluations.Template:Sfn

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.Template:Sfn

Moreover, the trapezoidal rule tends to become extremely accurate when periodic functions are integrated over their periods, which can be analyzed in various ways. Convergence usually is exponential or faster.Template:SfnTemplate:Sfn A similar effect is available for peak functions.[5]Template:Sfn

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 examples

Approximating the natural logarithm of 3

Since 131xdx=ln3ln1=ln3, we can use the trapezoidal rule to approximate the integral, thereby generating an approximation of ln3.

Applying the rule with n=3 segments gives ln3=131xdx13(1+65+67+13)=3563151.13015873, which has absolute error of 0.031546 and a relative error of 2.87148%.

Applying the rule with n=6 segments gives ln3=131xdx16(1+32+65+1+67+34+13)=278925201.10674603, which has absolute error of 8.13×103 and a relative error of 0.74036%.

Approximating the integral of a product

The following integral is given: 0.11.35xe2xdx. Template:Ordered list Solution Template:Ordered list

See also

Notes

Template:Notelist

  1. Script error: No such module "Citation/CS1".
  2. Script error: No such module "citation/CS1".
  3. Script error: No such module "citation/CS1".
  4. Script error: No such module "Citation/CS1".
  5. a b c Script error: No such module "Citation/CS1".

References

<templatestyles src="Refbegin/styles.css" />

  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".
  • Script error: No such module "citation/CS1".

External links

Template:Sister project

Template:Numerical integration Template:Calculus topics