﻿ c05az Method
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

C#
```public static void c05az(
ref double x,
ref double y,
double fx,
double tolx,
int ir,
double[] c,
ref int ind,
out int ifail
)```
Visual Basic
```Public Shared Sub c05az ( _
ByRef x As Double, _
ByRef y As Double, _
fx As Double, _
tolx As Double, _
ir As Integer, _
c As Double(), _
ByRef ind As Integer, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void c05az(
double% x,
double% y,
double fx,
double tolx,
int ir,
array<double>^ c,
int% ind,
[OutAttribute] int% ifail
)```
F#
```static member c05az :
x : float byref *
y : float byref *
fx : float *
tolx : float *
ir : int *
c : float[] *
ind : int byref *
ifail : int byref -> unit
```

Parameters

x
Type: System..::..Double%
On initial entry: x and y must define an initial interval $\left[a,b\right]$ containing the zero, such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$. It is not necessary that ${\mathbf{x}}<{\mathbf{y}}$.
On intermediate exit: x contains the point at which $f$ must be evaluated before re-entry to the method.
On final exit: x and y define a smaller interval containing the zero, such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$, and $\left|{\mathbf{x}}-{\mathbf{y}}\right|$ satisfies the accuracy specified by tolx and ir, unless an error has occurred. If ${\mathbf{ifail}}={4}$, x and y generally contain very good approximations to a pole; if ${\mathbf{ifail}}={5}$, x and y generally contain very good approximations to the zero (see [Error Indicators and Warnings]). If a point x is found such that $f\left({\mathbf{x}}\right)=0.0$, then on final exit ${\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.
y
Type: System..::..Double%
On initial entry: x and y must define an initial interval $\left[a,b\right]$ containing the zero, such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$. It is not necessary that ${\mathbf{x}}<{\mathbf{y}}$.
On intermediate exit: x contains the point at which $f$ must be evaluated before re-entry to the method.
On final exit: x and y define a smaller interval containing the zero, such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$, and $\left|{\mathbf{x}}-{\mathbf{y}}\right|$ satisfies the accuracy specified by tolx and ir, unless an error has occurred. If ${\mathbf{ifail}}={4}$, x and y generally contain very good approximations to a pole; if ${\mathbf{ifail}}={5}$, x and y generally contain very good approximations to the zero (see [Error Indicators and Warnings]). If a point x is found such that $f\left({\mathbf{x}}\right)=0.0$, then on final exit ${\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.
fx
Type: System..::..Double
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.
tolx
Type: System..::..Double
On initial entry: the accuracy to which the zero is required. The type of error test is specified by ir.
Constraint: ${\mathbf{tolx}}>0.0$.
ir
Type: System..::..Int32
On initial entry: indicates the type of error test.
${\mathbf{ir}}=0$
The test is: $\left|{\mathbf{x}}-{\mathbf{y}}\right|\le 2.0×{\mathbf{tolx}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1.0,\left|{\mathbf{x}}\right|\right)$.
${\mathbf{ir}}=1$
The test is: $\left|{\mathbf{x}}-{\mathbf{y}}\right|\le 2.0×{\mathbf{tolx}}$.
${\mathbf{ir}}=2$
The test is: $\left|{\mathbf{x}}-{\mathbf{y}}\right|\le 2.0×{\mathbf{tolx}}×\left|{\mathbf{x}}\right|$.
Suggested value: ${\mathbf{ir}}=0$.
Constraint: ${\mathbf{ir}}=0$, $1$ or $2$.
c
Type: array<System..::..Double>[]()[][]
An array of size [$17$]
On initial entry: if ${\mathbf{ind}}=1$, no elements of c need be set.
If ${\mathbf{ind}}=-1$, ${\mathbf{c}}\left[0\right]$ must contain $f\left({\mathbf{y}}\right)$, other elements of c need not be set.
On final exit: is undefined.
ind
Type: System..::..Int32%
On initial entry: must be set to $1$ or $-1$.
${\mathbf{ind}}=1$
fx and ${\mathbf{c}}\left[0\right]$ need not be set.
${\mathbf{ind}}=-1$
fx and ${\mathbf{c}}\left[0\right]$ must contain $f\left({\mathbf{x}}\right)$ and $f\left({\mathbf{y}}\right)$ respectively.
On intermediate exit: contains $2$, $3$ or $4$. The calling program must evaluate $f$ at x, storing the result in fx, and re-enter c05az with all other parameters unchanged.
On final exit: contains $0$.
Constraint: on entry ${\mathbf{ind}}=-1$, $1$, $2$, $3$ or $4$.
ifail
Type: System..::..Int32%
On initial entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Library Overview 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 method the values of the output parameters may be useful even if ${\mathbf{ifail}}\ne {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}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

Description

You must supply x and y to define an initial interval $\left[a,b\right]$ containing a simple zero of the function $f\left(x\right)$ (the choice of x and y must be such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0.0$). The method 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 $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains the zero and $\left|{\mathbf{x}}-{\mathbf{y}}\right|$ is less than some tolerance specified by tolx and ir (see [Parameters]). In fact, since the intermediate intervals $\left[{\mathbf{x}},{\mathbf{y}}\right]$ are determined only so that $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$ (violating the requirement that $f$ be continuous). c05az checks if the sign change is likely to correspond to a pole of $f$ and gives an error return in this case.
A feature of the algorithm used by this method is that unlike some other methods it guarantees convergence within about ${\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.
c05az 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 c05az again.
The method 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

Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
On entry, $f\left({\mathbf{x}}\right)$ and $f\left({\mathbf{y}}\right)$ have the same sign with neither equalling $0.0$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{ind}}\ne -1$, $1$, $2$, $3$ or $4$.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{tolx}}\le 0.0$, or ${\mathbf{ir}}\ne 0$, $1$ or $2$.
${\mathbf{ifail}}=4$
An interval $\left[{\mathbf{x}},{\mathbf{y}}\right]$ has been determined satisfying the error tolerance specified by tolx and ir and such that $f\left({\mathbf{x}}\right)×f\left({\mathbf{y}}\right)\le 0$. However, from observation of the values of $f$ during the calculation of $\left[{\mathbf{x}},{\mathbf{y}}\right]$, it seems that the interval $\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 $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains a zero, or the error exit may not be taken when $\left[{\mathbf{x}},{\mathbf{y}}\right]$ contains a pole. Both these cases occur most frequently when tolx is large.
${\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\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.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

Accuracy

The accuracy of the final value x as an approximation of the zero is determined by tolx and ir (see [Parameters]). A relative accuracy criterion (${\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 $\left[{\mathbf{x}},{\mathbf{y}}\right]$ is transformed linearly to the interval $\left[1,2\right]$, then the zero can be determined to a precise number of figures using an absolute (${\mathbf{ir}}=1$) or relative (${\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.

Parallelism and Performance

None.

For most problems, the time taken on each call to c05az will be negligible compared with the time spent evaluating $f\left(x\right)$ between calls to c05az.
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 and, possibly, when ${\mathbf{ifail}}={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$ 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.
This example calculates a zero of ${e}^{-x}-x$ with an initial interval $\left[0,1\right]$, ${\mathbf{tolx}}=\text{1.0E−5}$ and a mixed error test.