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 = ) nrerror("Root must be bracketed for bisection in rtbis"); rtb = f 0 for (j=1;j | 354 Chapter 9. Root Finding and Nonlinear Sets ofEquations include 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 ixacc. void nrerror char error_text int j float dx f fmid xmid rtb f func x1 fmid func x2 if f fmid nrerror Root must be bracketed for bisection in rtbis rtb f 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 Bisection loop. if fmid rtb xmid if fabs dx xacc fmid return rtb nrerror Too many bisections in rtbis return Never get here. 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 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 . 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 . so that lim efc 1 const x rk I1 618 k 1 Sample page from NUMERICAL RECIPES IN C THE ART OF SCIENTIFIC COMPUTING .