Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_opt_check_deriv2 (e04hd)

## Purpose

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

## Syntax

[g, hesl, hesd, iw, w, ifail] = e04hd(funct, h, x, lh, iw, w, 'n', n)
[g, hesl, hesd, iw, w, ifail] = nag_opt_check_deriv2(funct, h, x, lh, iw, w, 'n', n)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 22: liw, lw have been removed from the interface
.

## Description

Routines for minimizing a function F(x1,x2,,xn)$F\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$ of the variables x1,x2,,xn${x}_{1},{x}_{2},\dots ,{x}_{n}$ may require you to provide a function to evaluate the second derivatives of F$F$. nag_opt_check_deriv2 (e04hd) is designed to check the second derivatives calculated by such user-supplied functions. As well as the function to be checked (h), you must supply a function (funct) to evaluate the first derivatives, and a point x = (x1,x2,,xn)T$x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the checks will be made. Note that nag_opt_check_deriv2 (e04hd) checks functions of the form required for nag_opt_bounds_mod_deriv2_comp (e04lb).
nag_opt_check_deriv2 (e04hd) first calls user-supplied functions funct and h to evaluate the first and second derivatives of F$F$ at x$x$. The user-supplied Hessian matrix (H$H$, say) is projected onto two orthogonal vectors y$y$ and z$z$ to give the scalars yTHy${y}^{\mathrm{T}}Hy$ and zTHz${z}^{\mathrm{T}}Hz$ respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
 p = (yTg(x + hy) − yTg(x)) / h and q = (zTg(x + hz) − zTg(x)) / h
$p=(yTg(x+hy)-yTg(x))/h and q=(zTg(x+hz)-zTg(x))/h$
respectively, where g()$g\left(\right)$ denotes the vector of first derivatives at the point in brackets and h$h$ is a small positive scalar. If the relative difference between p$p$ and yTHy${y}^{\mathrm{T}}Hy$ or between q$q$ and zTHz${z}^{\mathrm{T}}Hz$ is judged too large, an error indicator is set.

None.

## Parameters

### Compulsory Input Parameters

1:     funct – function handle or string containing name of m-file
funct must evaluate the function and its first derivatives at a given point. (nag_opt_bounds_mod_deriv2_comp (e04lb) gives you the option of resetting parameters of funct to cause the minimization process to terminate immediately. nag_opt_check_deriv2 (e04hd) will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
[iflag, fc, gc, iw, w] = funct(iflag, n, xc, iw, w)

Input Parameters

1:     iflag – int64int32nag_int scalar
To funct, iflag will be set to 2$2$.
2:     n – int64int32nag_int scalar
The number n$n$ of variables.
3:     xc(n) – double array
The point x$x$ at which the function and first derivatives are required.
4:     iw(liw) – int64int32nag_int array
5:     w(lw) – double array
These parameters are present so that funct will be of the form required by nag_opt_bounds_mod_deriv2_comp (e04lb). funct is called with nag_opt_check_deriv2 (e04hd)'s parameters iw, liw, w, lw as these parameters. If the advice given in nag_opt_bounds_mod_deriv2_comp (e04lb) is being followed, you will have no reason to examine or change any elements of iw or w. In any case, funct must not change the first 5 × n$5×{\mathbf{n}}$ elements of w.

Output Parameters

1:     iflag – int64int32nag_int scalar
If you set iflag to some negative number in funct and return control to nag_opt_check_deriv2 (e04hd), nag_opt_check_deriv2 (e04hd) will terminate immediately with ifail set to your setting of iflag.
2:     fc – double scalar
Unless funct resets iflag, fc must be set to the value of the objective function F$F$ at the current point x$x$.
3:     gc(n) – double array
Unless funct resets iflag, gc(j)${\mathbf{gc}}\left(\mathit{j}\right)$ must be set to the value of the first derivative (F)/(xj) $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point x$x$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
4:     iw(liw) – int64int32nag_int array
5:     w(lw) – double array
nag_opt_check_deriv (e04hc) should be used to check the first derivatives calculated by funct before nag_opt_check_deriv2 (e04hd) is used to check the second derivatives, since nag_opt_check_deriv2 (e04hd) assumes that the first derivatives are correct.
2:     h – function handle or string containing name of m-file
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.)
[iflag, fhesl, fhesd, iw, w] = h(iflag, n, xc, lh, fhesd, iw, w)

Input Parameters

1:     iflag – int64int32nag_int scalar
Is set to a non-negative number.
2:     n – int64int32nag_int scalar
The number n$n$ of variables.
3:     xc(n) – double array
The point x$x$ at which the second derivatives of F(x)$F\left(x\right)$ are required.
4:     lh – int64int32nag_int scalar
The length of the array fhesl.
5:     fhesd(n) – double array
Contains the value of (F)/(xj) $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point x$x$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$. Functions written to take advantage of a similar feature of nag_opt_bounds_mod_deriv2_comp (e04lb) can be tested as they stand by nag_opt_check_deriv2 (e04hd).
6:     iw(liw) – int64int32nag_int array
7:     w(lw) – double array
As in funct, these parameters correspond to the parameters iw, liw, w and lw of nag_opt_check_deriv2 (e04hd). h must not change the first 5 × n$5×{\mathbf{n}}$ elements of w.

