C05 Chapter Contents
C05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentC05AWF

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

C05AWF attempts to locate a zero of a continuous function using a continuation method based on a secant iteration.

## 2  Specification

 SUBROUTINE C05AWF ( X, EPS, ETA, F, NFMAX, IUSER, RUSER, IFAIL)
 INTEGER NFMAX, IUSER(*), IFAIL REAL (KIND=nag_wp) X, EPS, ETA, F, RUSER(*) EXTERNAL F

## 3  Description

C05AWF attempts to obtain an approximation to a simple zero $\alpha$ of the function $f\left(x\right)$ given an initial approximation $x$ to $\alpha$. The zero is found by a call to C05AXF whose specification should be consulted for details of the method used.
The approximation $x$ to the zero $\alpha$ is determined so that at least one of the following criteria is satisfied:
 (i) $\left|x-\alpha \right|\sim {\mathbf{EPS}}$, (ii) $\left|f\left(x\right)\right|<{\mathbf{ETA}}$.

None.

## 5  Parameters

1:     X – REAL (KIND=nag_wp)Input/Output
On entry: an initial approximation to the zero.
On exit: the final approximation to the zero, unless ${\mathbf{IFAIL}}={\mathbf{1}}$, ${\mathbf{2}}$ or ${\mathbf{5}}$, in which case it contains no useful information.
2:     EPS – REAL (KIND=nag_wp)Input
On entry: an absolute tolerance to control the accuracy to which the zero is determined. In general, the smaller the value of EPS the more accurate X will be as an approximation to $\alpha$. Indeed, for very small positive values of EPS, it is likely that the final approximation will satisfy $\left|{\mathbf{X}}-\alpha \right|<{\mathbf{EPS}}$. You are advised to call the routine with more than one value for EPS to check the accuracy obtained.
Constraint: ${\mathbf{EPS}}>0.0$.
3:     ETA – REAL (KIND=nag_wp)Input
On entry: a value such that if $\left|f\left(x\right)\right|<{\mathbf{ETA}}$, $x$ is accepted as the zero. ETA may be specified as $0.0$ (see Section 7).
4:     F – REAL (KIND=nag_wp) FUNCTION, supplied by the user.External Procedure
F must evaluate the function $f$ whose zero is to be determined.
The specification of F is:
 FUNCTION F ( X, IUSER, RUSER)
 REAL (KIND=nag_wp) F
 INTEGER IUSER(*) REAL (KIND=nag_wp) X, RUSER(*)
1:     X – REAL (KIND=nag_wp)Input
On entry: the point at which the function must be evaluated.
2:     IUSER($*$) – INTEGER arrayUser Workspace
3:     RUSER($*$) – REAL (KIND=nag_wp) arrayUser Workspace
F is called with the parameters IUSER and RUSER as supplied to C05AWF. You are free to use the arrays IUSER and RUSER to supply information to F as an alternative to using COMMON global variables.
F must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which C05AWF is called. Parameters denoted as Input must not be changed by this procedure.
5:     NFMAX – INTEGERInput
On entry: the maximum permitted number of calls to F from C05AWF. If F is inexpensive to evaluate, NFMAX should be given a large value (say $\text{}>1000$).
Constraint: ${\mathbf{NFMAX}}>0$.
6:     IUSER($*$) – INTEGER arrayUser Workspace
7:     RUSER($*$) – REAL (KIND=nag_wp) arrayUser Workspace
IUSER and RUSER are not used by C05AWF, but are passed directly to F and may be used to pass information to this routine as an alternative to using COMMON global variables.
8:     IFAIL – INTEGERInput/Output
On 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, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On 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, ${\mathbf{EPS}}\le 0.0$, or ${\mathbf{NFMAX}}\le 0$.
${\mathbf{IFAIL}}=2$
An internally calculated scale factor has the wrong order of magnitude for the problem. If this error exit occurs, you are advised to call C05AXF instead where different scale values can be tried.
${\mathbf{IFAIL}}=3$
Either the function $f\left(x\right)$ given by F has no zero near X or too much accuracy has been requested in calculating the zero. The first is a more likely cause of this error exit and you should check the coding of F and make an independent investigation of its behaviour near X. The second can be alleviated by increasing EPS.
${\mathbf{IFAIL}}=4$
More than NFMAX calls have been made to F. This error exit can occur because NFMAX is too small for the problem (essentially because X is too far away from the zero) or for either of the reasons given under ${\mathbf{IFAIL}}={\mathbf{3}}$ above. If NFMAX is increased considerably and this error exit occurs again at approximately the same final value of X, then it is likely that one of the reasons given under ${\mathbf{IFAIL}}={\mathbf{3}}$ is the cause.
${\mathbf{IFAIL}}=5$ (C05AXF)
A serious error has occurred in an internal call. Check all subroutine calls. Seek expert help.

## 7  Accuracy

The levels of accuracy depend on the values of EPS and ETA. If full machine accuracy is required, they may be set very small, resulting in an exit with ${\mathbf{IFAIL}}={\mathbf{3}}$ or ${\mathbf{4}}$, although this may involve many more iterations than a lesser accuracy. You are recommended to set ${\mathbf{ETA}}=0.0$ and to use EPS to control the accuracy, unless you have considerable knowledge of the size of $f\left(x\right)$ for values of $x$ near the zero.

The time taken by C05AWF depends primarily on the time spent evaluating the function $f$ (see Section 5) and on how close the initial value of X is to the zero.
If a more flexible way of specifying the function $f$ is required or if you wish to have closer control of the calculation, then the reverse communication routine C05AXF is recommended instead of C05AWF.

## 9  Example

This example calculates the zero of $f\left(x\right)={e}^{-x}-x$ from a starting value ${\mathbf{X}}=1.0$. Two calculations are made with ${\mathbf{EPS}}=\text{1.0E−3}$ and $\text{1.0E−4}$ for comparison purposes, with ${\mathbf{ETA}}=0.0$ in both cases.

### 9.1  Program Text

Program Text (c05awfe.f90)

None.

### 9.3  Program Results

Program Results (c05awfe.r)