E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentE04BBF/E04BBA

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

E04BBF/E04BBA searches for a minimum, in a given finite interval, of a continuous function of a single variable, using function and first derivative values. The method (based on cubic interpolation) is intended for functions which have a continuous first derivative (although it will usually work if the derivative has occasional discontinuities).
E04BBA is a version of E04BBF that has additional parameters in order to make it safe for use in multithreaded applications (see Section 5).

## 2  Specification

### 2.1  Specification for E04BBF

 SUBROUTINE E04BBF ( FUNCT, E1, E2, A, B, MAXCAL, X, F, G, IFAIL)
 INTEGER MAXCAL, IFAIL REAL (KIND=nag_wp) E1, E2, A, B, X, F, G EXTERNAL FUNCT

### 2.2  Specification for E04BBA

 SUBROUTINE E04BBA ( FUNCT, E1, E2, A, B, MAXCAL, X, F, G, IUSER, RUSER, IFAIL)
 INTEGER MAXCAL, IUSER(*), IFAIL REAL (KIND=nag_wp) E1, E2, A, B, X, F, G, RUSER(*) EXTERNAL FUNCT

## 3  Description

E04BBF/E04BBA is applicable to problems of the form:
 $Minimize⁡Fx subject to a≤x≤b$
when the first derivative $\frac{dF}{dx}$ can be calculated. The routine normally computes a sequence of $x$ values which tend in the limit to a minimum of $F\left(x\right)$ subject to the given bounds. It also progressively reduces the interval $\left[a,b\right]$ in which the minimum is known to lie. It uses the safeguarded cubic-interpolation method described in Gill and Murray (1973).
You must supply a FUNCT to evaluate $F\left(x\right)$ and $\frac{dF}{dx}$. The parameters E1 and E2 together specify the accuracy
 $Tolx=E1×x+E2$
to which the position of the minimum is required. Note that FUNCT is never called at a point which is closer than $\mathit{Tol}\left(x\right)$ to a previous point.
If the original interval $\left[a,b\right]$ contains more than one minimum, E04BBF/E04BBA will normally find one of the minima.

## 4  References

Gill P E and Murray W (1973) Safeguarded steplength algorithms for optimization using descent methods NPL Report NAC 37 National Physical Laboratory

## 5  Parameters

1:     $\mathrm{FUNCT}$ – SUBROUTINE, supplied by the user.External Procedure
You must supply this routine to calculate the values of $F\left(x\right)$ and $\frac{dF}{dx}$ at any point $x$ in $\left[a,b\right]$.
It should be tested separately before being used in conjunction with E04BBF/E04BBA.
The specification of FUNCT for E04BBF is:
 SUBROUTINE FUNCT ( XC, FC, GC)
 REAL (KIND=nag_wp) XC, FC, GC
The specification of FUNCT for E04BBA is:
 SUBROUTINE FUNCT ( XC, FC, GC, IUSER, RUSER)
 INTEGER IUSER(*) REAL (KIND=nag_wp) XC, FC, GC, RUSER(*)
