hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
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(x1,x2,,xn) of the variables x1,x2,,xnx1,x2,,xn may require you to provide a function to evaluate the second derivatives of FF. 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)Tx=(x1,x2,,xn)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 FF at xx. The user-supplied Hessian matrix (HH, say) is projected onto two orthogonal vectors yy and zz to give the scalars yTHyyTHy and zTHzzTHz 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() denotes the vector of first derivatives at the point in brackets and hh is a small positive scalar. If the relative difference between pp and yTHyyTHy or between qq and zTHzzTHz is judged too large, an error indicator is set.

References

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 22.
2:     n – int64int32nag_int scalar
The number nn of variables.
3:     xc(n) – double array
The point xx 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 × n5×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 FF at the current point xx.
3:     gc(n) – double array
Unless funct resets iflag, gc(j)gcj must be set to the value of the first derivative (F)/(xj) F xj at the point xx, for j = 1,2,,nj=1,2,,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 nn of variables.
3:     xc(n) – double array
The point xx at which the second derivatives of F(x)F(x) are required.
4:     lh – int64int32nag_int scalar
The length of the array fhesl.
5:     fhesd(n) – double array
Contains the value of (F)/(xj) F xj  at the point xx, for j = 1,2,,nj=1,2,,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 × n5×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 FF (evaluated at the point xx) in fhesl, stored by rows, i.e., fhesl((i1)(i2) / 2 + j)fhesl(i-1)(i-2)/2+j must be set to the value of (2F)/(xixj) 2F xixj at the point xx, for i = 2,3,,ni=2,3,,n and j = 1,2,,i1j=1,2,,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 FF (evaluated at the point xx) in fhesd, i.e., fhesd(j)fhesdj must be set to the value of (2F)/(xj2) 2F xj2 at the point xx, for j = 1,2,,nj=1,2,,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 n1n1.
x(j)xj, for j = 1,2,,nj=1,2,,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.00.0 ​ 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)lhmax(1,n×(n-1)/2).
5:     iw(liw) – int64int32nag_int array
liw, the dimension of the array, must satisfy the constraint liw1liw1.
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 × nlw5×n.
Constraint: lw5 × nlw5×n.

Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
The number nn of independent variables in the objective function.
Constraint: n1n1.

Input Parameters Omitted from the MATLAB Interface

liw lw

Output Parameters

1:     g(n) – double array
Unless you set iflag negative in the first call of funct, g(j)gj contains the value of the first derivative (F)/(xj) F xj at the point given in x, as calculated by funct, for j = 1,2,,nj=1,2,,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 FF, 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 FF, as evaluated by h at the point given in x.
4:     iw(liw) – int64int32nag_int array
5:     w(lw) – double array
lw5 × nlw5×n.
Communication array, used to store information between calls to nag_opt_check_deriv2 (e04hd).
6:     ifail – int64int32nag_int scalar
ifail = 0ifail=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 < 0ifail<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 = 1ifail=1
On entry,n < 1n<1,
or lh < max (1,n × (n1) / 2) lh < max(1,n×(n-1)/2) ,
orliw < 1liw<1,
orlw < 5 × nlw<5×n.
W ifail = 2ifail=2
You should check carefully the derivation and programming of expressions for the second derivatives of F(x)F(x), because it is very unlikely that h is calculating them correctly.

Accuracy

ifail is set to 22 if
|yTHyp|sqrt(h) × (|yTHy| + 1.0)  or ​
|zTHzq|sqrt(h) × (|zTHz| + 1.0)
|yTHy-p|h×(|yTHy|+1.0)  or ​ |zTHz-q|h×(|zTHz|+1.0)
where hh is set equal to sqrt(ε)ε (εε being the machine precision as given by nag_machine_precision (x02aj)) and other quantities are as defined in Section [Description].

Further Comments

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



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013