正在加载图片...
320 Chapter 7.Random Numbers we have preserved original variable names.The parameter NDMX is what we have called K, the maximum number of increments along each axis;MXDIM is the maximum value of d;some other parameters are explained in the comments. The vegas routine performs m =itmx statistically independent evaluations of the desired integral,each with N=ncall function evaluations.While statistically independent, these iterations do assist each other,since each one is used to refine the sampling grid for the next one.The results of all iterations are combined into a single best answer,and its estimated error,by the relations 12 /best=】 (7.8.18) 8 Also returned is the quantity (I:-Ibest)2 (7.8.19) m-14 σ2 ICAL If this is significantly larger than 1,then the results of the iterations are statistically inconsistent,and the answers are suspect. The input flag init can be used to advantage.One might have a call with init=0, ncall=1000,itmx=5 immediately followed by a call with init=1,ncall=100000.itmx=1. The effect would be to develop a sampling grid over 5 iterations of a small number of samples, then to do a single high accuracy integration on the optimized grid. Note that the user-supplied integrand function,fxn,has an argument wgt in addition to the expected evaluation point x.In most applications you ignore wgt inside the function. Occasionally,however,you may want to integrate some additional function or functions along with the principal function f.The integral of any such function g can be estimated by Ig=>wig(x) (7.8.20) 灵兰 where the wi's and x's are the arguments wgt and x,respectively.It is straightforward to accumulate this sum inside your function fxn,and to pass the answer back to your main 6 program via global variables.Of course,g(x)had better resemble the principal function f to some degree,since the sampling will be optimized for f. COMPUTING #include <stdio.h> 19200 #include <math.h> (ISBN #include "nrutil.h" #define ALPH 1.5 色 #define NDMX 50 #define MXDIM 10 Numerical Recipes 43108 #define TINY 1.0e-30 extern long idum; For random number initialization in main. (outside void vegas(float regn[],int ndim,float (*fxn)(float []float),int init, Software. unsigned long ncall,int itmx,int nprn,float *tgral,float *sd, North float *chi2a) Performs Monte Carlo integration of a user-supplied ndim-dimensional function fxn over a rectangular volume specified by regn[1..2*ndim],a vector consisting of ndim "lower left" coordinates of the region followed by ndim "upper right"coordinates.The integration consists of itmx iterations,each with approximately ncall calls to the function.After each iteration the grid is refined;more than 5 or 10 iterations are rarely useful.The input flag init signals whether this call is a new start,or a subsequent call for additional iterations (see comments below).The input flag nprn (normally 0)controls the amount of diagnostic output.Returned answers are tgral (the best estimate of the integral),sd (its standard deviation),and chi2a (x-per degree of freedom,an indicator of whether consistent results are being obtained).See text for further details. float ran2(long *idum);320 Chapter 7. Random Numbers Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copyin Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by Numerical Recipes Software. Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5) g of machine￾readable files (including this one) to any server computer, is strictly prohibited. To order Numerical Recipes books or CDROMs, visit website http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America). we have preserved original variable names. The parameter NDMX is what we have called K, the maximum number of increments along each axis; MXDIM is the maximum value of d; some other parameters are explained in the comments. The vegas routine performs m = itmx statistically independent evaluations of the desired integral, each with N = ncall function evaluations. While statistically independent, these iterations do assist each other, since each one is used to refine the sampling grid for the next one. The results of all iterations are combined into a single best answer, and its estimated error, by the relations Ibest = m i=1 Ii σ2 i  m i=1 1 σ2 i σbest =  m i=1 1 σ2 i −1/2 (7.8.18) Also returned is the quantity χ2 /m ≡ 1 m − 1 m i=1 (Ii − Ibest) 2 σ2 i (7.8.19) If this is significantly larger than 1, then the results of the iterations are statistically inconsistent, and the answers are suspect. The input flag init can be used to advantage. One might have a call with init=0, ncall=1000, itmx=5 immediately followed by a call with init=1, ncall=100000, itmx=1. The effect would be to develop a sampling grid over 5 iterations of a small number of samples, then to do a single high accuracy integration on the optimized grid. Note that the user-supplied integrand function, fxn, has an argument wgt in addition to the expected evaluation point x. In most applications you ignore wgt inside the function. Occasionally, however, you may want to integrate some additional function or functions along with the principal function f. The integral of any such function g can be estimated by Ig = i wig(x) (7.8.20) where the wi’s and x’s are the arguments wgt and x, respectively. It is straightforward to accumulate this sum inside your function fxn, and to pass the answer back to your main program via global variables. Of course, g(x) had better resemble the principal function f to some degree, since the sampling will be optimized for f. #include <stdio.h> #include <math.h> #include "nrutil.h" #define ALPH 1.5 #define NDMX 50 #define MXDIM 10 #define TINY 1.0e-30 extern long idum; For random number initialization in main. void vegas(float regn[], int ndim, float (*fxn)(float [], float), int init, unsigned long ncall, int itmx, int nprn, float *tgral, float *sd, float *chi2a) Performs Monte Carlo integration of a user-supplied ndim-dimensional function fxn over a rectangular volume specified by regn[1..2*ndim], a vector consisting of ndim “lower left” coordinates of the region followed by ndim “upper right” coordinates. The integration consists of itmx iterations, each with approximately ncall calls to the function. After each iteration the grid is refined; more than 5 or 10 iterations are rarely useful. The input flag init signals whether this call is a new start, or a subsequent call for additional iterations (see comments below). The input flag nprn (normally 0) controls the amount of diagnostic output. Returned answers are tgral (the best estimate of the integral), sd (its standard deviation), and chi2a (χ2 per degree of freedom, an indicator of whether consistent results are being obtained). See text for further details. { float ran2(long *idum);
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有