c9-2 (779532)
Текст из файла
354Chapter 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;3x41Figure 9.2.1. Secant method. Extrapolation or interpolation lines (dashed) are drawn through the twomost recently evaluated points, whether or not they bracket the function.
The points are numbered inthe order that they are used.34x1Figure 9.2.2. False position method. Interpolation lines (dashed) are drawn through the most recentpoints that bracket the root. In this example, point 1 thus remains “active” for many steps. False positionconverges less rapidly than the secant method, but it is more certain.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).2f(x)3559.2 Secant Method, False Position Method, and Ridders’ Method2f(x)356Chapter 9.Root Finding and Nonlinear Sets of Equationsf (x)21 3 4Figure 9.2.3.
Example where both the secant and false position methods will take many iterations toarrive at the true root. This function would be difficult for many other root-finding methods.False position, since it sometimes keeps an older rather than newer functionevaluation, has a lower order of convergence. Since the newer function value willsometimes be kept, the method is often superlinear, but estimation of its exact orderis not so easy.Here are sample implementations of these two related methods. While thesemethods are standard textbook fare, Ridders’ method, described below, or Brent’smethod, in the next section, are almost always better choices. Figure 9.2.3 shows thebehavior of secant and false-position methods in a difficult situation.#include <math.h>#define MAXIT 30Set to the maximum allowed number of iterations.float rtflsp(float (*func)(float), float x1, float x2, float xacc)Using the false position method, find the root of a function func known to lie between x1 andx2.
The root, returned as rtflsp, is refined until its accuracy is ±xacc.{void nrerror(char error_text[]);int j;float fl,fh,xl,xh,swap,dx,del,f,rtf;fl=(*func)(x1);fh=(*func)(x2);Be sure the interval brackets a root.if (fl*fh > 0.0) nrerror("Root must be bracketed in rtflsp");if (fl < 0.0) {Identify the limits so that xl corresponds to the lowxl=x1;side.xh=x2;} else {xl=x2;xh=x1;swap=fl;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).x9.2 Secant Method, False Position Method, and Ridders’ Method357fl=fh;fh=swap;}#include <math.h>#define MAXIT 30Maximum allowed number of iterations.float rtsec(float (*func)(float), float x1, float x2, float xacc)Using the secant method, find the root of a function func thought to lie between x1 and x2.The root, returned as rtsec, is refined until its accuracy is ±xacc.{void nrerror(char error_text[]);int j;float fl,f,dx,swap,xl,rts;fl=(*func)(x1);f=(*func)(x2);if (fabs(fl) < fabs(f)) {Pick the bound with the smaller function value asrts=x1;the most recent guess.xl=x2;swap=fl;fl=f;f=swap;} else {xl=x1;rts=x2;}for (j=1;j<=MAXIT;j++) {Secant loop.dx=(xl-rts)*f/(f-fl);Increment with respect to latest value.xl=rts;fl=f;rts += dx;f=(*func)(rts);if (fabs(dx) < xacc || f == 0.0) return rts;Convergence.}nrerror("Maximum number of iterations exceeded in rtsec");return 0.0;Never get here.}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).}dx=xh-xl;for (j=1;j<=MAXIT;j++) {False position loop.rtf=xl+dx*fl/(fl-fh);Increment with respect to latest value.f=(*func)(rtf);if (f < 0.0) {Replace appropriate limit.del=xl-rtf;xl=rtf;fl=f;} else {del=xh-rtf;xh=rtf;fh=f;}dx=xh-xl;if (fabs(del) < xacc || f == 0.0) return rtf;Convergence.}nrerror("Maximum number of iterations exceeded in rtflsp");return 0.0;Never get here.358Chapter 9.Root Finding and Nonlinear Sets of EquationsRidders’ Methodf(x1 ) − 2f(x3 )eQ + f(x2 )e2Q = 0(9.2.2)This is a quadratic equation in eQ , which can be solved to givepf(x3 ) + sign[f(x2 )] f(x3 )2 − f(x1 )f(x2 )e =f(x2 )Q(9.2.3)Now the false position method is applied, not to the values f(x1 ), f(x3 ), f(x2 ), butto the values f(x1 ), f(x3 )eQ , f(x2 )e2Q , yielding a new guess for the root, x4 .
Theoverall updating formula (incorporating the solution 9.2.3) issign[f(x1 ) − f(x2 )]f(x3 )x4 = x3 + (x3 − x1 ) pf(x3 )2 − f(x1 )f(x2 )(9.2.4)Equation (9.2.4) has some very nice properties. First, x4 is guaranteed to liein the interval (x1 , x2 ), so the method never jumps out of its brackets. Second,the convergence of successive applications of equation (9.2.4) is quadratic, that is,m = 2 in equation (9.1.4).
Since each application√ of (9.2.4) requires two functionevaluations, the actual order of the method is 2, not 2; but this is still quiterespectably superlinear: the number of significant digits in the answer approximatelydoubles with each two function evaluations. Third, taking out the function’s “bend”via exponential (that is, ratio) factors, rather than via a polynomial technique (e.g.,fitting a parabola), turns out to give an extraordinarily robust algorithm. In bothreliability and speed, Ridders’ method is generally competitive with the more highlydeveloped and better established (but more complicated) method of Van Wijngaarden,Dekker, and Brent, which we next discuss.#include <math.h>#include "nrutil.h"#define MAXIT 60#define UNUSED (-1.11e30)float zriddr(float (*func)(float), float x1, float x2, float xacc)Using Ridders’ method, return the root of a function func known to lie between x1 and x2.The root, returned as zriddr, will be refined to an approximate accuracy xacc.{int j;float ans,fh,fl,fm,fnew,s,xh,xl,xm,xnew;fl=(*func)(x1);fh=(*func)(x2);if ((fl > 0.0 && fh < 0.0) || (fl < 0.0 && fh > 0.0)) {xl=x1;xh=x2;ans=UNUSED;Any highly unlikely value, to simplify logicbelow.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.
Характеристики
Тип файла PDF
PDF-формат наиболее широко используется для просмотра любого типа файлов на любом устройстве. В него можно сохранить документ, таблицы, презентацию, текст, чертежи, вычисления, графики и всё остальное, что можно показать на экране любого устройства. Именно его лучше всего использовать для печати.
Например, если Вам нужно распечатать чертёж из автокада, Вы сохраните чертёж на флешку, но будет ли автокад в пункте печати? А если будет, то нужная версия с нужными библиотеками? Именно для этого и нужен формат PDF - в нём точно будет показано верно вне зависимости от того, в какой программе создали PDF-файл и есть ли нужная программа для его просмотра.















