e04hd checks that a method for calculating second derivatives of an objective function is consistent with a method for calculating the corresponding first derivatives.

# Syntax

C#
public static void e04hd(
int n,
E04..::..E04HD_FUNCT funct,
E04..::..E04HD_H h,
double[] x,
double[] g,
double[] hesl,
double[] hesd,
out int ifail
)
Visual Basic
Public Shared Sub e04hd ( _
n As Integer, _
funct As E04..::..E04HD_FUNCT, _
h As E04..::..E04HD_H, _
x As Double(), _
g As Double(), _
hesl As Double(), _
hesd As Double(), _
<OutAttribute> ByRef ifail As Integer _
)
Visual C++
public:
static void e04hd(
int n,
E04..::..E04HD_FUNCT^ funct,
E04..::..E04HD_H^ h,
array<double>^ x,
array<double>^ g,
array<double>^ hesl,
array<double>^ hesd,
[OutAttribute] int% ifail
)
F#
static member e04hd :
n : int *
funct : E04..::..E04HD_FUNCT *
h : E04..::..E04HD_H *
x : float[] *
g : float[] *
hesl : float[] *
hesd : 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..::..E04HD_FUNCT
funct must evaluate the function and its first derivatives at a given point. (e04lb gives you the option of resetting parameters of funct to cause the minimization process to terminate immediately. e04hd will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)

A delegate of type E04HD_FUNCT.

e04hc should be used to check the first derivatives calculated by funct before e04hd is used to check the second derivatives, since e04hd assumes that the first derivatives are correct.
h
Type: NagLibrary..::..E04..::..E04HD_H
h must evaluate the second derivatives of the function at a given point. (As with funct, a parameter can be set to cause immediate termination.)

A delegate of type E04HD_H.

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 contain 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 advisable that no two elements of x should be the same.
g
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: unless you set iflag negative in the first call of funct, ${\mathbf{g}}\left[\mathit{j}-1\right]$ contains the value of the first derivative $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point given in x, as calculated by funct, for $\mathit{j}=1,2,\dots ,{\mathbf{n}}$.
hesl
Type: array<System..::..Double>[]()[][]
An array of size [lh]
On exit: unless you set iflag negative in h, hesl contains the strict lower triangle of the second derivative matrix of $F$, as evaluated by h at the point given in x, stored by rows.
hesd
Type: array<System..::..Double>[]()[][]
An array of size [n]
On exit: unless you set iflag negative in h, hesd contains the diagonal elements of the second derivative matrix of $F$, as evaluated by h at the point given in x.
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 $F\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$ of the variables ${x}_{1},{x}_{2},\dots ,{x}_{n}$ may require you to provide a method to evaluate the second derivatives of $F$. e04hd is designed to check the second derivatives calculated by such user-supplied delegates. As well as the method to be checked (h), you must supply a method (funct) to evaluate the first derivatives, and a point $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the checks will be made. Note that e04hd checks methods of the form required for e04lb.
e04hd first calls user-supplied delegates funct and h to evaluate the first and second derivatives of $F$ at $x$. The user-supplied Hessian matrix ($H$, say) is projected onto two orthogonal vectors $y$ and $z$ to give the scalars ${y}^{\mathrm{T}}Hy$ and ${z}^{\mathrm{T}}Hz$ respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
 $p=yTgx+hy-yTgx/handq=zTgx+hz-zTgx/h$
respectively, where $g\left(\right)$ denotes the vector of first derivatives at the point in brackets and $h$ is a small positive scalar. If the relative difference between $p$ and ${y}^{\mathrm{T}}Hy$ or between $q$ and ${z}^{\mathrm{T}}Hz$ is judged too large, an error indicator is set.

None.

# Error Indicators and Warnings

Note: e04hd may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the method:
Some error messages may refer to parameters that are dropped from this interface (IW, LIW, W, LW) In these cases, an error in another parameter has usually caused an incorrect value to be inferred.
${\mathbf{ifail}}<0$
A negative value of ifail indicates an exit from e04hd because you have set iflag negative in funct or h. The value of ifail will be the same as the value you set for iflag. The check on h will not have been completed.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<1$, or ${\mathbf{lh}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$,
${\mathbf{ifail}}=2$
You should check carefully the derivation and programming of expressions for the second derivatives of $F\left(x\right)$, because it is very unlikely that h is calculating them correctly.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

ifail is set to $2$ if
 $yTHy-p≥h×yTHy+1.0 or ​zTHz-q≥h×zTHz+1.0$
where $h$ is set equal to $\sqrt{\epsilon }$ ($\epsilon$ being the machine precision as given by x02aj) and other quantities are as defined in [Description].

None.

# Further Comments

e04hd calls h once and funct three times.