NAG Library Routine Document

c05azf  (contfn_brent_rcomm)

 Contents

    1  Purpose
    7  Accuracy

1
Purpose

c05azf 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.

2
Specification

Fortran Interface
Subroutine c05azf ( x, y, fx, tolx, ir, c, ind, ifail)
Integer, Intent (In):: ir
Integer, Intent (Inout):: ind, ifail
Real (Kind=nag_wp), Intent (In):: fx, tolx
Real (Kind=nag_wp), Intent (Inout):: x, y, c(17)
C Header Interface
#include nagmk26.h
void  c05azf_ ( double *x, double *y, const double *fx, const double *tolx, const Integer *ir, double c[], Integer *ind, Integer *ifail)

3
Description

You must supply x and y to define an initial interval a,b  containing a simple zero of the function fx  (the choice of x and y must be such that fx × fy 0.0 ). The routine 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  contains the zero and x-y  is less than some tolerance specified by tolx and ir (see Section 5). In fact, since the intermediate intervals x,y  are determined only so that fx × fy 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). c05azf 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 routine is that unlike some other methods it guarantees convergence within about log2 b-a / δ 2  function evaluations, where δ is related to the argument tolx. See Brent (1973) for more details.
c05azf returns to the calling program for each evaluation of fx . On each return you should set fx = fx  and call c05azf again.
The routine is a modified version of procedure ‘zeroin’ given by Brent (1973).

4
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

5
Arguments

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 than fx must remain unchanged.
1:     x – Real (Kind=nag_wp)Input/Output
2:     y – Real (Kind=nag_wp)Input/Output
On initial entry: x and y must define an initial interval a,b  containing the zero, such that fx × fy0.0 . It is not necessary that x<y .
On intermediate exit: x contains the point at which f must be evaluated before re-entry to the routine.
On final exit: x and y define a smaller interval containing the zero, such that fx×fy0.0 , and x-y  satisfies the accuracy specified by tolx and ir, unless an error has occurred. If ifail=4, x and y generally contain very good approximations to a pole; if ifail=5, x and y generally contain very good approximations to the zero (see Section 6). If a point x is found such that fx=0.0 , on final exit x=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:     fx – Real (Kind=nag_wp)Input
On initial entry: if ind=1, fx need not be set.
If ind=-1, fx must contain fx  for the initial value of x.
On intermediate re-entry: must contain fx  for the current value of x.
4:     tolx – Real (Kind=nag_wp)Input
On initial entry: the accuracy to which the zero is required. The type of error test is specified by ir.
Constraint: tolx>0.0 .
5:     ir – IntegerInput
On initial entry: indicates the type of error test.
ir=0
The test is: x-y2.0×tolx×max1.0,x .
ir=1
The test is: x-y2.0×tolx .
ir=2
The test is: x-y2.0×tolx×x .
Suggested value: ir=0.
Constraint: ir=0, 1 or 2.
6:     c17 – Real (Kind=nag_wp) arrayInput/Output
On initial entry: if ind=1 , no elements of c need be set.
If ind=-1 , c1  must contain fy , other elements of c need not be set.
On final exit: is undefined.
7:     ind – IntegerInput/Output
On initial entry: must be set to 1 or -1 .
ind=1
fx and c1  need not be set.
ind=-1
fx and c1  must contain fx  and fy  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 c05azf with all other arguments unchanged.
On final exit: contains 0.
Constraint: on entry ind=-1, 1, 2, 3 or 4.
Note: any values you return to c05azf 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 c05azf. If your code inadvertently does return any NaNs or infinities, c05azf is likely to produce unexpected results.
8:     ifail – IntegerInput/Output
On initial entry: ifail must be set to 0, -1​ or ​1. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if ifail0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of ifail on exit.
On final exit: ifail=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6
Error Indicators and Warnings

If on entry 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:
ifail=1
On entry, fx  and fy  have the same sign with neither equalling 0.0 .
ifail=2
On entry, ind-1, 1, 2, 3 or 4.
ifail=3
On entry,tolx0.0,
orir0, 1 or 2.
ifail=4
An interval x,y  has been determined satisfying the error tolerance specified by tolx and ir and such that fx × fy 0 . However, from observation of the values of f during the calculation of x,y , it seems that the interval x,y  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  contains a zero, or the error exit may not be taken when x,y  contains a pole. Both these cases occur most frequently when tolx is large.
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ε, where ε 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.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

7
Accuracy

The accuracy of the final value x as an approximation of the zero is determined by tolx and ir (see Section 5). A relative accuracy criterion ( 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  is transformed linearly to the interval 1,2 , then the zero can be determined to a precise number of figures using an absolute ( ir=1 ) or relative ( 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.

8
Parallelism and Performance

c05azf is not threaded in any implementation.

9
Further Comments

For most problems, the time taken on each call to c05azf will be negligible compared with the time spent evaluating fx  between calls to c05azf.
If the calculation terminates because fx=0.0 , then on return y is set to x. (In fact, y=x  on return only in this case and, possibly, when 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 fx=0.0 , then x must correspond to a multiple zero of f rather than a simple zero.

10
Example

This example calculates a zero of e-x - x  with an initial interval 0,1 , tolx=1.0E−5  and a mixed error test.

10.1
Program Text

Program Text (c05azfe.f90)

10.2
Program Data

None.

10.3
Program Results

Program Results (c05azfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017