17.11.2012 Views

Numerical recipes

Numerical recipes

Numerical recipes

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

356 Chapter 9. Root Finding and Nonlinear Sets of Equations<br />

1 3 4<br />

f(x)<br />

Figure 9.2.3. Example where both the secant and false position methods will take many iterations to<br />

arrive at the true root. This function would be difficult for many other root-finding methods.<br />

False position, since it sometimes keeps an older rather than newer function<br />

evaluation, has a lower order of convergence. Since the newer function value will<br />

sometimes be kept, the method is often superlinear, but estimation of its exact order<br />

is not so easy.<br />

Here are sample implementations of these two related methods. While these<br />

methods are standard textbook fare, Ridders’ method, described below, or Brent’s<br />

method, in the next section, are almost always better choices. Figure 9.2.3 shows the<br />

behavior of secant and false-position methods in a difficult situation.<br />

#include <br />

#define MAXIT 30 Set to the maximum allowed number of iterations.<br />

float rtflsp(float (*func)(float), float x1, float x2, float xacc)<br />

Using the false position method, find the root of a function func known to lie between x1 and<br />

x2. The root, returned as rtflsp, is refined until its accuracy is ±xacc.<br />

{<br />

void nrerror(char error_text[]);<br />

int j;<br />

float fl,fh,xl,xh,swap,dx,del,f,rtf;<br />

fl=(*func)(x1);<br />

fh=(*func)(x2); Be sure the interval brackets a root.<br />

if (fl*fh > 0.0) nrerror("Root must be bracketed in rtflsp");<br />

if (fl < 0.0) { Identify the limits so that xl corresponds to the low<br />

xl=x1;<br />

side.<br />

xh=x2;<br />

} else {<br />

xl=x2;<br />

xh=x1;<br />

swap=fl;<br />

2<br />

x<br />

Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)<br />

Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by <strong>Numerical</strong> Recipes Software.<br />

Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable<br />

files (including this one) to any server computer, is strictly prohibited. To order <strong>Numerical</strong> Recipes books or CDROMs, visit website<br />

http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!