NAG Library Routine Document
C05AXF
1 Purpose
C05AXF attempts to locate a zero of a continuous function using a continuation method based on a secant iteration. It uses reverse communication for evaluating the function.
2 Specification
INTEGER 
IR, IND, IFAIL 
REAL (KIND=nag_wp) 
X, FX, TOL, SCAL, C(26) 

3 Description
C05AXF uses a modified version of an algorithm given in
Swift and Lindfield (1978) to compute a zero
$\alpha $ of a continuous function
$f\left(x\right)$. The algorithm used is based on a continuation method in which a sequence of problems
are solved, where
$1={\theta}_{0}>{\theta}_{1}>\cdots >{\theta}_{m}=0$ (the value of
$m$ is determined as the algorithm proceeds) and where
${x}_{0}$ is your initial estimate for the zero of
$f\left(x\right)$. For each
${\theta}_{r}$ the current problem is solved by a robust secant iteration using the solution from earlier problems to compute an initial estimate.
You must supply an error tolerance
TOL.
TOL is used directly to control the accuracy of solution of the final problem (
${\theta}_{m}=0$)
in the continuation method, and
$\sqrt{{\mathbf{TOL}}}$ is used to control the accuracy in the intermediate problems
(
${\theta}_{1},{\theta}_{2},\dots ,{\theta}_{m1}$).
4 References
Swift A and Lindfield G R (1978) Comparison of a continuation method for the numerical solution of a single nonlinear equation Comput. J. 21 359–362
5 Parameters
Note: this routine uses
reverse communication. Its use involves an initial entry, intermediate exits and reentries, and a final exit, as indicated by the parameter
IND. Between intermediate exits and reentries,
all parameters other than FX must remain unchanged.
 1: X – REAL (KIND=nag_wp)Input/Output
On initial entry: an initial approximation to the zero.
On intermediate exit:
the point at which $f$ must be evaluated before reentry to the routine.
On final exit: the final approximation to the zero.
 2: 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 reentry: must contain
$f\left({\mathbf{X}}\right)$ for the current value of
X.
 3: TOL – REAL (KIND=nag_wp)Input
On initial entry: a value that controls the accuracy to which the zero is determined.
TOL is used in determining the convergence of the secant iteration used at each stage of the continuation process. It is used directly when solving the last problem (
${\theta}_{m}=0$ in
Section 3), and
$\sqrt{{\mathbf{TOL}}}$ is used for the problem defined by
${\theta}_{r}$,
$r<m$. Convergence to the accuracy specified by
TOL is not guaranteed, and so you are recommended to find the zero using at least two values for
TOL to check the accuracy obtained.
Constraint:
${\mathbf{TOL}}>0.0$.
 4: IR – INTEGERInput
On initial entry: indicates the type of error test required, as follows. Solving the problem defined by
${\theta}_{r}$,
$1\le r\le m$, involves computing a sequence of secant iterates
${x}_{r}^{0},{x}_{r}^{1},\dots \text{}$. This sequence will be considered to have converged only if:
for
${\mathbf{IR}}=0$,
for
${\mathbf{IR}}=1$,
for
${\mathbf{IR}}=2$,
for some
$i>1$; here
$\mathit{eps}$ is either
TOL or
$\sqrt{{\mathbf{TOL}}}$ as discussed above. Note that there are other subsidiary conditions (not given here) which must also be satisfied before the secant iteration is considered to have converged.
Constraint:
${\mathbf{IR}}=0$, $1$ or $2$.
 5: SCAL – REAL (KIND=nag_wp)Input
On initial entry: a factor for use in determining a significant approximation to the derivative of
$f\left(x\right)$ at
$x={x}_{0}$, the initial value. A number of difference approximations to
${f}^{\prime}\left({x}_{0}\right)$ are calculated using
where
$\lefth\right<\left{\mathbf{SCAL}}\right$ and
$h$ has the same sign as
SCAL. A significance (cancellation) check is made on each difference approximation and the approximation is rejected if insignificant.
Suggested value:
$\sqrt{\epsilon}$, where
$\epsilon $ is the
machine precision returned by
X02AJF.
Constraint:
${\mathbf{SCAL}}$ must be sufficiently large that ${\mathbf{X}}+{\mathbf{SCAL}}\ne {\mathbf{X}}$ on the computer.
 6: C($26$) – REAL (KIND=nag_wp) arrayCommunication Array
