正在加载图片...
354 Chapter 9. Root Finding and Nonlinear Sets of Equations #include <math.h> #define JMAX 40 Maximum allowed number of bisections. float rtbis(float (*func)(float),float xi,float x2,float xacc) Using bisection,find the root of a function func known to lie between x1 and x2.The root, returned as rtbis,will be refined until its accuracy is txacc. void nrerror(char error-text☐); int ji float dx,f,fmid,xmid,rtb; f=(*func)(x1): 三 fmid=(*func)(x2); if (f*fmid >=0.0)nrerror("Root must be bracketed for bisection in rtbis"); rtb=f<0.0?(dx=x2-x1,x1):(dx=x1-x2,x2); Orient the search so that f>0 for (j=1;j<=JMAX;++){ lies at x+dx. fmid=(*func)(xmid=rtb+(dx *0.5)); Bisection loop. if (fmid <=0.0)rtb-xmid; if (fabs(dx)<xacc II fmid ==0.0)return rtb; ICAL nrerror("Too many bisections in rtbis"); return 0.0; Never get here. RECIPES I America computer, Press. 9 9.2 Secant Method,False Position Method, Programs and Ridders'Method IENTIFIC For functions that are smooth near a root,the methods known respectively to dir as false position (or regula falsi)and secant method generally converge faster than bisection.In both of these methods the function is assumed to be approximately linear in the local region of interest,and the next improvement in the root is taken as the point where the approximating line crosses the axis.After each iteration one of the previous boundary points is discarded in favor of the latest estimate of the root. Recipes 10621 The only difference between the methods is that secant retains the most recent Numerica of the prior estimates(Figure 9.2.1;this requires an arbitrary choice on the first 43106 iteration),while false position retains that prior estimate for which the function value Recipes has opposite sign from the function value at the current best estimate of the root, (outside 腿 so that the two points continue to bracket the root(Figure 9.2.2).Mathematically, the secant method converges more rapidly near a root of a sufficiently continuous North function.Its order of convergence can be shown to be the"golden ratio"1.618..., so that lim+const x (9.2.1) The secant method has,however,the disadvantage that the root does not necessarily remain bracketed.For functions that are not sufficiently continuous.the algorithm can therefore not be guaranteed to converge:Local behavior might send it off towards infinity354 Chapter 9. Root Finding and Nonlinear Sets of Equations 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). #include <math.h> #define JMAX 40 Maximum allowed number of bisections. float rtbis(float (*func)(float), float x1, float x2, float xacc) Using bisection, find the root of a function func known to lie between x1 and x2. The root, returned as rtbis, will be refined until its accuracy is ±xacc. { void nrerror(char error_text[]); int j; float dx,f,fmid,xmid,rtb; f=(*func)(x1); fmid=(*func)(x2); if (f*fmid >= 0.0) nrerror("Root must be bracketed for bisection in rtbis"); rtb = f < 0.0 ? (dx=x2-x1,x1) : (dx=x1-x2,x2); Orient the search so that f>0 for (j=1;j<=JMAX;j++) { lies at x+dx. fmid=(*func)(xmid=rtb+(dx *= 0.5)); Bisection loop. if (fmid <= 0.0) rtb=xmid; if (fabs(dx) < xacc || fmid == 0.0) return rtb; } nrerror("Too many bisections in rtbis"); return 0.0; Never get here. } 9.2 Secant Method, False Position Method, and Ridders’ Method For functions that are smooth near a root, the methods known respectively as false position (or regula falsi) and secant method generally converge faster than bisection. In both of these methods the function is assumed to be approximately linear in the local region of interest, and the next improvement in the root is taken as the point where the approximating line crosses the axis. After each iteration one of the previous boundary points is discarded in favor of the latest estimate of the root. The only difference between the methods is that secant retains the most recent of the prior estimates (Figure 9.2.1; this requires an arbitrary choice on the first iteration), while false position retains that prior estimate for which the function value has opposite sign from the function value at the current best estimate of the root, so that the two points continue to bracket the root (Figure 9.2.2). Mathematically, the secant method converges more rapidly near a root of a sufficiently continuous function. Its order of convergence can be shown to be the “golden ratio” 1.618 ... , so that lim k→∞ |k+1| ≈ const × |k| 1.618 (9.2.1) The secant method has, however, the disadvantage that the root does not necessarily remain bracketed. For functions that are not sufficiently continuous, the algorithm can therefore not be guaranteed to converge: Local behavior might send it off towards infinity.
向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有