C05 Chapter Contents
C05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentC05AZF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

C05AZF locates a simple zero of a continuous function in a given interval by using Brent's method, which is a combination of nonlinear interpolation, linear extrapolation and bisection. It uses reverse communication for evaluating the function.

## 2  Specification

 SUBROUTINE C05AZF ( X, Y, FX, TOLX, IR, C, IND, IFAIL)
 INTEGER IR, IND, IFAIL REAL (KIND=nag_wp) X, Y, FX, TOLX, C(17)

## 3  Description

You must supply X and Y to define an initial interval $\left[a,b\right]$ containing a simple zero of the function $f\left(x\right)$ (the choice of X and Y must be such that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0.0$). The routine combines the methods of bisection, nonlinear interpolation and linear extrapolation (see Dahlquist and Björck (1974)), to find a sequence of sub-intervals of the initial interval such that the final interval $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ contains the zero and $\left|{\mathbf{X}}-{\mathbf{Y}}\right|$ is less than some tolerance specified by TOLX and IR (see Section 5). In fact, since the intermediate intervals $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ are determined only so that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0.0$, it is possible that the final interval may contain a discontinuity or a pole of $f$ (violating the requirement that $f$ be continuous). C05AZF checks if the sign change is likely to correspond to a pole of $f$ and gives an error return in this case.
A feature of the algorithm used by this routine is that unlike some other methods it guarantees convergence within about ${\left({\mathrm{log}}_{2}\left[\left(b-a\right)/\delta \right]\right)}^{2}$ function evaluations, where $\delta$ is related to the parameter TOLX. See Brent (1973) for more details.
C05AZF returns to the calling program for each evaluation of $f\left(x\right)$. On each return you should set ${\mathbf{FX}}=f\left({\mathbf{X}}\right)$ and call C05AZF again.
The routine is a modified version of procedure ‘zeroin’ given by Brent (1973).

## 4  References

Brent R P (1973) Algorithms for Minimization Without Derivatives Prentice–Hall
Bus J C P and Dekker T J (1975) Two efficient algorithms with guaranteed convergence for finding a zero of a function ACM Trans. Math. Software 1 330–345
Dahlquist G and Björck Å (1974) Numerical Methods Prentice–Hall

## 5  Parameters

Note: this routine uses reverse communication. Its use involves an initial entry, intermediate exits and re-entries, and a final exit, as indicated by the parameter IND. Between intermediate exits and re-entries, all parameters other than FX must remain unchanged.
1:     X – REAL (KIND=nag_wp)Input/Output
2:     Y – REAL (KIND=nag_wp)Input/Output
On initial entry: X and Y must define an initial interval $\left[a,b\right]$ containing the zero, such that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0.0$. It is not necessary that ${\mathbf{X}}<{\mathbf{Y}}$.
On intermediate exit: X contains the point at which $f$ must be evaluated before re-entry to the routine.
On final exit: X and Y define a smaller interval containing the zero, such that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0.0$, and $\left|{\mathbf{X}}-{\mathbf{Y}}\right|$ satisfies the accuracy specified by TOLX and IR, unless an error has occurred. If ${\mathbf{IFAIL}}={\mathbf{4}}$, X and Y generally contain very good approximations to a pole; if ${\mathbf{IFAIL}}={\mathbf{5}}$, X and Y generally contain very good approximations to the zero (see Section 6). If a point X is found such that $f\left({\mathbf{X}}\right)=0.0$, then on final exit ${\mathbf{X}}={\mathbf{Y}}$ (in this case there is no guarantee that X is a simple zero). In all cases, the value returned in X is the better approximation to the zero.
3:     FX – REAL (KIND=nag_wp)Input
On initial entry: if ${\mathbf{IND}}=1$, FX need not be set.
If ${\mathbf{IND}}=-1$, FX must contain $f\left({\mathbf{X}}\right)$ for the initial value of X.
On intermediate re-entry: must contain $f\left({\mathbf{X}}\right)$ for the current value of X.
4:     TOLX – REAL (KIND=nag_wp)Input
On initial entry: the accuracy to which the zero is required. The type of error test is specified by IR.
Constraint: ${\mathbf{TOLX}}>0.0$.
5:     IR – INTEGERInput
On initial entry: indicates the type of error test.
${\mathbf{IR}}=0$
The test is: $\left|{\mathbf{X}}-{\mathbf{Y}}\right|\le 2.0×{\mathbf{TOLX}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1.0,\left|{\mathbf{X}}\right|\right)$.
${\mathbf{IR}}=1$
The test is: $\left|{\mathbf{X}}-{\mathbf{Y}}\right|\le 2.0×{\mathbf{TOLX}}$.
${\mathbf{IR}}=2$
The test is: $\left|{\mathbf{X}}-{\mathbf{Y}}\right|\le 2.0×{\mathbf{TOLX}}×\left|{\mathbf{X}}\right|$.
Suggested value: ${\mathbf{IR}}=0$.
Constraint: ${\mathbf{IR}}=0$, $1$ or $2$.
6:     C($17$) – REAL (KIND=nag_wp) arrayInput/Output
On initial entry: if ${\mathbf{IND}}=1$, no elements of C need be set.
If ${\mathbf{IND}}=-1$, ${\mathbf{C}}\left(1\right)$ must contain $f\left({\mathbf{Y}}\right)$, other elements of C need not be set.
On final exit: is undefined.
7:     IND – INTEGERInput/Output
On initial entry: must be set to $1$ or $-1$.
${\mathbf{IND}}=1$
FX and ${\mathbf{C}}\left(1\right)$ need not be set.
${\mathbf{IND}}=-1$
FX and ${\mathbf{C}}\left(1\right)$ must contain $f\left({\mathbf{X}}\right)$ and $f\left({\mathbf{Y}}\right)$ respectively.
On intermediate exit: contains $2$, $3$ or $4$. The calling program must evaluate $f$ at X, storing the result in FX, and re-enter C05AZF with all other parameters unchanged.
On final exit: contains $0$.
Constraint: on entry ${\mathbf{IND}}=-1$, $1$, $2$, $3$ or $4$.
8:     IFAIL – INTEGERInput/Output
On initial entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}1$ is used it is essential to test the value of IFAIL on exit.
On final exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
On entry, $f\left({\mathbf{X}}\right)$ and $f\left({\mathbf{Y}}\right)$ have the same sign with neither equalling $0.0$.
${\mathbf{IFAIL}}=2$
On entry, ${\mathbf{IND}}\ne -1$, $1$, $2$, $3$ or $4$.
${\mathbf{IFAIL}}=3$
 On entry, ${\mathbf{TOLX}}\le 0.0$, or ${\mathbf{IR}}\ne 0$, $1$ or $2$.