Output Parameters

1:     iflag – int64int32nag_int scalar
If h resets iflag to a negative number, nag_opt_check_deriv2 (e04hd) will terminate immediately with ifail set to your setting of iflag.
2:     fhesl(lh) – double array
Unless iflag is reset, h must place the strict lower triangle of the second derivative matrix of F$F$ (evaluated at the point x$x$) in fhesl, stored by rows, i.e., fhesl((i1)(i2) / 2 + j)${\mathbf{fhesl}}\left(\left(\mathit{i}-1\right)\left(\mathit{i}-2\right)/2+\mathit{j}\right)$ must be set to the value of (2F)/(xixj) $\frac{{\partial }^{2}F}{\partial {x}_{\mathit{i}}\partial {x}_{\mathit{j}}}$ at the point x$x$, for i = 2,3,,n$\mathit{i}=2,3,\dots ,n$ and j = 1,2,,i1$\mathit{j}=1,2,\dots ,\mathit{i}-1$. (The upper triangle is not required because the matrix is symmetric.)
3:     fhesd(n) – double array
Unless iflag is reset, h must place the diagonal elements of the second derivative matrix of F$F$ (evaluated at the point x$x$) in fhesd, i.e., fhesd(j)${\mathbf{fhesd}}\left(\mathit{j}\right)$ must be set to the value of (2F)/(xj2) $\frac{{\partial }^{2}F}{\partial {x}_{\mathit{j}}^{2}}$ at the point x$x$, for j = 1,2,,n$\mathit{j}=1,2,\dots ,n$.
4:     iw(liw) – int64int32nag_int array
5:     w(lw) – double array
3:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n1${\mathbf{n}}\ge 1$.
x(j)${\mathbf{x}}\left(\mathit{j}\right)$, for j = 1,2,,n$\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 ​ or ​ 1.0$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.
4:     lh – int64int32nag_int scalar
The dimension of the array hesl as declared in the (sub)program from which nag_opt_check_deriv2 (e04hd) is called.
Constraint: lhmax (1,n × (n1) / 2)${\mathbf{lh}}\ge \mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$.
5:     iw(liw) – int64int32nag_int array
liw, the dimension of the array, must satisfy the constraint liw1$\mathit{liw}\ge 1$.
This array is in the parameter list so that it can be used by other library functions for passing integer quantities to funct or h. It is not examined or changed by nag_opt_check_deriv2 (e04hd). In general you must provide an array iw, but are advised not to use it.
6:     w(lw) – double array
lw, the dimension of the array, must satisfy the constraint lw5 × n$\mathit{lw}\ge 5×{\mathbf{n}}$.
Constraint: lw5 × n$\mathit{lw}\ge 5×{\mathbf{n}}$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
The number n$n$ of independent variables in the objective function.
Constraint: n1${\mathbf{n}}\ge 1$.

liw lw

### Output Parameters

