C05 Chapter Contents
C05 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentC05AVF

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

C05AVF attempts to locate an interval containing a simple zero of a continuous function using a binary search. It uses reverse communication for evaluating the function.

## 2  Specification

 SUBROUTINE C05AVF ( X, FX, H, BOUNDL, BOUNDU, Y, C, IND, IFAIL)
 INTEGER IND, IFAIL REAL (KIND=nag_wp) X, FX, H, BOUNDL, BOUNDU, Y, C(11)

## 3  Description

You must supply an initial point X and a step H. C05AVF attempts to locate a short interval $\left[{\mathbf{X}},{\mathbf{Y}}\right]\subset \left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$ containing a simple zero of $f\left(x\right)$.
(On exit we may have ${\mathbf{X}}>{\mathbf{Y}}$; X is determined as the first point encountered in a binary search where the sign of $f\left(x\right)$ differs from the sign of $f\left(x\right)$ at the initial input point X.) The routine attempts to locate a zero of $f\left(x\right)$ using H, $0.1×{\mathbf{H}}$, $0.01×{\mathbf{H}}$ and $0.001×{\mathbf{H}}$ in turn as its basic step before quitting with an error exit if unsuccessful.
C05AVF 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 C05AVF again.

None.

## 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
On initial entry: the best available approximation to the zero.
Constraint: X must lie in the closed interval $\left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$ (see below).
On intermediate exit: contains the point at which $f$ must be evaluated before re-entry to the routine.
On final exit: contains one end of an interval containing the zero, the other end being in Y, unless an error has occurred. If ${\mathbf{IFAIL}}={\mathbf{4}}$, X and Y are the end points of the largest interval searched. If a zero is located exactly, its value is returned in X (and in Y).
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 re-entry: must contain $f\left({\mathbf{X}}\right)$ for the current value of X.
3:     H – REAL (KIND=nag_wp)Input/Output
On initial entry: a basic step size which is used in the binary search for an interval containing a zero. The basic step sizes ${\mathbf{H}},0.1×{\mathbf{H}}$, $0.01×{\mathbf{H}}$ and $0.001×{\mathbf{H}}$ are used in turn when searching for the zero.
Constraint: either ${\mathbf{X}}+{\mathbf{H}}$ or ${\mathbf{X}}-{\mathbf{H}}$ must lie inside the closed interval $\left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$.
H must be sufficiently large that ${\mathbf{X}}+{\mathbf{H}}\ne {\mathbf{X}}$ on the computer.
On final exit: is undefined.
4:     BOUNDL – REAL (KIND=nag_wp)Input
5:     BOUNDU – REAL (KIND=nag_wp)Input
On initial entry: BOUNDL and BOUNDU must contain respectively lower and upper bounds for the interval of search for the zero.
Constraint: ${\mathbf{BOUNDL}}<{\mathbf{BOUNDU}}$.
6:     Y – REAL (KIND=nag_wp)Input/Output
On initial entry: need not be set.
On final exit: contains the closest point found to the final value of X, such that $f\left({\mathbf{X}}\right)×f\left({\mathbf{Y}}\right)\le 0.0$. If a value X is found such that $f\left({\mathbf{X}}\right)=0$, then ${\mathbf{Y}}={\mathbf{X}}$. On final exit with ${\mathbf{IFAIL}}={\mathbf{4}}$, X and Y are the end points of the largest interval searched.
7:     C($11$) – REAL (KIND=nag_wp) arrayCommunication Array
On initial entry: need not be set.
On final exit: if ${\mathbf{IFAIL}}={\mathbf{0}}$ or ${\mathbf{4}}$, ${\mathbf{C}}\left(1\right)$ contains $f\left({\mathbf{Y}}\right)$.
8:     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$ or $3$. The calling program must evaluate $f$ at X, storing the result in FX, and re-enter C05AVF with all other parameters unchanged.
On final exit: contains $0$.
Constraint: on entry ${\mathbf{IND}}=-1$, $1$, $2$ or $3$.
9:     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{BOUNDU}}\le {\mathbf{BOUNDL}}$, or ${\mathbf{X}}\notin \left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$, or both ${\mathbf{X}}+{\mathbf{H}}$ and ${\mathbf{X}}-{\mathbf{H}}\notin \left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$.
${\mathbf{IFAIL}}=2$
On initial entry, H is too small to be used to perturb the initial value of X in the search.
${\mathbf{IFAIL}}=3$
The parameter IND is incorrectly set on initial or intermediate entry.
${\mathbf{IFAIL}}=4$
C05AVF has been unable to determine an interval containing a simple zero starting from the initial value of X and using the step H. If you have prior knowledge that a simple zero lies in the interval $\left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$, you should vary X and H in an attempt to find it. (See also Section 8.)

