# NAG FL Interfacec05ayf (contfn_​brent)

## 1Purpose

c05ayf locates a simple zero of a continuous function in a given interval using Brent's method, which is a combination of nonlinear interpolation, linear extrapolation and bisection.

## 2Specification

Fortran Interface
 Subroutine c05ayf ( a, b, eps, eta, f, x,
 Integer, Intent (Inout) :: iuser(*), ifail Real (Kind=nag_wp), External :: f Real (Kind=nag_wp), Intent (In) :: a, b, eps, eta Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Real (Kind=nag_wp), Intent (Out) :: x
#include <nag.h>
 void c05ayf_ (const double *a, const double *b, const double *eps, const double *eta, double (NAG_CALL *f)(const double *x, Integer iuser[], double ruser[]),double *x, Integer iuser[], double ruser[], Integer *ifail)
The routine may be called by the names c05ayf or nagf_roots_contfn_brent.

## 3Description

c05ayf attempts to obtain an approximation to a simple zero of the function $f\left(x\right)$ given an initial interval $\left[a,b\right]$ such that $f\left(a\right)×f\left(b\right)\le 0$. The same core algorithm is used by c05azf whose specification should be consulted for details of the method used.
The approximation $x$ to the zero $\alpha$ is determined so that at least one of the following criteria is satisfied:
1. (i)$\left|x-\alpha \right|\le {\mathbf{eps}}$,
2. (ii)$\left|f\left(x\right)\right|\le {\mathbf{eta}}$.
Brent R P (1973) Algorithms for Minimization Without Derivatives Prentice–Hall

## 5Arguments

1: $\mathbf{a}$Real (Kind=nag_wp) Input
On entry: $a$, the lower bound of the interval.
2: $\mathbf{b}$Real (Kind=nag_wp) Input
On entry: $b$, the upper bound of the interval.
Constraint: ${\mathbf{b}}\ne {\mathbf{a}}$.
3: $\mathbf{eps}$Real (Kind=nag_wp) Input
On entry: the termination tolerance on $x$ (see Section 3).
Constraint: ${\mathbf{eps}}>0.0$.
4: $\mathbf{eta}$Real (Kind=nag_wp) Input
On entry: a value such that if $\left|f\left(x\right)\right|\le {\mathbf{eta}}$, $x$ is accepted as the zero. eta may be specified as $0.0$ (see Section 7).
5: $\mathbf{f}$real (Kind=nag_wp) Function, supplied by the user. External Procedure
f must evaluate the function $f$ whose zero is to be determined.
The specification of f is:
Fortran Interface
 Function f ( x,
 Real (Kind=nag_wp) :: f Integer, Intent (Inout) :: iuser(*) Real (Kind=nag_wp), Intent (In) :: x Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
 double f_ (const double *x, Integer iuser[], double ruser[])
1: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: the point at which the function must be evaluated.
2: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
3: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
f is called with the arguments iuser and ruser as supplied to c05ayf. You should use the arrays iuser and ruser to supply information to f.
f must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which c05ayf is called. Arguments denoted as Input must not be changed by this procedure.
Note: f should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by c05ayf. If your code inadvertently does return any NaNs or infinities, c05ayf is likely to produce unexpected results.
6: $\mathbf{x}$Real (Kind=nag_wp) Output
On exit: if ${\mathbf{ifail}}={\mathbf{0}}$ or ${\mathbf{2}}$, x is the final approximation to the zero. If ${\mathbf{ifail}}={\mathbf{3}}$, x is likely to be a pole of $f\left(x\right)$. Otherwise, x contains no useful information.
7: $\mathbf{iuser}\left(*\right)$Integer array User Workspace
8: $\mathbf{ruser}\left(*\right)$Real (Kind=nag_wp) array User Workspace
iuser and ruser are not used by c05ayf, but are passed directly to f and may be used to pass information to this routine.
9: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, $-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 $-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 $-1$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On 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 $-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{a}}=〈\mathit{\text{value}}〉$ and ${\mathbf{b}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{a}}\ne {\mathbf{b}}$.
On entry, ${\mathbf{eps}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{eps}}>0.0$.
On entry, ${\mathbf{f}}\left({\mathbf{a}}\right)$ and ${\mathbf{f}}\left({\mathbf{b}}\right)$ have the same sign with neither equalling $0.0$: ${\mathbf{f}}\left({\mathbf{a}}\right)=〈\mathit{\text{value}}〉$ and ${\mathbf{f}}\left({\mathbf{b}}\right)=〈\mathit{\text{value}}〉$.
${\mathbf{ifail}}=2$
No further improvement in the solution is possible. eps is too small: ${\mathbf{eps}}=〈\mathit{\text{value}}〉$. The final value of x returned is an accurate approximation to the zero.
${\mathbf{ifail}}=3$
The function values in the interval $\left[{\mathbf{a}},{\mathbf{b}}\right]$ might contain a pole rather than a zero. Reducing eps may help in distinguishing between a pole and a zero.
${\mathbf{ifail}}=-99$
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

The levels of accuracy depend on the values of eps and eta. If full machine accuracy is required, they may be set very small, resulting in an exit with ${\mathbf{ifail}}={\mathbf{2}}$, although this may involve many more iterations than a lesser accuracy. You are recommended to set ${\mathbf{eta}}=0.0$ and to use eps to control the accuracy, unless you have considerable knowledge of the size of $f\left(x\right)$ for values of $x$ near the zero.

## 8Parallelism and Performance

c05ayf is not threaded in any implementation.

The time taken by c05ayf depends primarily on the time spent evaluating f (see Section 5).
If it is important to determine an interval of relative length less than $2×{\mathbf{eps}}$ containing the zero, or if f is expensive to evaluate and the number of calls to f is to be restricted, then use of c05azf is recommended. Use of c05azf is also recommended when the structure of the problem to be solved does not permit a simple f to be written: the reverse communication facilities of c05azf are more flexible than the direct communication of f required by c05ayf.

## 10Example

This example calculates an approximation to the zero of ${e}^{-x}-x$ within the interval $\left[0,1\right]$ using a tolerance of ${\mathbf{eps}}=\text{1.0E−5}$.

### 10.1Program Text

Program Text (c05ayfe.f90)

None.

### 10.3Program Results

Program Results (c05ayfe.r)