(${\mathbf{C}}\left(5\right)$ contains the current ${\theta}_{r}$, this value may be useful in the event of an error exit.)
 7: IND – INTEGERInput/Output
On initial entry: must be set to
$1$ or
$1$.
 ${\mathbf{IND}}=1$
 FX need not be set.
 ${\mathbf{IND}}=1$
 FX must contain $f\left({\mathbf{X}}\right)$.
On intermediate exit:
contains
$2$,
$3$ or
$4$. The calling program must evaluate
$f$ at
X, storing the result in
FX, and reenter C05AXF 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,  ${\mathbf{TOL}}\le 0.0$, 
or  ${\mathbf{IR}}\ne 0$, $1$ or $2$. 
 ${\mathbf{IFAIL}}=2$
The parameter
IND is incorrectly set on initial or intermediate entry.
 ${\mathbf{IFAIL}}=3$
SCAL is too small, or significant derivatives of
$f$ cannot be computed (this can happen when
$f$ is almost constant and nonzero, for any value of
SCAL).
 ${\mathbf{IFAIL}}=4$
The current problem in the continuation sequence cannot be solved, see
${\mathbf{C}}\left(5\right)$ for the value of
${\theta}_{r}$. The most likely explanation is that the current problem has no solution, either because the original problem had no solution or because the continuation path passes through a set of insoluble problems. This latter reason for failure should occur rarely, and not at all if the initial approximation to the zero is sufficiently close. Other possible explanations are that
TOL is too small and hence the accuracy requirement is too stringent, or that
TOL is too large and the initial approximation too poor, leading to successively worse intermediate solutions.
 ${\mathbf{IFAIL}}=5$
Continuation away from the initial point is not possible. This error exit will usually occur if the problem has not been properly posed or the error requirement is extremely stringent.
 ${\mathbf{IFAIL}}=6$
The final problem (with ${\theta}_{m}=0$) cannot be solved. It is likely that too much accuracy has been requested, or that the zero is at $\alpha =0$ and ${\mathbf{IR}}=2$.
7 Accuracy
The accuracy of the approximation to the zero depends on
TOL and
IR. In general decreasing
TOL will give more accurate results. Care must be exercised when using the relative error criterion (
${\mathbf{IR}}=2$).
If the zero is at
${\mathbf{X}}=0$, or if the initial value of
X and the zero bracket the point
${\mathbf{X}}=0$, it is likely that an error exit with
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$ or
${\mathbf{6}}$ will occur.
It is possible to request too much or too little accuracy. Since it is not possible to achieve more than machine accuracy, a value of
${\mathbf{TOL}}\ll \mathit{machineprecision}$ should not be input and may lead to an error exit with
${\mathbf{IFAIL}}={\mathbf{4}}$,
${\mathbf{5}}$ or
${\mathbf{6}}$. For the reasons discussed under
${\mathbf{IFAIL}}={\mathbf{4}}$ in
Section 6,
TOL should not be taken too large, say no larger than
${\mathbf{TOL}}=\text{1.0E\u22123}$.
For most problems, the time taken on each call to C05AXF will be negligible compared with the time spent evaluating
$f\left(x\right)$ between calls to C05AXF. However, the initial value of
X and the choice of
TOL will clearly affect the timing. The closer that
X is to the root, the less evaluations of
$f$ required. The effect of the choice of
TOL will not be large, in general, unless
TOL is very small, in which case the timing will increase.
9 Example
This example calculates a zero of $x{e}^{x}$ with initial approximation ${x}_{0}=1.0$, and ${\mathbf{TOL}}=\text{1.0E\u22123}$ and $\text{1.0E\u22124}$.
9.1 Program Text
Program Text (c05axfe.f90)
9.2 Program Data
None.
9.3 Program Results
Program Results (c05axfe.r)