# NAG Library Routine Document

## 1Purpose

e04cbf minimizes a general function $F\left(\mathbf{x}\right)$ of $n$ independent variables $\mathbf{x}={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ by the Nelder and Mead simplex method (see Nelder and Mead (1965)). Derivatives of the function need not be supplied.

## 2Specification

Fortran Interface
 Subroutine e04cbf ( n, x, f, tolf, tolx,
 Integer, Intent (In) :: n, maxcal Integer, Intent (Inout) :: iuser(*), ifail Real (Kind=nag_wp), Intent (In) :: tolf, tolx Real (Kind=nag_wp), Intent (Inout) :: x(n), ruser(*) Real (Kind=nag_wp), Intent (Out) :: f External :: funct, monit
#include nagmk26.h
 void e04cbf_ (const Integer *n, double x[], double *f, const double *tolf, const double *tolx, void (NAG_CALL *funct)(const Integer *n, const double xc[], double *fc, Integer iuser[], double ruser[]),void (NAG_CALL *monit)(const double *fmin, const double *fmax, const double sim[], const Integer *n, const Integer *ncall, const double *serror, const double *vratio, Integer iuser[], double ruser[]),const Integer *maxcal, Integer iuser[], double ruser[], Integer *ifail)

## 3Description

e04cbf finds an approximation to a minimum of a function $F$ of $n$ variables. You must supply a subroutine to calculate the value of $F$ for any set of values of the variables.
The method is iterative. A simplex of $n+1$ points is set up in the $n$-dimensional space of the variables (for example, in $2$ dimensions the simplex is a triangle) under the assumption that the problem has been scaled so that the values of the independent variables at the minimum are of order unity. The starting point you have provided is the first vertex of the simplex, the remaining $n$ vertices are generated by e04cbf. The vertex of the simplex with the largest function value is reflected in the centre of gravity of the remaining vertices and the function value at this new point is compared with the remaining function values. Depending on the outcome of this test the new point is accepted or rejected, a further expansion move may be made, or a contraction may be carried out. See Nelder and Mead (1965) and Parkinson and Hutchinson (1972) for more details. When no further progress can be made the sides of the simplex are reduced in length and the method is repeated.
The method can be slow, but computational bottlenecks have been reduced following Singer and Singer (2004). However, e04cbf is robust, and therefore very useful for functions that are subject to inaccuracies.
There are the following options for successful termination of the method: based only on the function values at the vertices of the current simplex (see (1)); based only on a volume ratio between the current simplex and the initial one (see (2)); or based on which one of the previous two tests passes first. The volume test may be useful if $F$ is discontinuous, while the function-value test should be sufficient on its own if $F$ is continuous.

## 4References

Nelder J A and Mead R (1965) A simplex method for function minimization Comput. J. 7 308–313
Parkinson J M and Hutchinson D (1972) An investigation into the efficiency of variants of the simplex method Numerical Methods for Nonlinear Optimization (ed F A Lootsma) Academic Press
Singer S and Singer S (2004) Efficient implementation of the Nelder–Mead search algorithm Appl. Num. Anal. Comp. Math. 1(3) 524–534

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of variables.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: a guess at the position of the minimum. Note that the problem should be scaled so that the values of the ${\mathbf{x}}\left(i\right)$ are of order unity.
On exit: the value of $\mathbf{x}$ corresponding to the function value in f.
3:     $\mathbf{f}$ – Real (Kind=nag_wp)Output
On exit: the lowest function value found.
4:     $\mathbf{tolf}$ – Real (Kind=nag_wp)Input
On entry: the error tolerable in the function values, in the following sense. If ${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,n+1$, are the individual function values at the vertices of the current simplex, and if ${f}_{m}$ is the mean of these values, then you can request that e04cbf should terminate if
 $1 n+1 ∑ i=1 n+1 fi - fm 2 < tolf .$ (1)
You may specify ${\mathbf{tolf}}=0$ if you wish to use only the termination criterion (2) on the spatial values: see the description of tolx.
Constraint: ${\mathbf{tolf}}$ must be greater than or equal to the machine precision (see Chapter X02), or if tolf equals zero then tolx must be greater than or equal to the machine precision.
5:     $\mathbf{tolx}$ – Real (Kind=nag_wp)Input
On entry: the error tolerable in the spatial values, in the following sense. If $LV$ denotes the ‘linearized’ volume of the current simplex, and if ${LV}_{\mathrm{init}}$ denotes the ‘linearized’ volume of the initial simplex, then you can request that e04cbf should terminate if
 $LV LV init < tolx .$ (2)
You may specify ${\mathbf{tolx}}=0$ if you wish to use only the termination criterion (1) on function values: see the description of tolf.
Constraint: ${\mathbf{tolx}}$ must be greater than or equal to the machine precision (see Chapter X02), or if tolx equals zero then tolf must be greater than or equal to the machine precision.
6:     $\mathbf{funct}$ – Subroutine, supplied by the user.External Procedure
funct must evaluate the function $F$ at a specified point. It should be tested separately before being used in conjunction with e04cbf.
The specification of funct is:
Fortran Interface
 Subroutine funct ( n, xc, fc,
 Integer, Intent (In) :: n Integer, Intent (Inout) :: iuser(*) Real (Kind=nag_wp), Intent (In) :: xc(n) Real (Kind=nag_wp), Intent (Inout) :: ruser(*) Real (Kind=nag_wp), Intent (Out) :: fc
#include nagmk26.h
 void funct (const Integer *n, const double xc[], double *fc, Integer iuser[], double ruser[])
1:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of variables.
2:     $\mathbf{xc}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the point at which the function value is required.
3:     $\mathbf{fc}$ – Real (Kind=nag_wp)Output
On exit: the value of the function $F$ at the current point $\mathbf{x}$.
4:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
5:     $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
funct is called with the arguments iuser and ruser as supplied to e04cbf. You should use the arrays iuser and ruser to supply information to funct.
funct must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04cbf is called. Arguments denoted as Input must not be changed by this procedure.
Note: funct should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04cbf. If your code inadvertently does return any NaNs or infinities, e04cbf is likely to produce unexpected results.
7:     $\mathbf{monit}$ – Subroutine, supplied by the NAG Library or the user.External Procedure
monit may be used to monitor the optimization process. It is invoked once every iteration.
If no monitoring is required, monit may be the dummy monitoring routine e04cbk supplied by the NAG Library.
The specification of monit is:
Fortran Interface
 Subroutine monit ( fmin, fmax, sim, n,
 Integer, Intent (In) :: n, ncall Integer, Intent (Inout) :: iuser(*) Real (Kind=nag_wp), Intent (In) :: fmin, fmax, sim(n+1,n), serror, vratio Real (Kind=nag_wp), Intent (Inout) :: ruser(*)
#include nagmk26.h
 void monit (const double *fmin, const double *fmax, const double sim[], const Integer *n, const Integer *ncall, const double *serror, const double *vratio, Integer iuser[], double ruser[])
1:     $\mathbf{fmin}$ – Real (Kind=nag_wp)Input
On entry: the smallest function value in the current simplex.
2:     $\mathbf{fmax}$ – Real (Kind=nag_wp)Input
On entry: the largest function value in the current simplex.
3:     $\mathbf{sim}\left({\mathbf{n}}+1,{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the $n+1$ position vectors of the current simplex.
4:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of variables.
5:     $\mathbf{ncall}$ – IntegerInput
On entry: the number of times that funct has been called so far.
6:     $\mathbf{serror}$ – Real (Kind=nag_wp)Input
On entry: the current value of the standard deviation in function values used in termination test (1).
7:     $\mathbf{vratio}$ – Real (Kind=nag_wp)Input
On entry: the current value of the linearized volume ratio used in termination test (2).
8:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
9:     $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
monit is called with the arguments iuser and ruser as supplied to e04cbf. You should use the arrays iuser and ruser to supply information to monit.
monit must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which e04cbf is called. Arguments denoted as Input must not be changed by this procedure.
8:     $\mathbf{maxcal}$ – IntegerInput
On entry: the maximum number of function evaluations to be allowed.
Constraint: ${\mathbf{maxcal}}\ge 1$.
9:     $\mathbf{iuser}\left(*\right)$ – Integer arrayUser Workspace
10:   $\mathbf{ruser}\left(*\right)$ – Real (Kind=nag_wp) arrayUser Workspace
iuser and ruser are not used by e04cbf, but are passed directly to funct and monit and may be used to pass information to these routines.
11:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ 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\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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{maxcal}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{maxcal}}\ge 1$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
On entry, ${\mathbf{tolf}}=0.0$ and ${\mathbf{tolx}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{tolf}}=0.0$ then ${\mathbf{tolx}}$ is greater than or equal to the machine precision.
On entry, ${\mathbf{tolf}}=〈\mathit{\text{value}}〉$ and ${\mathbf{tolx}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{tolf}}\ne 0.0$ and ${\mathbf{tolx}}\ne 0.0$ then both should be greater than or equal to the machine precision.
On entry, ${\mathbf{tolx}}=0.0$ and ${\mathbf{tolf}}=〈\mathit{\text{value}}〉$.
Constraint: if ${\mathbf{tolx}}=0.0$ then ${\mathbf{tolf}}$ is greater than or equal to the machine precision.
${\mathbf{ifail}}=2$
maxcal function evaluations have been completed without any other termination test passing. Check the coding of funct before increasing the value of maxcal.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

On a successful exit the accuracy will be as defined by tolf or tolx, depending on which criterion was satisfied first.

## 8Parallelism and Performance

e04cbf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.

Local workspace arrays of fixed lengths (depending on n) are allocated internally by e04cbf. The total size of these arrays amounts to ${{\mathbf{n}}}^{2}+6{\mathbf{n}}+2$ real elements.
The time taken by e04cbf depends on the number of variables, the behaviour of the function and the distance of the starting point from the minimum. Each iteration consists of $1$ or $2$ function evaluations unless the size of the simplex is reduced, in which case $n+1$ function evaluations are required.

## 10Example

This example finds a minimum of the function
 $F x1,x2 = ex1 4 x12 + 2 x22 + 4 x1 x2 + 2 x2 + 1 .$
This example uses the initial point $\left(-1,1\right)$ (see Section 10.3), and we expect to reach the minimum at $\left(0.5,-1\right)$.

### 10.1Program Text

Program Text (e04cbfe.f90)

None.

### 10.3Program Results

Program Results (e04cbfe.r)

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