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

- h
- Type: NagLibrary..::..E04..::..E04HD_Hh 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]

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

- hesd
- 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 $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

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.

$$\begin{array}{ll}p=\left({y}^{\mathrm{T}}g\left(x+hy\right)-{y}^{\mathrm{T}}g\left(x\right)\right)/h& \text{and}\\ & q=\left({z}^{\mathrm{T}}g\left(x+hz\right)-{z}^{\mathrm{T}}g\left(x\right)\right)/h\end{array}$$ |

# References

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$

- ${\mathbf{ifail}}=1$
On entry, ${\mathbf{n}}<1$, or ${\mathbf{lh}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}\times \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.

# Accuracy

ifail is set to $2$ if

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

$$\begin{array}{l}\left|{y}^{\mathrm{T}}Hy-p\right|\ge \sqrt{h}\times \left(\left|{y}^{\mathrm{T}}Hy\right|+1.0\right)\text{\hspace{1em} or}\\ \left|{z}^{\mathrm{T}}Hz-q\right|\ge \sqrt{h}\times \left(\left|{z}^{\mathrm{T}}Hz\right|+1.0\right)\end{array}$$ |

# Parallelism and Performance

None.