1:     $\mathrm{XC}$ – REAL (KIND=nag_wp)Input
On entry: the point $x$ at which the values of $F$ and $\frac{dF}{dx}$ are required.
2:     $\mathrm{FC}$ – REAL (KIND=nag_wp)Output
On exit: must be set to the value of the function $F$ at the current point $x$.
3:     $\mathrm{GC}$ – REAL (KIND=nag_wp)Output
On exit: must be set to the value of the first derivative $\frac{dF}{dx}$ at the current point $x$.
Note: the following are additional parameters for specific use with E04BBA. Users of E04BBF therefore need not read the remainder of this description.
4:     $\mathrm{IUSER}\left(*\right)$ – INTEGER arrayUser Workspace
5:     $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace
FUNCT is called with the parameters IUSER and RUSER as supplied to E04BBF/E04BBA. You are free to use the arrays IUSER and RUSER to supply information to FUNCT as an alternative to using COMMON global variables.
FUNCT must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04BBF/E04BBA is called. Parameters denoted as Input must not be changed by this procedure.
2:     $\mathrm{E1}$ – REAL (KIND=nag_wp)Input/Output
On entry: the relative accuracy to which the position of a minimum is required. (Note that, since E1 is a relative tolerance, the scaling of $x$ is automatically taken into account.)
E1 should be no smaller than $2\epsilon$, and preferably not much less than $\sqrt{\epsilon }$, where $\epsilon$ is the machine precision.
On exit: if you set E1 to $0.0$ (or to any value less than $\epsilon$), E1 will be reset to the default value $\sqrt{\epsilon }$ before starting the minimization process.
3:     $\mathrm{E2}$ – REAL (KIND=nag_wp)Input/Output
On entry: the absolute accuracy to which the position of a minimum is required. E2 should be no smaller than $2\epsilon$.
On exit: if you set E2 to $0.0$ (or to any value less than $\epsilon$), E2 will be reset to the default value $\sqrt{\epsilon }$.
4:     $\mathrm{A}$ – REAL (KIND=nag_wp)Input/Output
On entry: the lower bound $a$ of the interval containing a minimum.
On exit: an improved lower bound on the position of the minimum.
5:     $\mathrm{B}$ – REAL (KIND=nag_wp)Input/Output
On entry: the upper bound $b$ of the interval containing a minimum.
On exit: an improved upper bound on the position of the minimum.
6:     $\mathrm{MAXCAL}$ – INTEGERInput/Output
On entry: the maximum number of calls of FUNCT to be allowed.
Constraint: ${\mathbf{MAXCAL}}\ge 2$. (Few problems will require more than $20$.)
There will be an error exit (see Section 6) after MAXCAL calls of FUNCT
On exit: the total number of times that FUNCT was actually called.
7:     $\mathrm{X}$ – REAL (KIND=nag_wp)Output
On exit: the estimated position of the minimum.
8:     $\mathrm{F}$ – REAL (KIND=nag_wp)Output
On exit: the function value at the final point given in X.
9:     $\mathrm{G}$ – REAL (KIND=nag_wp)Output
On exit: the value of the first derivative at the final point in X.
10:   $\mathrm{IFAIL}$ – INTEGERInput/Output
Note: for E04BBA, IFAIL does not occur in this position in the parameter list. See the additional parameters described below.
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction 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 routine the values of the output parameters may be useful even if ${\mathbf{IFAIL}}\ne {\mathbf{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 exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
Note: the following are additional parameters for specific use with E04BBA. Users of E04BBF therefore need not read the remainder of this description.
10:   $\mathrm{IUSER}\left(*\right)$ – INTEGER arrayUser Workspace
11:   $\mathrm{RUSER}\left(*\right)$ – REAL (KIND=nag_wp) arrayUser Workspace
IUSER and RUSER are not used by E04BBF/E04BBA, but are passed directly to FUNCT and may be used to pass information to this routine as an alternative to using COMMON global variables.
12:   $\mathrm{IFAIL}$ – INTEGERInput/Output
Note: see the parameter description for IFAIL above.

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: E04BBF/E04BBA may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, $\left({\mathbf{A}}+{\mathbf{E2}}\right)\ge {\mathbf{B}}$, or ${\mathbf{MAXCAL}}<2$.
${\mathbf{IFAIL}}=2$
The number of calls of FUNCT has exceeded MAXCAL. This may have happened simply because MAXCAL was set too small for a particular problem, or may be due to a mistake in FUNCT. If no mistake can be found in FUNCT, restart E04BBF/E04BBA (preferably with the values of A and B given on exit from the previous call of E04BBF/E04BBA).
${\mathbf{IFAIL}}=-99$
See Section 3.8 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.7 in the Essential Introduction for further information.
${\mathbf{IFAIL}}=-999$
Dynamic memory allocation failed.
See Section 3.6 in the Essential Introduction for further information.

## 7  Accuracy

If $F\left(x\right)$ is $\delta$-unimodal for some $\delta <\mathit{Tol}\left(x\right)$, where $\mathit{Tol}\left(x\right)={\mathbf{E1}}×\left|x\right|+{\mathbf{E2}}$, then, on exit, $x$ approximates the minimum of $F\left(x\right)$ in the original interval $\left[a,b\right]$ with an error less than $3×\mathit{Tol}\left(x\right)$.

## 8  Parallelism and Performance

Not applicable.

Timing depends on the behaviour of $F\left(x\right)$, the accuracy demanded and the length of the interval $\left[a,b\right]$. Unless $F\left(x\right)$ and $\frac{dF}{dx}$ can be evaluated very quickly, the run time will usually be dominated by the time spent in FUNCT.
If $F\left(x\right)$ has more than one minimum in the original interval $\left[a,b\right]$, E04BBF/E04BBA will determine an approximation $x$ (and improved bounds $a$ and $b$) for one of the minima.
If E04BBF/E04BBA finds an $x$ such that $F\left(x-{\delta }_{1}\right)>F\left(x\right) for some ${\delta }_{1},{\delta }_{2}\ge \mathit{Tol}\left(x\right)$, the interval $\left[x-{\delta }_{1},x+{\delta }_{2}\right]$ will be regarded as containing a minimum, even if $F\left(x\right)$ is less than $F\left(x-{\delta }_{1}\right)$ and $F\left(x+{\delta }_{2}\right)$ only due to rounding errors in the subroutine. Therefore FUNCT should be programmed to calculate $F\left(x\right)$ as accurately as possible, so that E04BBF/E04BBA will not be liable to find a spurious minimum. (For similar reasons, $\frac{dF}{dx}$ should be evaluated as accurately as possible.)

## 10  Example

A sketch of the function
 $Fx=sin⁡xx$
shows that it has a minimum somewhere in the range $\left[3.5,5.0\right]$. The following program shows how E04BBF/E04BBA can be used to obtain a good approximation to the position of a minimum.

### 10.1  Program Text

Note: the following programs illustrate the use of E04BBF and E04BBA.

Program Text (e04bbfe.f90)

Program Text (e04bbae.f90)

None.

### 10.3  Program Results

Program Results (e04bbfe.r)

Program Results (e04bbae.r)