c9-1 (779531), страница 2
Текст из файла (страница 2)
Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited. To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).Once we know that an interval contains a root, several classical procedures areavailable to refine it.
These proceed with varying degrees of speed and surenesstowards the answer. Unfortunately, the methods that are guaranteed to converge plodalong most slowly, while those that rush to the solution in the best cases can also dashrapidly to infinity without warning if measures are not taken to avoid such behavior.The bisection method is one that cannot fail.
It is thus not to be sneered atas a method for otherwise badly behaved problems. The idea is simple. Oversome interval the function is known to pass through zero because it changes sign.Evaluate the function at the interval’s midpoint and examine its sign. Use themidpoint to replace whichever limit has the same sign. After each iteration thebounds containing the root decrease by a factor of two. If after n iterations the rootis known to be within an interval of size n , then after the next iteration it will bebracketed within an interval of size354Chapter 9.Root Finding and Nonlinear Sets of Equations#include <math.h>#define JMAX 40Maximum allowed number of bisections.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>0for (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’ MethodFor functions that are smooth near a root, the methods known respectivelyas false position (or regula falsi) and secant method generally converge faster thanbisection.
In both of these methods the function is assumed to be approximatelylinear in the local region of interest, and the next improvement in the root is taken asthe point where the approximating line crosses the axis. After each iteration one ofthe 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 recentof the prior estimates (Figure 9.2.1; this requires an arbitrary choice on the firstiteration), while false position retains that prior estimate for which the function valuehas 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 continuousfunction. Its order of convergence can be shown to be the “golden ratio” 1.618 .
. .,so thatlim |k+1 | ≈ const × |k |1.618k→∞(9.2.1)The secant method has, however, the disadvantage that the root does not necessarilyremain bracketed. For functions that are not sufficiently continuous, the algorithmcan therefore not be guaranteed to converge: Local behavior might send it offtowards infinity.Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)Copyright (C) 1988-1992 by Cambridge University Press.Programs Copyright (C) 1988-1992 by Numerical Recipes Software.Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable files (including this one) to any servercomputer, is strictly prohibited.
To order Numerical Recipes books,diskettes, or CDROMsvisit website http://www.nr.com or call 1-800-872-7423 (North America only),or send email to trade@cup.cam.ac.uk (outside North America).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;.















