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_brent_rcomm (c05az)

## Purpose

nag_roots_contfn_brent_rcomm (c05az) locates a simple zero of a continuous function in a given interval by using Brent's method, which is a combination of nonlinear interpolation, linear extrapolation and bisection. It uses reverse communication for evaluating the function.

## Syntax

[x, y, c, ind, ifail] = c05az(x, y, fx, tolx, c, ind, 'ir', ir)
[x, y, c, ind, ifail] = nag_roots_contfn_brent_rcomm(x, y, fx, tolx, c, ind, 'ir', ir)
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 x and y to define an initial interval [a,b] $\left[a,b\right]$ containing a simple zero of the function f(x) $f\left(x\right)$ (the choice of x and y must be such that f(x) × f(y) 0.0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$). The function combines the methods of bisection, nonlinear interpolation and linear extrapolation (see Dahlquist and Björck (1974)), to find a sequence of sub-intervals of the initial interval such that the final interval [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains the zero and |xy| $|{\mathbf{x}}-{\mathbf{y}}|$ is less than some tolerance specified by tolx and ir (see Section [Parameters]). In fact, since the intermediate intervals [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ are determined only so that f(x) × f(y) 0.0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$, it is possible that the final interval may contain a discontinuity or a pole of f$f$ (violating the requirement that f$f$ be continuous). nag_roots_contfn_brent_rcomm (c05az) checks if the sign change is likely to correspond to a pole of f$f$ and gives an error return in this case.
A feature of the algorithm used by this function is that unlike some other methods it guarantees convergence within about (log2[(ba) / δ])2 ${\left({\mathrm{log}}_{2}\left[\left(b-a\right)/\delta \right]\right)}^{2}$ function evaluations, where δ$\delta$ is related to the parameter tolx. See Brent (1973) for more details.
nag_roots_contfn_brent_rcomm (c05az) 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_brent_rcomm (c05az) again.
The function is a modified version of procedure ‘zeroin’ given by Brent (1973).

## References

Brent R P (1973) Algorithms for Minimization Without Derivatives Prentice–Hall
Bus J C P and Dekker T J (1975) Two efficient algorithms with guaranteed convergence for finding a zero of a function ACM Trans. Math. Software 1 330–345
Dahlquist G and Björck Å (1974) Numerical Methods Prentice–Hall

## 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
2:     y – double scalar
On initial entry: x and y must define an initial interval [a,b] $\left[a,b\right]$ containing the zero, such that f(x) × f(y)0.0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$. It is not necessary that x < y ${\mathbf{x}}<{\mathbf{y}}$.
3:     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.
4:     tolx – double scalar
On initial entry: the accuracy to which the zero is required. The type of error test is specified by ir.
Constraint: tolx > 0.0 ${\mathbf{tolx}}>0.0$.
5:     c(17$17$) – double array
On initial entry: if ind = 1 ${\mathbf{ind}}=1$, no elements of c need be set.
If ind = 1 ${\mathbf{ind}}=-1$, c(1) ${\mathbf{c}}\left(1\right)$ must contain f(y) $f\left({\mathbf{y}}\right)$, other elements of c need not be set.
6:     ind – int64int32nag_int scalar
On initial entry: must be set to 1$1$ or 1 $-1$.
ind = 1 ${\mathbf{ind}}=1$
fx and c(1) ${\mathbf{c}}\left(1\right)$ need not be set.
ind = 1 ${\mathbf{ind}}=-1$
fx and c(1) ${\mathbf{c}}\left(1\right)$ must contain f(x) $f\left({\mathbf{x}}\right)$ and f(y) $f\left({\mathbf{y}}\right)$ respectively.
Constraint: on entry ind = -1${\mathbf{ind}}=-1$, 1$1$, 2$2$, 3$3$ or 4$4$.

### Optional Input Parameters

1:     ir – int64int32nag_int scalar
On initial entry: indicates the type of error test.
ir = 0${\mathbf{ir}}=0$
The test is: |xy|2.0 × tolx × max (1.0,|x|) $|{\mathbf{x}}-{\mathbf{y}}|\le 2.0×{\mathbf{tolx}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1.0,|{\mathbf{x}}|\right)$.
ir = 1${\mathbf{ir}}=1$
The test is: |xy|2.0 × tolx $|{\mathbf{x}}-{\mathbf{y}}|\le 2.0×{\mathbf{tolx}}$.
ir = 2${\mathbf{ir}}=2$
The test is: |xy|2.0 × tolx × |x| $|{\mathbf{x}}-{\mathbf{y}}|\le 2.0×{\mathbf{tolx}}×|{\mathbf{x}}|$.
Default: 0$0$
Constraint: ir = 0${\mathbf{ir}}=0$, 1$1$ or 2$2$.

None.

### Output Parameters

1:     x – double scalar
2:     y – double scalar
On intermediate exit: x contains the point at which f$f$ must be evaluated before re-entry to the function.
On final exit: x and y define a smaller interval containing the zero, such that f(x) × f(y)0.0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$, and |xy| $|{\mathbf{x}}-{\mathbf{y}}|$ satisfies the accuracy specified by tolx and ir, unless an error has occurred. If ${\mathbf{ifail}}={\mathbf{4}}$, x and y generally contain very good approximations to a pole; if ${\mathbf{ifail}}={\mathbf{5}}$, x and y generally contain very good approximations to the zero (see Section [Error Indicators and Warnings]). If a point x is found such that f(x) = 0.0 $f\left({\mathbf{x}}\right)=0.0$, then on final exit x = y ${\mathbf{x}}={\mathbf{y}}$ (in this case there is no guarantee that x is a simple zero). In all cases, the value returned in x is the better approximation to the zero.
3:     c(17$17$) – double array
On final exit: is undefined.
4:     ind – int64int32nag_int scalar
On intermediate exit: contains 2$2$, 3$3$ or 4$4$. The calling program must evaluate f$f$ at x, storing the result in fx, and re-enter nag_roots_contfn_brent_rcomm (c05az) with all other parameters unchanged.
On final exit: contains 0$0$.
5:     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:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

ifail = 1${\mathbf{ifail}}=1$
On entry, f(x) $f\left({\mathbf{x}}\right)$ and f(y) $f\left({\mathbf{y}}\right)$ have the same sign with neither equalling 0.0 $0.0$.
ifail = 2${\mathbf{ifail}}=2$
On entry, ind-1${\mathbf{ind}}\ne -1$, 1$1$, 2$2$, 3$3$ or 4$4$.
ifail = 3${\mathbf{ifail}}=3$
 On entry, tolx ≤ 0.0${\mathbf{tolx}}\le 0.0$, or ir ≠ 0${\mathbf{ir}}\ne 0$, 1$1$ or 2$2$.
W ifail = 4${\mathbf{ifail}}=4$
An interval [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ has been determined satisfying the error tolerance specified by tolx and ir and such that f(x) × f(y) 0 $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0$. However, from observation of the values of f$f$ during the calculation of [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$, it seems that the interval [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains a pole rather than a zero. Note that this error exit is not completely reliable: the error exit may be taken in extreme cases when [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains a zero, or the error exit may not be taken when [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains a pole. Both these cases occur most frequently when tolx is large.
W ifail = 5${\mathbf{ifail}}=5$
The tolerance tolx is too small for the problem being solved. This indicator is only set when the interval containing the zero has been reduced to one of relative length at most 2ε$2\epsilon$, where ε$\epsilon$ is the machine precision, but the exit condition specified by ir is not satisfied. It is unsafe to continue reducing the interval beyond this point, but the final values of x and y returned are accurate approximations to the zero.

## Accuracy

The accuracy of the final value x as an approximation of the zero is determined by tolx and ir (see Section [Parameters]). A relative accuracy criterion ( ir = 2 ${\mathbf{ir}}=2$) should not be used when the initial values x and y are of different orders of magnitude. In this case a change of origin of the independent variable may be appropriate. For example, if the initial interval [x,y] $\left[{\mathbf{x}},{\mathbf{y}}\right]$ is transformed linearly to the interval [1,2] $\left[1,2\right]$, then the zero can be determined to a precise number of figures using an absolute ( ir = 1 ${\mathbf{ir}}=1$) or relative ( ir = 2 ${\mathbf{ir}}=2$) error test and the effect of the transformation back to the original interval can also be determined. Except for the accuracy check, such a transformation has no effect on the calculation of the zero.

For most problems, the time taken on each call to nag_roots_contfn_brent_rcomm (c05az) will be negligible compared with the time spent evaluating f(x) $f\left(x\right)$ between calls to nag_roots_contfn_brent_rcomm (c05az).
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 and, possibly, when ${\mathbf{ifail}}={\mathbf{5}}$.) There is no guarantee that the value returned in x corresponds to a simple root 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_brent_rcomm_example
x = 0;
y = 1;
fx = 0;
tolx = 1e-05;
c = zeros(17,1);
ind = int64(1);
while (ind ~= 0)
[x, y, c, ind, ifail] = nag_roots_contfn_brent_rcomm(x, y, fx, tolx, c, ind);
fx = exp(-x) - x;
fprintf('x = %8.5f fx = %12.4e ind = %d\n', x, fx, ind);
end
if ifail == 0
fprintf('\nSolution: x = %8.5f y = %8.5f\n', x, y);
elseif ifail == 4 || ifail == 5
fprintf('\nx = %8.5f y = %8.5f\n', x, y);
end
```
```
x =  0.00000 fx =   1.0000e+00 ind = 2
x =  1.00000 fx =  -6.3212e-01 ind = 3
x =  0.61270 fx =  -7.0814e-02 ind = 4
x =  0.56707 fx =   1.1542e-04 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 4
x =  0.56713 fx =   1.4727e-05 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 0

Solution: x =  0.56714 y =  0.56713

```
```function c05az_example
x = 0;
y = 1;
fx = 0;
tolx = 1e-05;
c = zeros(17,1);
ind = int64(1);
while (ind ~= 0)
[x, y, c, ind, ifail] = c05az(x, y, fx, tolx, c, ind);
fx = exp(-x) - x;
fprintf('x = %8.5f fx = %12.4e ind = %d\n', x, fx, ind);
end
if ifail == 0
fprintf('\nSolution: x = %8.5f y = %8.5f\n', x, y);
elseif ifail == 4 || ifail == 5
fprintf('\nx = %8.5f y = %8.5f\n', x, y);
end
```
```
x =  0.00000 fx =   1.0000e+00 ind = 2
x =  1.00000 fx =  -6.3212e-01 ind = 3
x =  0.61270 fx =  -7.0814e-02 ind = 4
x =  0.56707 fx =   1.1542e-04 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 4
x =  0.56713 fx =   1.4727e-05 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 4
x =  0.56714 fx =  -9.4481e-07 ind = 0

Solution: x =  0.56714 y =  0.56713

```

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