Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_roots_contfn_interval_rcomm (c05av)

## Purpose

nag_roots_contfn_interval_rcomm (c05av) 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.

## Syntax

[x, h, y, c, ind, ifail] = c05av(x, fx, h, boundl, boundu, y, c, ind)
[x, h, y, c, ind, ifail] = nag_roots_contfn_interval_rcomm(x, fx, h, boundl, boundu, y, c, ind)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: fx no longer an output parameter
.

## Description

You must supply an initial point x and a step h. nag_roots_contfn_interval_rcomm (c05av) attempts to locate a short interval [x,y] [boundl,boundu] $\left[{\mathbf{x}},{\mathbf{y}}\right]\subset \left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$ containing a simple zero of f(x) $f\left(x\right)$.
(On exit we may have x > y ${\mathbf{x}}>{\mathbf{y}}$; x is determined as the first point encountered in a binary search where the sign of f(x) $f\left(x\right)$ differs from the sign of f(x) $f\left(x\right)$ at the initial input point x.) The function attempts to locate a zero of f(x) $f\left(x\right)$ using h, 0.1 × h $0.1×{\mathbf{h}}$, 0.01 × h $0.01×{\mathbf{h}}$ and 0.001 × h $0.001×{\mathbf{h}}$ in turn as its basic step before quitting with an error exit if unsuccessful.
nag_roots_contfn_interval_rcomm (c05av) returns to the calling program for each evaluation of f(x) $f\left(x\right)$. On each return you should set fx = f(x) ${\mathbf{fx}}=f\left({\mathbf{x}}\right)$ and call nag_roots_contfn_interval_rcomm (c05av) again.

None.

## Parameters

Note: this function 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.

### Compulsory Input Parameters

1:     x – double scalar
On initial entry: the best available approximation to the zero.
Constraint: x must lie in the closed interval [boundl,boundu] $\left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$ (see below).
2:     fx – double scalar
On initial entry: if ind = 1 ${\mathbf{ind}}=1$, fx need not be set.
If ind = 1 ${\mathbf{ind}}=-1$, fx must contain f(x) $f\left({\mathbf{x}}\right)$ for the initial value of x.
On intermediate re-entry: must contain f(x) $f\left({\mathbf{x}}\right)$ for the current value of x.
3:     h – double scalar
On initial entry: a basic step size which is used in the binary search for an interval containing a zero. The basic step sizes h,0.1 × h ${\mathbf{h}},0.1×{\mathbf{h}}$, 0.01 × h $0.01×{\mathbf{h}}$ and 0.001 × h $0.001×{\mathbf{h}}$ are used in turn when searching for the zero.
Constraint: either x + h ${\mathbf{x}}+{\mathbf{h}}$ or xh ${\mathbf{x}}-{\mathbf{h}}$ must lie inside the closed interval [boundl,boundu] $\left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$.
h must be sufficiently large that x + hx ${\mathbf{x}}+{\mathbf{h}}\ne {\mathbf{x}}$ on the computer.
4:     boundl – double scalar
5:     boundu – double scalar
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 – double scalar
On initial entry: need not be set.
7:     c(11$11$) – double array
On initial entry: need not be set.
8:     ind – int64int32nag_int scalar
On initial entry: must be set to 1$1$ or 1 $-1$.
ind = 1 ${\mathbf{ind}}=1$
fx need not be set.
ind = 1 ${\mathbf{ind}}=-1$
fx must contain f(x) $f\left({\mathbf{x}}\right)$.
Constraint: on entry ind = -1${\mathbf{ind}}=-1$, 1$1$, 2$2$ or 3$3$.

None.

None.

### Output Parameters