1:     g(n) – double array
Unless you set iflag negative in the first call of funct, g(j)${\mathbf{g}}\left(\mathit{j}\right)$ contains the value of the first derivative (F)/(xj) $\frac{\partial F}{\partial {x}_{\mathit{j}}}$ at the point given in x, as calculated by funct, for j = 1,2,,n$\mathit{j}=1,2,\dots ,{\mathbf{n}}$.
2:     hesl(lh) – double array
Unless you set iflag negative in h, hesl contains the strict lower triangle of the second derivative matrix of F$F$, as evaluated by h at the point given in x, stored by rows.
3:     hesd(n) – double array
Unless you set iflag negative in h, hesd contains the diagonal elements of the second derivative matrix of F$F$, as evaluated by h at the point given in x.
4:     iw(liw) – int64int32nag_int array
5:     w(lw) – double array
lw5 × n$\mathit{lw}\ge 5×{\mathbf{n}}$.
Communication array, used to store information between calls to nag_opt_check_deriv2 (e04hd).
6:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Note: nag_opt_check_deriv2 (e04hd) may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W ifail < 0${\mathbf{ifail}}<0$
A negative value of ifail indicates an exit from nag_opt_check_deriv2 (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.
ifail = 1${\mathbf{ifail}}=1$
 On entry, n < 1${\mathbf{n}}<1$, or lh < max (1,n × (n − 1) / 2) ${\mathbf{lh}}<\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(1,{\mathbf{n}}×\left({\mathbf{n}}-1\right)/2\right)$, or liw < 1$\mathit{liw}<1$, or lw < 5 × n$\mathit{lw}<5×{\mathbf{n}}$.
W ifail = 2${\mathbf{ifail}}=2$
You should check carefully the derivation and programming of expressions for the second derivatives of F(x)$F\left(x\right)$, because it is very unlikely that h is calculating them correctly.

## Accuracy

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

nag_opt_check_deriv2 (e04hd) calls h once and funct three times.

## Example

```function nag_opt_check_deriv2_example
x = [1.46;
-0.82;
0.57;
1.21];
lh = int64(6);
iw = [int64(0)];
w =  zeros(20,1);
[g, hesl, hesd, iwOut, wOut, ifail] = nag_opt_check_deriv2(@funct, @hess, x, lh, iw, w)

function [iflag, fc, gc] = funct(iflag, n, xc)
gc = zeros(n, 1);
fc = (xc(1)+10*xc(2))^2 + 5*(xc(3)-xc(4))^2 + (xc(2)-2*xc(3))^4 + ...
10*(xc(1)-xc(4))^4;
gc(1) = 2*(xc(1)+10*xc(2)) + 40*(xc(1)-xc(4))^3;
gc(2) = 20*(xc(1)+10*xc(2)) + 4*(xc(2)-2*xc(3))^3;
gc(3) = 10*(xc(3)-xc(4)) - 8*(xc(2)-2*xc(3))^3;
gc(4) = 10*(xc(4)-xc(3)) - 40*(xc(1)-xc(4))^3;

function [iflag, fhesl, fhesd] = hess(iflag, n, xc, lh, fhesd)
fhesl = zeros(lh, 1);
fhesd = zeros(n, 1);

fhesd(1) = 2 + 120*(xc(1)-xc(4))^2;
fhesd(2) = 200 + 12*(xc(2)-2*xc(3))^2;
fhesd(3) = 10 + 48*(xc(2)-2*xc(3))^2;
fhesd(4) = 10 + 120*(xc(1)-xc(4))^2;

fhesl(1) = 20;
fhesl(2) = 0;
fhesl(3) = -24*(xc(2)-2*xc(3))^2;
fhesl(4) = -120*(xc(1)-xc(4))^2;
fhesl(5) = 0;
fhesl(6) = -10;
```
```

g =

-12.8550
-164.9181
53.8363
5.7750

hesl =

20.0000
0
-92.1984
-7.5000
0
-10.0000

hesd =

9.5000
246.0992
194.3968
17.5000

iwOut =

0

wOut =

-0.0170
-0.2808
-0.5983
0.1863
0.5726
0.3279
0.1075
0.8916
1.4600
-0.8200
0.5700
1.2100
5.4397
92.1407
-9.3317
10.2327
-12.8550
-164.9181
53.8363
5.7750

ifail =

0

```
```function e04hd_example
x = [1.46;
-0.82;
0.57;
1.21];
lh = int64(6);
iw = [int64(0)];
w =  zeros(20,1);
[g, hesl, hesd, iwOut, wOut, ifail] = e04hd(@funct, @hess, x, lh, iw, w)

function [iflag, fc, gc] = funct(iflag, n, xc)
gc = zeros(n, 1);
fc = (xc(1)+10*xc(2))^2 + 5*(xc(3)-xc(4))^2 + (xc(2)-2*xc(3))^4 + ...
10*(xc(1)-xc(4))^4;
gc(1) = 2*(xc(1)+10*xc(2)) + 40*(xc(1)-xc(4))^3;
gc(2) = 20*(xc(1)+10*xc(2)) + 4*(xc(2)-2*xc(3))^3;
gc(3) = 10*(xc(3)-xc(4)) - 8*(xc(2)-2*xc(3))^3;
gc(4) = 10*(xc(4)-xc(3)) - 40*(xc(1)-xc(4))^3;

function [iflag, fhesl, fhesd] = hess(iflag, n, xc, lh, fhesd)
fhesl = zeros(lh, 1);
fhesd = zeros(n, 1);

fhesd(1) = 2 + 120*(xc(1)-xc(4))^2;
fhesd(2) = 200 + 12*(xc(2)-2*xc(3))^2;
fhesd(3) = 10 + 48*(xc(2)-2*xc(3))^2;
fhesd(4) = 10 + 120*(xc(1)-xc(4))^2;

fhesl(1) = 20;
fhesl(2) = 0;
fhesl(3) = -24*(xc(2)-2*xc(3))^2;
fhesl(4) = -120*(xc(1)-xc(4))^2;
fhesl(5) = 0;
fhesl(6) = -10;
```
```

g =

-12.8550
-164.9181
53.8363
5.7750

hesl =

20.0000
0
-92.1984
-7.5000
0
-10.0000

hesd =

9.5000
246.0992
194.3968
17.5000

iwOut =

0

wOut =

-0.0170
-0.2808
-0.5983
0.1863
0.5726
0.3279
0.1075
0.8916
1.4600
-0.8200
0.5700
1.2100
5.4397
92.1407
-9.3317
10.2327
-12.8550
-164.9181
53.8363
5.7750

ifail =

0

```