## 7  Accuracy

C05AVF is not intended to be used to obtain accurate approximations to the zero of $f\left(x\right)$ but rather to locate an interval containing a zero. This interval can then be used as input to an accurate rootfinder such as C05AYF or C05AZF. The size of the interval determined depends somewhat unpredictably on the choice of X and H. The closer X is to the root and the smaller the initial value of H, then, in general, the smaller (more accurate) the interval determined; however, the accuracy of this statement depends to some extent on the behaviour of $f\left(x\right)$ near $x={\mathbf{X}}$ and on the size of H.

For most problems, the time taken on each call to C05AVF will be negligible compared with the time spent evaluating $f\left(x\right)$ between calls to C05AVF. However, the initial value of X and H will clearly affect the timing. The closer X is to the root, and the larger the initial value of H then the less time taken. (However taking a large H can affect the accuracy and reliability of the routine, see below.)
You are expected to choose BOUNDL and BOUNDU as physically (or mathematically) realistic limits on the interval of search. For example, it may be known, from physical arguments, that no zero of $f\left(x\right)$ of interest will lie outside $\left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$. Alternatively, $f\left(x\right)$ may be more expensive to evaluate for some values of X than for others and such expensive evaluations can sometimes be avoided by careful choice of BOUNDL and BOUNDU.
The choice of BOUNDL and BOUNDU affects the search only in that these values provide physical limitations on the search values and that the search is terminated if it seems, from the available information about $f\left(x\right)$, that the zero lies outside $\left[{\mathbf{BOUNDL}},{\mathbf{BOUNDU}}\right]$. In this case (${\mathbf{IFAIL}}={\mathbf{4}}$ on exit), only one of $f\left({\mathbf{BOUNDL}}\right)$ and $f\left({\mathbf{BOUNDU}}\right)$ may have been evaluated and a zero close to the other end of the interval could be missed. The actual interval searched is returned in the parameters X and Y and you can call C05AVF again to search the remainder of the original interval.
Though C05AVF is intended primarily for determining an interval containing a zero of $f\left(x\right)$, it may be used to shorten a known interval. This could be useful if, for example, a large interval containing the zero is known and it is also known that the root lies close to one end of the interval; by setting X to this end of the interval and H small, a short interval will usually be determined. However, it is worth noting that once any interval containing a zero has been determined, a call to C05AZF will usually be the most efficient way to calculate an interval of specified length containing the zero. To assist in this determination, the information in FX and in X, Y and ${\mathbf{C}}\left(1\right)$ on successful exit from C05AVF is in the correct form for a call to routine C05AZF with ${\mathbf{IND}}=-1$.
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.) In this case, there is no guarantee that the value in X corresponds to a simple zero 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 finds a sub-interval of $\left[0.0,4.0\right]$ containing a simple zero of ${x}^{2}-3x+2$. The zero nearest to $3.0$ is required and so we set ${\mathbf{X}}=3.0$ initially.

### 9.1  Program Text

Program Text (c05avfe.f90)

None.

### 9.3  Program Results

Program Results (c05avfe.r)