${\mathbf{IFAIL}}=4$
An interval $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ has been determined satisfying the error tolerance specified by TOLX and IR and such that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0$. However, from observation of the values of $f$ during the calculation of $\left[{\mathbf{X}},{\mathbf{Y}}\right]$, it seems that the interval $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ contains a pole rather than a zero. Note that this error exit is not completely reliable: the error exit may be taken in extreme cases when $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ contains a zero, or the error exit may not be taken when $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ contains a pole. Both these cases occur most frequently when TOLX is large.
${\mathbf{IFAIL}}=5$
The tolerance TOLX is too small for the problem being solved. This indicator is only set when the interval containing the zero has been reduced to one of relative length at most $2\epsilon$, where $\epsilon$ is the machine precision, but the exit condition specified by IR is not satisfied. It is unsafe to continue reducing the interval beyond this point, but the final values of X and Y returned are accurate approximations to the zero.

## 7  Accuracy

The accuracy of the final value X as an approximation of the zero is determined by TOLX and IR (see Section 5). A relative accuracy criterion (${\mathbf{IR}}=2$) should not be used when the initial values X and Y are of different orders of magnitude. In this case a change of origin of the independent variable may be appropriate. For example, if the initial interval $\left[{\mathbf{X}},{\mathbf{Y}}\right]$ is transformed linearly to the interval $\left[1,2\right]$, then the zero can be determined to a precise number of figures using an absolute (${\mathbf{IR}}=1$) or relative (${\mathbf{IR}}=2$) error test and the effect of the transformation back to the original interval can also be determined. Except for the accuracy check, such a transformation has no effect on the calculation of the zero.

## 8  Further Comments

For most problems, the time taken on each call to C05AZF will be negligible compared with the time spent evaluating $f\left(x\right)$ between calls to C05AZF.
If the calculation terminates because $f\left({\mathbf{X}}\right)=0.0$, then on return Y is set to X. (In fact, ${\mathbf{Y}}={\mathbf{X}}$ on return only in this case and, possibly, when ${\mathbf{IFAIL}}={\mathbf{5}}$.) There is no guarantee that the value returned in X corresponds to a simple root and you should check whether it does. One way to check this is to compute the derivative of $f$ at the point X, preferably analytically, or, if this is not possible, numerically, perhaps by using a central difference estimate. If ${f}^{\prime }\left({\mathbf{X}}\right)=0.0$, then X must correspond to a multiple zero of $f$ rather than a simple zero.

## 9  Example

This example calculates a zero of ${e}^{-x}-x$ with an initial interval $\left[0,1\right]$, ${\mathbf{TOLX}}=\text{1.0E−5}$ and a mixed error test.

### 9.1  Program Text

Program Text (c05azfe.f90)

None.

### 9.3  Program Results

Program Results (c05azfe.r)