Press, Teukolsly, Vetterling, Flannery - Numerical Recipes in C (523184), страница 43
Текст из файла (страница 43)
If the boundary also has this symmetry, then thedimension can be reduced. In three dimensions, for example, the integration of aspherically symmetric function over a spherical region reduces, in polar coordinates,to a one-dimensional integral.The next questions to be asked will guide your choice between two entirelydifferent approaches to doing the problem. The questions are: Is the shape of theboundary of the region of integration simple or complicated? Inside the region, isthe integrand smooth and simple, or complicated, or locally strongly peaked? Does162Chapter 4.Integration of Functionsthe problem require high accuracy, or does it require an answer accurate only toa percent, or a few percent?If your answers are that the boundary is complicated, the integrand is notstrongly peaked in very small regions, and relatively low accuracy is tolerable, thenyour problem is a good candidate for Monte Carlo integration.
This method is verystraightforward to program, in its cruder forms. One needs only to know a regionwith simple boundaries that includes the complicated region of integration, plus amethod of determining whether a random point is inside or outside the region ofintegration. Monte Carlo integration evaluates the function at a random sample ofpoints, and estimates its integral based on that random sample.
We will discuss it inmore detail, and with more sophistication, in Chapter 7.If the boundary is simple, and the function is very smooth, then the remainingapproaches, breaking up the problem into repeated one-dimensional integrals, ormultidimensional Gaussian quadratures, will be effective and relatively fast [1].
Ifyou require high accuracy, these approaches are in any case the only ones availableto you, since Monte Carlo methods are by nature asymptotically slow to converge.For low accuracy, use repeated one-dimensional integration or multidimensionalGaussian quadratures when the integrand is slowly varying and smooth in the regionof integration, Monte Carlo when the integrand is oscillatory or discontinuous, butnot strongly peaked in small regions.If the integrand is strongly peaked in small regions, and you know where thoseregions are, break the integral up into several regions so that the integrand is smoothin each, and do each separately.
If you don’t know where the strongly peaked regionsare, you might as well (at the level of sophistication of this book) quit: It is hopelessto expect an integration routine to search out unknown pockets of large contributionin a huge N -dimensional space. (But see §7.8.)If, on the basis of the above guidelines, you decide to pursue the repeated onedimensional integration approach, here is how it works. For definiteness, we willconsider the case of a three-dimensional integral in x, y, z-space. Two dimensions,or more than three dimensions, are entirely analogous.The first step is to specify the region of integration by (i) its lower and upperlimits in x, which we will denote x1 and x2 ; (ii) its lower and upper limits in y at aspecified value of x, denoted y1 (x) and y2 (x); and (iii) its lower and upper limitsin z at specified x and y, denoted z1 (x, y) and z2 (x, y). In other words, find thenumbers x1 and x2 , and the functions y1 (x), y2 (x), z1 (x, y), and z2 (x, y) such that& & &I≡dx dy dzf(x, y, z)&&x2dx=x1&y2 (x)dyy1 (x)(4.6.2)z2 (x,y)dz f(x, y, z)z1 (x,y)For example, a two-dimensional integral over a circle of radius one centered onthe origin becomes&&1dx−1√1−x2√− 1−x2dy f(x, y)(4.6.3)1634.6 Multidimensional Integralsouter integrationinner integrationyxFigure 4.6.1.Function evaluations for a two-dimensional integral over an irregular region, shownschematically.
The outer integration routine, in y, requests values of the inner, x, integral at locationsalong the y axis of its own choosing. The inner integration routine then evaluates the function atx locations suitable to it. This is more accurate in general than, e.g., evaluating the function on aCartesian mesh of points.Now we can define a function G(x, y) that does the innermost integral,&G(x, y) ≡z2 (x,y)f(x, y, z)dz(4.6.4)z1 (x,y)and a function H(x) that does the integral of G(x, y),&H(x) ≡y2 (x)G(x, y)dy(4.6.5)y1 (x)and finally our answer as an integral over H(x)&x2H(x)dxI=(4.6.6)x1In an implementation of equations (4.6.4)–(4.6.6), some basic one-dimensionalintegration routine (e.g., qgaus in the program following) gets called recursively:once to evaluate the outer integral I, then many times to evaluate the middle integralH, then even more times to evaluate the inner integral G (see Figure 4.6.1).
Currentvalues of x and y, and the pointer to your function func, are passed “over the head”of the intermediate calls through static top-level variables.164Chapter 4.Integration of Functionsstatic float xsav,ysav;static float (*nrfunc)(float,float,float);float quad3d(float (*func)(float, float, float), float x1, float x2)Returns the integral of a user-supplied function func over a three-dimensional region specifiedby the limits x1, x2, and by the user-supplied functions yy1, yy2, z1, and z2, as defined in(4.6.2). (The functions y1 and y2 are here called yy1 and yy2 to avoid conflict with the namesof Bessel functions in some C libraries). Integration is performed by calling qgaus recursively.{float qgaus(float (*func)(float), float a, float b);float f1(float x);nrfunc=func;return qgaus(f1,x1,x2);}float f1(float x)This is H of eq.
(4.6.5).{float qgaus(float (*func)(float), float a, float b);float f2(float y);float yy1(float),yy2(float);xsav=x;return qgaus(f2,yy1(x),yy2(x));}float f2(float y)This is G of eq. (4.6.4).{float qgaus(float (*func)(float), float a, float b);float f3(float z);float z1(float,float),z2(float,float);ysav=y;return qgaus(f3,z1(xsav,y),z2(xsav,y));}float f3(float z)The integrand f (x, y, z) evaluated at fixed x and y.{return (*nrfunc)(xsav,ysav,z);}The necessary user-supplied functions have the following prototypes:floatfloatfloatfloatfloatfunc(float x,float y,float z);yy1(float x);yy2(float x);z1(float x,float y);z2(float x,float y);The 3-dimensional function to be integrated.CITED REFERENCES AND FURTHER READING:Stroud, A.H. 1971, Approximate Calculation of Multiple Integrals (Englewood Cliffs, NJ: PrenticeHall).
[1]Dahlquist, G., and Bjorck, A. 1974, Numerical Methods (Englewood Cliffs, NJ: Prentice-Hall),§7.7, p. 318.Johnson, L.W., and Riess, R.D. 1982, Numerical Analysis, 2nd ed. (Reading, MA: AddisonWesley), §6.2.5, p. 307.Abramowitz, M., and Stegun, I.A. 1964, Handbook of Mathematical Functions, Applied Mathematics Series, Volume 55 (Washington: National Bureau of Standards; reprinted 1968 byDover Publications, New York), equations 25.4.58ff.Chapter 5.Evaluation of Functions5.0 IntroductionThe purpose of this chapter is to acquaint you with a selection of the techniquesthat are frequently used in evaluating functions. In Chapter 6, we will apply andillustrate these techniques by giving routines for a variety of specific functions.The purposes of this chapter and the next are thus mostly in harmony, but thereis nevertheless some tension between them: Routines that are clearest and mostillustrative of the general techniques of this chapter are not always the methods ofchoice for a particular special function.
By comparing this chapter to the next one,you should get some idea of the balance between “general” and “special” methodsthat occurs in practice.Insofar as that balance favors general methods, this chapter should give youideas about how to write your own routine for the evaluation of a function which,while “special” to you, is not so special as to be included in Chapter 6 or thestandard program libraries.CITED REFERENCES AND FURTHER READING:Fike, C.T.
1968, Computer Evaluation of Mathematical Functions (Englewood Cliffs, NJ: PrenticeHall).Lanczos, C. 1956, Applied Analysis; reprinted 1988 (New York: Dover), Chapter 7.5.1 Series and Their ConvergenceEverybody knows that an analytic function can be expanded in the neighborhoodof a point x0 in a power series,f(x) =∞ak (x − x0 )k(5.1.1)k=0Such series are straightforward to evaluate. You don’t, of course, evaluate the kthpower of x − x0 ab initio for each term; rather you keep the k − 1st power and updateit with a multiply.
Similarly, the form of the coefficients a is often such as to makeuse of previous work: Terms like k! or (2k)! can be updated in a multiply or two.165166Chapter 5.Evaluation of FunctionsHow do you know when you have summed enough terms? In practice, theterms had better be getting small fast, otherwise the series is not a good techniqueto use in the first place. While not mathematically rigorous in all cases, standardpractice is to quit when the term you have just added is smaller in magnitude thansome small times the magnitude of the sum thus far accumulated. (But watch outif isolated instances of ak = 0 are possible!).A weakness of a power series representation is that it is guaranteed not toconverge farther than that distance from x0 at which a singularity is encounteredin the complex plane.