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

# Syntax

C#
```public static void e04cb(
int n,
double[] x,
out double f,
double tolf,
double tolx,
E04..::..E04CB_FUNCT funct,
E04..::..E04CB_MONIT monit,
int maxcal,
out int ifail
)```
Visual Basic
```Public Shared Sub e04cb ( _
n As Integer, _
x As Double(), _
<OutAttribute> ByRef f As Double, _
tolf As Double, _
tolx As Double, _
funct As E04..::..E04CB_FUNCT, _
monit As E04..::..E04CB_MONIT, _
maxcal As Integer, _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void e04cb(
int n,
array<double>^ x,
[OutAttribute] double% f,
double tolf,
double tolx,
E04..::..E04CB_FUNCT^ funct,
E04..::..E04CB_MONIT^ monit,
int maxcal,
[OutAttribute] int% ifail
)```
F#
```static member e04cb :
n : int *
x : float[] *
f : float byref *
tolf : float *
tolx : float *
funct : E04..::..E04CB_FUNCT *
monit : E04..::..E04CB_MONIT *
maxcal : int *
ifail : int byref -> unit
```

#### Parameters

n
Type: System..::..Int32
On entry: $n$, the number of variables.
Constraint: ${\mathbf{n}}\ge 1$.
x
Type: array<System..::..Double>[]()[][]
An array of size [n]
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-1\right]$ are of order unity.
On exit: the value of $\mathbf{x}$ corresponding to the function value in f.
f
Type: System..::..Double%
On exit: the lowest function value found.
tolf
Type: System..::..Double
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 e04cb should terminate if
 $1n+1∑i=1n+1fi-fm2 (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 X02 class), or if tolf equals zero then tolx must be greater than or equal to the machine precision.
tolx
Type: System..::..Double
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 e04cb should terminate if
 $LVLVinit (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 X02 class), or if tolx equals zero then tolf must be greater than or equal to the machine precision.
funct
Type: NagLibrary..::..E04..::..E04CB_FUNCT
funct must evaluate the function $F$ at a specified point. It should be tested separately before being used in conjunction with e04cb.

A delegate of type E04CB_FUNCT.

monit
Type: NagLibrary..::..E04..::..E04CB_MONIT
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 method E04CBK supplied by the NAG Library.

A delegate of type E04CB_MONIT.

maxcal
Type: System..::..Int32
On entry: the maximum number of function evaluations to be allowed.
Constraint: ${\mathbf{maxcal}}\ge 1$.
ifail
Type: System..::..Int32%
On exit: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

e04cb finds an approximation to a minimum of a function $F$ of $n$ variables. You must supply a method 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 e04cb. 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, e04cb 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.

# References

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

# Error Indicators and Warnings

Errors or warnings detected by the method:
${\mathbf{ifail}}=1$
An input parameter is invalid. The output message provides more details of the invalid argument.
${\mathbf{ifail}}=2$
maxcal function evaluations have been completed without termination test (1) or (2) passing. Check the coding of funct before increasing the value of maxcal.
${\mathbf{ifail}}=-999$
Internal memory allocation failed.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

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

# Parallelism and Performance

None.

Local workspace arrays of fixed lengths (depending on n) are allocated internally by e04cb. The total size of these arrays amounts to ${{\mathbf{n}}}^{2}+6{\mathbf{n}}+2$ real elements.
The time taken by e04cb 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.

# Example

This example finds a minimum of the function
 $Fx1,x2=ex14x12+2x22+4x1x2+2x2+1.$
This example uses the initial point $\left(-1,1\right)$ (see []), and we expect to reach the minimum at $\left(0.5,-1\right)$.

Example program (C#): e04cbe.cs

Example program results: e04cbe.r