254 Appendix E: A Detailed Look at f

In many cases you will be familiar enough with the function you want to integrate that you’ll know whether the function has any quick wiggles relative to the interval of integration. If you're not familiar with the function, and you have reason to suspect that it may cause problems, you can quickly plot a few points by evaluating the function using the subroutine you wrote for that purpose.

If for any reason, after obtaining an approximation to an integral, you have reason to suspect its validity, there's a very simple procedure you can use to verify it: subdivide the interval of integration into two or more adjacent subintervals, integrate the function over each subinterval, then add the resulting approximations. This causes the function to be sampled at a brand new set of sample points, thereby more likely revealing any previously hidden spikes. If the initial approximation was valid, it will equal the sum of the approximations over the subintervals.

Conditions That Prolong Calculation Time

In the preceding example (page 251), you saw that the algorithm gave an incorrect answer because it never detected the spike in the function. This happened because the variation in the function was too quick relative to the width of the interval of integration. If the width of the interval were smaller, you would get the correct answer; but it would take a very long time if the interval were still too wide.

For certain integrals such as the one in that example, calculating the integral may be unduly prolonged because the width of the interval of integration is too large relative to certain features of the functions being integrated. Consider an integral where the interval of integration is wide enough to require excessive calculation time but not so wide that it would be calculated incorrectly. Note that because f(x) = xe-xapproaches zero very quickly as x approaches ∞, the contribution to the integral of the function at large values of x is negligible. Therefore, you can evaluate the integral by replacing ∞, the upper limit of integration, by a number not so large as 1099, say 103.