]>
In this chapter we show you how to carry out numerical integrations. We will derive the Simpson and trapezoid rules because they are so easy and give a good idea of how integration algorithms work, but we will just sketch the other algorithms and quote some error estimates.
An experiment has measured , the number of particles entering a counter, per unit time, as a function of time. Your problem is to integrate this spectrum to obtain the number of particles that entered the counter in the first second:
As an explicit assessment, in § 4.8 we integrate an exponential spectrum. Nonetheless, the methods work equally well for integrating a table of numbers.The integration of a function may require some cleverness to do analytically, but it is relatively straightforward on a computer. The Riemann definition of an integral is the limit of the sum over boxes as the width of the box approaches zero:
For our problem, the function is the spectrum as a function of time, . A traditional way to measure the area numerically is to take a piece of graph paper and count the number of boxes or quadrilaterals lying below the curve of the function. For this reason numerical integration is also called numerical quadrature , even when it gets beyond the box-counting stage.The integral of a function is approximated numerically by the equivalent of a sum over boxes:
In this example, is evaluated at points in the interval , and the function are summed with each term in the sum weighted by . While, in general, the sum in ( 4.3 ) will give the exact integral only when , for certain classes of functions it may be exact for finite . The different integration algorithms amount to different ways of choosing the points and weights. Generally, the precision increases as gets larger, with roundoff error eventually limiting the increase. Because the "best" approximation depends on the specific behavior of the function , there is no universally best approximation. In fact, some of the automated integration schemes found in subroutine libraries will switch from one method to another until they find one that works well.
![]()
|
More accurate integration rules are possible if the points are not constrained to be equally spaced. Gaussian quadrature methods have the ingenious ability to exactly 0 integrate the product of a function times a ( ) degree polynomial, with only function evaluations. This is no harder than evaluating the sum ( 4.3 ), and explains why so many computational scientists carry their Gaussian quadrature routines with them wherever they go.
In general, results with Gaussian quadrature will be superior to results with equally spaced points as long as there are no singularities in the integrand or its derivative. In the latter cases, the use of Simpson's rule may help avoid a catastrophe, but
|
you would be better off to remove singularities analytically from your integration before attempting any numerical quadrature . |
You may be able to do this by breaking the interval down into several subintervals, so the singularity is at an endpoint where a Gauss point never falls, or by a change of variable. For example
Likewise, if your integrand has a very slow variation in some region, you can speed up the integration by changing to a variable that compresses that region and places few points there. Conversely, if your integrand has a very rapid variation in some region, you may want to change to variables that expand that region to ensure that no oscillations are missed.
The trapezoid and Simpson integration rules use values of at evenly spaced values of . They use points , evenly spaced at a distance apart throughout the integration region and include the endpoints . This means that there are intervals of length :
Notice that we start our counting at , and that Simpson's rule requires an odd number of points .
In Fig. 4.2 we see that in the trapezoid rule we take the integration interval and construct a trapezoid of width in it. This approximates by a straight line in that interval , and uses the average height as the value for . The area of a single trapezoid is in this way
In terms of our standard integration formula ( 4.3 ), the "rule" in ( 4.9 ) is for points with weight .
![]()
|
In Simpson's rule we approximate the function by a parabola
for each interval, still keeping the intervals equally spaced. The area of each section is then the integral of this parabola This is equivalent to integrating the Taylor series up to the quadratic term. In order to relate the parameters , , and to the function, we consider an interval from to , in which case But we notice that In this way we can express the integral as the weighted sum over the values of the function at 3 points: Because 3 values of the function are needed, we generalize this result to our problem by evaluating the integral over two adjacent intervals, in which case we evaluate the function at the two endpoints and the middle: Simpson's rule requires the elementary integration to be over pairs of intervals, and this requires that the total number of intervals be even or the number of points be odd. In order to apply Simpson's rule to the entire interval, we add up the contributions from each pair of subintervals, counting all but the first and last endpoints twice: In terms of our standard integration rule ( 4.3 ), we have The sum of these weights provides a useful check on your integration: Remember , must be odd.In general, you want to choose an integration rule that gives an accurate answer using the least number of integration points. We obtain a feel for the absolute approximation or algorithmic error and the relative error , by expanding in a Taylor series around the midpoint of the integration interval. We then multiply that error by the number of intervals to estimate the error for the entire region . For the trapezoid and Simpson rules this yields
We see that the third derivative term in Simpson's rule cancels (much like the central difference method in differentiation). The equations ( 4.21 )-( 4.22 ) are illuminating by showing how increasing the sophistication of an integration rule leads to an error that falls off with a higher inverse power of , yet that is also proportional to higher derivatives of . Consequently, for small intervals and functions with well-behaved high derivatives, Simpson's rule should converge more rapidly than the trapezoid rule.
To be more specific, we assume that after steps the relative roundoff error is random and of the form
where is the machine precision ( for single precision and for double precision ). We want to determine an that minimizes the total error, that is, the sum of the approximation and roundoff error This occurs, approximately, when the two relative errors are of equal magnitude, which we approximate even further by assuming that the two errors are equal To continue the search for optimum for a general function , we set the scale of function size by assuming and the scale of length by assuming The estimate ( 4.26 ), when applied to the trapezoid rule , yieldsBecause the machine precision differs for single- and double-precision calculations, the optimum number of steps for the trapezoid rule has the following two values:
The corresponding errors are The estimate ( 4.26 ) when applied to Simpson's rule yieldsFor single and double precision, this now yields
The corresponding errors are These results are illuminating because they show thatIt is often useful to rewrite the basic integration formula ( 4.3 ) such that we separate a weighting function from the integrand:
In the Gaussian quadrature approach to integration, the weights are chosen to make the approximation error actually vanish if was a degree polynomial. To obtain this incredible optimization, the points end up having a very specific distribution over . (If is only given by an equally spaced table, then a Simpson or trapezoid rule is simpler, although any integration algorithm can be used if the table is interpolated. 1 ) In general, if is smooth, or can be made smooth by factoring out some , Gaussian algorithms produce higher accuracy than lower-order ones, or conversely, the same accuracy with a fewer number of points. If the function being integrated is not smooth (for example, if it has some noise in it), then using a higher-order method such as Gaussian quadrature may well lead to lower accuracy. Sometimes the function may not be smooth because it has different behaviors in different regions. In these cases it makes sense to integrate each region separately with a low-order quadrature rule and then add the answers together. In fact, some of the "smart" integration subroutines will decide for themselves how many intervals to use and what rule to use in each interval.All the rules indicated in Table ?? are Gaussian with the general form ( 4.37 ). We can see that in one case the weighting function is an exponential, in another a Gaussian, and in several an integrable singularity. In contrast to the equally-spaced rules, there is never an integration point at the extremes of the intervals, yet all of the points and weights change as the number of points changes.
The values of and in ( 4.37 ) help define each integration scheme. If the integral you need to evaluate is over the range and this differs from , you must map your onto , possibly using one of the methods discussed below.
Although we will leave it to the references on numerical methods for the derivation of the Gauss points and weights, we note here that for ordinary Gaussian (Gauss-Legendre) integration, the points turn out to be the zeros of the Legendre polynomials, with the weights related to the derivatives:
Subroutines to generate these points and weights are standard in mathematical function libraries, are found in tables such as those in [ ? ], or can be computed. The gauss subroutines we provide on the diskette and the Web also scales the points to a specified region. As a check that your points are correct, you may want to compare them to the four-point set we give in Table ?? .Our standard convention ( 4.3 ) for the general interval is
But when the points and weights, , are for a fixed integration range (such as Gaussian), the programmer must scale the Gaussian interval to . Here are some mappings we have found useful in our work (they are in the subroutine gauss given on the diskette and Web). In the scalings below, are the elementary Gaussian points and weights for the interval , and we want to scale to .You can see, that if your integration range extends out to infinity, there will be points at large but not infinite . As you keep increasing the number of grid points , the largest moves farther and farther out.
Write a program to integrate an arbitrary function numerically using the trapezoid rule, the Simpson rule, and Gaussian quadrature. Use single precision in order to show more quickly the effects of error. (This may not be possible if your quadrature routines, such as the ones we supply on the diskette and the Web, are written in only double precision.)
For our problem we assume exponential decay so that there actually is an analytic answer:
Compare the relative error
for the trapezoid rule, Simpson's rule, and Gaussian quadrature.Try values of 2, 10, 20, 40, 80, 160, ….
Try two integrals for which the answers are less obvious:
Explain why the computer may have trouble with these integrals.As in the case of numerical differentiation, we can use the known functional dependence of the error on interval size to reduce the error in integration. For simple rules like the trapezoid and Simpson's, we have the analytic estimates ( 4.26 ), while for others you may have to experiment to determine the dependence. For example, if and are the values of the integral determined with the trapezoid rule using interval and , respectively, we know the integrals have expansions with a leading error term proportional to :
Consequently, we make the term vanish by computing the combination
Clearly this particular trick (Romberg's extrapolation) works only if the term dominates the error, and then only if the derivatives of the function are well behaved. An analogous extrapolation can also be made for other algorithms.In Table ?? we gave the weights for several equal-interval rules. Whereas the Simpson rule used two intervals, the rule uses three, and the Milne 3 rule four. (These are single-interval rules and must be strung together to obtain a rule extended over the entire integration range. This means that the points that end one interval and begin the next get weighted twice.) You can easily determine the number of elementary intervals integrated over, and check whether you and we have written the weights right, by summing the weights for any rule. The sum is the integral of and must equal times the number of intervals (which, in turn, equals ):