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.
The routine may be called by the names c05avf or nagf_roots_contfn_interval_rcomm.
3Description
You must supply an initial point x and a step h. c05avf attempts to locate a short interval $[{\mathbf{x}},{\mathbf{y}}]\subset [{\mathbf{boundl}},{\mathbf{boundu}}]$ 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\times {\mathbf{h}}$, $0.01\times {\mathbf{h}}$ and $0.001\times {\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.
4References
None.
5Arguments
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 argument ind. Between intermediate exits and re-entries, all arguments other thanfx must remain unchanged.
1: $\mathbf{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 $[{\mathbf{boundl}},{\mathbf{boundu}}]$ (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: $\mathbf{fx}$ – Real (Kind=nag_wp)Input
On initial entry: if ${\mathbf{ind}}=1$, fx need not be set.
If ${\mathbf{ind}}=\mathrm{-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: $\mathbf{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\times {\mathbf{h}}$, $0.01\times {\mathbf{h}}$ and $0.001\times {\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 $[{\mathbf{boundl}},{\mathbf{boundu}}]$.
h must be sufficiently large that ${\mathbf{x}}+{\mathbf{h}}\ne {\mathbf{x}}$ on the computer.
On final exit: is undefined.
4: $\mathbf{boundl}$ – Real (Kind=nag_wp)Input
5: $\mathbf{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.
On final exit: contains the closest point found to the final value of x, such that $f\left({\mathbf{x}}\right)\times f\left({\mathbf{y}}\right)\le 0.0$. If a value x is found such that $f\left({\mathbf{x}}\right)=0$, ${\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: $\mathbf{c}\left(11\right)$ – 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: $\mathbf{ind}$ – IntegerInput/Output
On initial entry: must be set to $1$ or $\mathrm{-1}$.
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 arguments unchanged.
On final exit: contains $0$.
Constraint:
on entry ${\mathbf{ind}}=\mathrm{-1}$, $1$, $2$ or $3$.
Note: any values you return to c05avf as part of the reverse communication procedure should not include floating-point NaN (Not a Number) or infinity values, since these are not handled by c05avf. If your code does inadvertently return any NaNs or infinities, c05avf is likely to produce unexpected results.
9: $\mathbf{ifail}$ – IntegerInput/Output
On initial entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $\mathrm{-1}$ is recommended since useful values can be provided in some output arguments even when ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{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).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-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{boundl}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{boundu}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{boundl}}<{\mathbf{boundu}}$.
On entry, ${\mathbf{x}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{boundl}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{boundu}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{boundl}}\le {\mathbf{x}}\le {\mathbf{boundu}}$.
On entry, ${\mathbf{x}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{h}}=\u27e8\mathit{\text{value}}\u27e9$, ${\mathbf{boundl}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{boundu}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: either ${\mathbf{x}}+{\mathbf{h}}$ or ${\mathbf{x}}-{\mathbf{h}}$ must lie inside the closed interval $[{\mathbf{boundl}},{\mathbf{boundu}}]$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{x}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{h}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: h must be sufficiently large that ${\mathbf{x}}+{\mathbf{h}}\ne {\mathbf{x}}$ on the computer.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{ind}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{ind}}=\mathrm{-1}$, $1$, $2$ or $3$.
${\mathbf{ifail}}=4$
An interval containing the zero could not be found. Try restarting with modified x and h.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
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 c05ayforc05azf. 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.
8Parallelism and Performance
c05avf is not threaded in any implementation.
9Further Comments
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 $[{\mathbf{boundl}},{\mathbf{boundu}}]$. 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 $[{\mathbf{boundl}},{\mathbf{boundu}}]$. 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 arguments 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}}=\mathrm{-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.
10Example
This example finds a sub-interval of $[0.0,4.0]$ 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.