Trapezoidal Rule

Description

The trapezoidal rule approximates the integral of a function f(x) on the closed and bounded interval [a,a+h] of length h > 0 by the (signed) area of the trapezoid formed by the line segments joining (a,0) to (a+h,0), (a+h,0) to (a+h,f(a+h)), (a+h,f(a+h)) to (a,f(a)) and (a,f(a)) to (a,0). The composite trapezoidal rule is used to approximate the integral of a function f(x) over a closed and bounded interval [a,b] where a < b, by decomposing the interval [a,b] into n > 1 subintervals of equal length h = (b - a) / n, then adding the results of applying the trapezoidal rule to each subinterval. By abuse of language both the composite trapezoidal rule and the trapezoidal rule sometimes are referred to simply as the trapezoidal rule.
Let I[a,b](f) be the integral of f(x) over the closed and bounded interval [a,b], and let Th(f) be the result of applying the trapezoidal rule with n subintervals of length h, i.e.
Th(f)=h [ f(a)/2 + f(a+h) + ··· + f(b-h) + f(b)/2 ].
The Euler-Maclaurin summation formula relates I[a,b](f) and Th(f)
Th(f) = I[a,b](f) + (h2/12) [ f'(b) - f'(a) ] - (h4/720) [ f'''(b) - f'''(a) ] + ··· + K h 2p-2 [f(2p-3)(b) - f(2p-3)(a) ] + O(h 2p) ,
where f', f''', and f(2p-3) are the first, third and (p-3)rd derivatives of f and K is a constant.
 
The last term, O(h 2p) is important. Given an infinitely differentiable function in which the first 2p-3 derivatives vanish at both endpoints of the interval of integration, it is not true that Th(f) = I[a,b](f) but rather what the theorem says is that
limh->0( Th(f) - I[a,b](f) ) / h2p < M,
where M > 0.
 
 
If f is at least twice differentiable on the interval [a,b], then applying the mean-value theorem to
Th(f) - I[a,b](f) = (h2/12) [ f'(b) - f'(a) ] - (h4/720) [ f'''(b) - f'''(a) ] + ··· + K h 2p-2 [f(2p-3)(b) - f(2p-3)(a) ] + O(h 2p)
yields the standard truncation error expression
Th(f) - I[a,b](f) = (h2/12) (b - a) f''(c), for some point c where a <= c <= b.
A corollary of which is that if f''(x) = 0 for all x in [a,b], i.e. if f(x) is linear, then the trapezoidal rule is exact.
 
The Euler-Maclaurin summation formula also shows that usually n should be chosen large enough so that h = (b - a) / n < 1. For example, if h = 0.1 then
T0.1(f) = I[a,b](f) + 0.00083 [ f'(b) - f'(a) ] - (0.00000014) [ f'''(b) - f'''(a) ] + ···
and if h = 0.01 then
T0.01(f) = I[a,b](f) + 0.0000083 [ f'(b) - f'(a) ] - (0.000000000014) [ f'''(b) - f'''(a) ] + ···
while if h = 10 then
T10(f) = I[a,b](f) + 8.3333 [ f'(b) - f'(a) ] - 13.89 [ f'''(b) - f'''(a) ] + ···
However, if the function f(x) is a linear, then n may be chosen to be 1.
 
Besides the truncation error described above, the algorithm is also subject to the usual round-off errors and errors due to number of significant bits in the machine representation of floating point numbers.
 
The source code below is programmed in double precision, but the number of significant bits can easily be extended to extended precision by changing double to long double and by affixing an L to any floating point number e.g. 0.5 to 0.5L.
 
Mathematically addition is associative, (a+b)+c = a + (b+c), but if intermediate results are rounded to machine precision, machine addition is not necessarily associative. E.g. consider a decimal machine with 3 significant digits, then ( ( 0.400 + 0.400 ) + 0.400 ) + 122.0 = 123.0 > 122.0 = 0.400 + ( 0.400 + ( 0.400 + 122.0 ) ). In order to reduce the effect of intermediate result round-off errors when adding a series of floating point numbers, there are two versions of the rectangular rule, one which adds left to right
Th(f)=h [ (((···( ( f(a)/2 + f(a+h) ) + f(a+2h) ) + ··· + f(b-2h) ) + f(b)/2 ]
and the other which adds right to left
Th(f)=h [ f(a)/2 + ( f(a+h) + ( f(a+2h) + ··· + (f(b-h) + f(b) )···))) ].
In general, if the function is increasing in the interval of integration, addition should be performed left to right and if the function is decreasing in the interval of integration, addition should be performed right to left.

Function List

C Source