1:     x – double scalar
On intermediate exit: contains the point at which f$f$ must be evaluated before re-entry to the function.
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:     h – double scalar
On final exit: is undefined.
3:     y – double scalar
On final exit: contains the closest point found to the final value of x, such that f(x) × f(y)0.0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$. If a value x is found such that f(x) = 0 $f\left({\mathbf{x}}\right)=0$, then y = x ${\mathbf{y}}={\mathbf{x}}$. On final exit with ${\mathbf{ifail}}={\mathbf{4}}$, x and y are the end points of the largest interval searched.
4:     c(11$11$) – double array
On final exit: if ${\mathbf{ifail}}={\mathbf{0}}$ or 4${\mathbf{4}}$, c(1) ${\mathbf{c}}\left(1\right)$ contains f(y) $f\left({\mathbf{y}}\right)$.
5:     ind – int64int32nag_int scalar
On intermediate exit: contains 2$2$ or 3$3$. The calling program must evaluate f$f$ at x, storing the result in fx, and re-enter nag_roots_contfn_interval_rcomm (c05av) with all other parameters unchanged.
On final exit: contains 0$0$.
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:
ifail = 1${\mathbf{ifail}}=1$
 On entry, ${\mathbf{boundu}}\le {\mathbf{boundl}}$, or x ∉ [boundl,boundu] ${\mathbf{x}}\notin \left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$, or both x + h ${\mathbf{x}}+{\mathbf{h}}$ and x − h ∉ [boundl,boundu] ${\mathbf{x}}-{\mathbf{h}}\notin \left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$.
ifail = 2${\mathbf{ifail}}=2$
On initial entry, h is too small to be used to perturb the initial value of x in the search.
ifail = 3${\mathbf{ifail}}=3$
The parameter ind is incorrectly set on initial or intermediate entry.
ifail = 4${\mathbf{ifail}}=4$
nag_roots_contfn_interval_rcomm (c05av) 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 [boundl,boundu] $\left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$, you should vary x and h in an attempt to find it. (See also Section [Further Comments].)

## Accuracy

nag_roots_contfn_interval_rcomm (c05av) is not intended to be used to obtain accurate approximations to the zero of f(x) $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 nag_roots_contfn_brent (c05ay) or nag_roots_contfn_brent_rcomm (c05az). 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(x) $f\left(x\right)$ near x = x $x={\mathbf{x}}$ and on the size of h.

For most problems, the time taken on each call to nag_roots_contfn_interval_rcomm (c05av) will be negligible compared with the time spent evaluating f(x) $f\left(x\right)$ between calls to nag_roots_contfn_interval_rcomm (c05av). 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 function, 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(x) $f\left(x\right)$ of interest will lie outside [boundl,boundu] $\left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$. Alternatively, f(x) $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(x) $f\left(x\right)$, that the zero lies outside [boundl,boundu] $\left[{\mathbf{boundl}},{\mathbf{boundu}}\right]$. In this case (${\mathbf{ifail}}={\mathbf{4}}$ on exit), only one of f(boundl) $f\left({\mathbf{boundl}}\right)$ and f(boundu) $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 nag_roots_contfn_interval_rcomm (c05av) again to search the remainder of the original interval.
Though nag_roots_contfn_interval_rcomm (c05av) is intended primarily for determining an interval containing a zero of f(x) $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 nag_roots_contfn_brent_rcomm (c05az) 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 c(1) ${\mathbf{c}}\left(1\right)$ on successful exit from nag_roots_contfn_interval_rcomm (c05av) is in the correct form for a call to function nag_roots_contfn_brent_rcomm (c05az) with ind = 1 ${\mathbf{ind}}=-1$.
If the calculation terminates because f(x) = 0.0 $f\left({\mathbf{x}}\right)=0.0$, then on return y is set to x. (In fact, y = x ${\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$f$ at the point x, preferably analytically, or, if this is not possible, numerically, perhaps by using a central difference estimate. If f(x) = 0.0 ${f}^{\prime }\left({\mathbf{x}}\right)=0.0$, then x must correspond to a multiple zero of f$f$ rather than a simple zero.

## Example

```function nag_roots_contfn_interval_rcomm_example
x = 3;
fx = 0;
h = 0.1;
boundl = 0;
boundu = 4;
y = 0;
c = zeros(11, 1);
ind = int64(1);
while (ind ~= int64(0))
[x, h, y, c, ind, ifail] = nag_roots_contfn_interval_rcomm(x, fx, h, boundl, boundu, y, c, ind);
fx = x^2 -3*x + 2;
end
[x,y]
[fx,c(1)]
```
```

ans =

1.7000    2.5000

ans =

-0.2100    0.7500

```
```function c05av_example
x = 3;
fx = 0;
h = 0.1;
boundl = 0;
boundu = 4;
y = 0;
c = zeros(11, 1);
ind = int64(1);
while (ind ~= int64(0))
[x, h, y, c, ind, ifail] = c05av(x, fx, h, boundl, boundu, y, c, ind);
fx = x^2 -3*x + 2;
end
[x,y]
[fx,c(1)]
```
```

ans =

1.7000    2.5000

ans =

-0.2100    0.7500

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013