e04hc checks that a method for evaluating an objective function and its first derivatives produces derivative values which are consistent with the function values calculated.

# Syntax

C# |
---|

public static void e04hc( int n, E04..::..E04HC_FUNCT funct, double[] x, out double f, double[] g, out int ifail ) |

Visual Basic |
---|

Public Shared Sub e04hc ( _ n As Integer, _ funct As E04..::..E04HC_FUNCT, _ x As Double(), _ <OutAttribute> ByRef f As Double, _ g As Double(), _ <OutAttribute> ByRef ifail As Integer _ ) |

Visual C++ |
---|

public: static void e04hc( int n, E04..::..E04HC_FUNCT^ funct, array<double>^ x, [OutAttribute] double% f, array<double>^ g, [OutAttribute] int% ifail ) |

F# |
---|

static member e04hc : n : int * funct : E04..::..E04HC_FUNCT * x : float[] * f : float byref * g : float[] * ifail : int byref -> unit |

#### Parameters

- n
- Type: System..::..Int32
*On entry*: the number $n$ of independent variables in the objective function.*Constraint*: ${\mathbf{n}}\ge 1$.

- funct
- Type: NagLibrary..::..E04..::..E04HC_FUNCTfunct must evaluate the function and its first derivatives at a given point. (The minimization methods mentioned in [Description] gives you the option of resetting parameters of funct to cause the minimization process to terminate immediately. e04hc will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
A delegate of type E04HC_FUNCT.

- x
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On entry*: ${\mathbf{x}}\left[\mathit{j}-1\right]$, for $\mathit{j}=1,2,\dots ,n$, must be set to the coordinates of a suitable point at which to check the derivatives calculated by funct. ‘Obvious’ settings, such as $0.0\text{ or}1.0$, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors could go undetected. Similarly, it is preferable that no two elements of x should be the same.

- f
- Type: System..::..Double%

- g
- Type: array<System..::..Double>[]()[][]An array of size [n]

- 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

Routines for minimizing a function of several variables may require you to supply a method to evaluate the objective function $F\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$ and its first derivatives. e04hc is designed to check the derivatives calculated by such user-supplied delegates . As well as the method to be checked ( funct), you must supply a point $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the check will be made. Note that e04hc checks methods of the form required for e04kd and e04lb.

e04hc first calls funct to evaluate $F$ and its first derivatives ${g}_{\mathit{j}}=\frac{\partial F}{\partial {x}_{\mathit{j}}}$, for $\mathit{j}=1,2,\dots ,n$ at $x$. The components of the user-supplied derivatives along two orthogonal directions (defined by unit vectors ${p}_{1}$ and ${p}_{2}$, say) are then calculated; these will be ${g}^{\mathrm{T}}{p}_{1}$ and ${g}^{\mathrm{T}}{p}_{2}$ respectively. The same components are also estimated by finite differences, giving quantities

where $h$ is a small positive scalar. If the relative difference between ${v}_{1}$ and ${g}^{\mathrm{T}}{p}_{1}$ or between ${v}_{2}$ and ${g}^{\mathrm{T}}{p}_{2}$ is judged too large, an error indicator is set.

$${v}_{k}=\frac{F\left(x+h{p}_{k}\right)-F\left(x\right)}{h}\text{, \hspace{1em}}k=1,2$$ |

# References

None.

# Error Indicators and Warnings

**Note:**e04hc may return useful information for one or more of the following detected errors or warnings.

Errors or warnings detected by the method:

- ${\mathbf{ifail}}<0$

- ${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}<1$, or ${\mathbf{liw}}<1$, or ${\mathbf{lw}}<3\times {\mathbf{n}}$.

- ${\mathbf{ifail}}=2$
- You should check carefully the derivation and programming of expressions for the derivatives of $F\left(x\right)$, because it is very unlikely that funct is calculating them correctly.

# Accuracy

ifail is set to $2$ if

for $k=1\text{ or}2$. (See [Description] for definitions of the quantities involved.) The scalar $h$ is set equal to $\sqrt{\epsilon}$, where $\epsilon $ is the machine precision as given by x02aj.

$${\left({v}_{k}-{g}^{\mathrm{T}}{p}_{k}\right)}^{2}\ge h\times \left({\left({g}^{\mathrm{T}}{p}_{k}\right)}^{2}+1\right)$$ |

# Parallelism and Performance

None.

# Further Comments

funct is called $3$ times.

Before using e04hc to check the calculation of first derivatives, you should be confident that funct is calculating $F$ correctly. The usual way of checking the calculation of the function is to compare values of $F\left(x\right)$ calculated by funct at nontrivial points $x$ with values calculated independently. (‘Non-trivial’ means that, as when setting $x$ before calling e04hc, coordinates such as $0.0$ or $1.0$ should be avoided.)

e04hc only checks the derivatives calculated when ${\mathbf{iflag}}=2$. So, if funct is intended for use in conjunction with a minimization method which may set iflag to $1$, you must check that, for given settings of the ${\mathbf{xc}}\left[j-1\right]$, funct produces the same values for the ${\mathbf{gc}}\left[j-1\right]$ when iflag is set to $1$ as when iflag is set to $2$.