PDF version (NAG web site
, 64-bit version, 64-bit version)
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:
At Mark 22: |
liw and lw were removed from the interface |
Description
Routines for minimizing a function
of the variables
may require you to provide a function to evaluate the second derivatives of
.
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
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
at
. The user-supplied Hessian matrix (
, say) is projected onto two orthogonal vectors
and
to give the scalars
and
respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
respectively, where
denotes the vector of first derivatives at the point in brackets and
is a small positive scalar. If the relative difference between
and
or between
and
is judged too large, an error indicator is set.
References
None.
Parameters
Compulsory Input Parameters
- 1:
– 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 arguments 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 argument in question is reset.)
[iflag, fc, gc, iw, w] = funct(iflag, n, xc, iw, w)
Input Parameters
- 1:
– int64int32nag_int scalar
-
To
funct,
iflag will be set to
.
- 2:
– int64int32nag_int scalar
-
The number of variables.
- 3:
– double array
-
The point at which the function and first derivatives are required.
- 4:
– int64int32nag_int array
- 5:
– double array
-
These arguments 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 arguments
iw,
liw,
w,
lw as these arguments. 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
elements of
w.
Output Parameters
- 1:
– 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:
– double scalar
-
Unless
funct resets
iflag,
fc must be set to the value of the objective function
at the current point
.
- 3:
– double array
-
Unless
funct resets
iflag,
must be set to the value of the first derivative
at the point
, for
.
- 4:
– int64int32nag_int array
- 5:
– 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:
– 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 argument can be set to cause immediate termination.)
[iflag, fhesl, fhesd, iw, w] = h(iflag, n, xc, lh, fhesd, iw, w)
Input Parameters
- 1:
– int64int32nag_int scalar
-
Is set to a non-negative number.
- 2:
– int64int32nag_int scalar
-
The number of variables.
- 3:
– double array
-
The point at which the second derivatives of are required.
- 4:
– int64int32nag_int scalar
-
The length of the array
fhesl.
- 5:
– double array
-
Contains the value of
at the point
, for
. 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:
– int64int32nag_int array
- 7:
– double array
-
As in
funct, these arguments correspond to the arguments
iw,
liw,
w and
lw of
nag_opt_check_deriv2 (e04hd).
h must not change the first
elements of
w.
Output Parameters
- 1:
– 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:
– double array
-
Unless
iflag is reset,
h must place the strict lower triangle of the second derivative matrix of
(evaluated at the point
) in
fhesl, stored by rows, i.e.,
must be set to the value of
at the point
, for
and
. (The upper triangle is not required because the matrix is symmetric.)
- 3:
– double array
-
Unless
iflag is reset,
h must place the diagonal elements of the second derivative matrix of
(evaluated at the point
) in
fhesd, i.e.,
must be set to the value of
at the point
, for
.
- 4:
– int64int32nag_int array
- 5:
– double array
-
- 3:
– double array
-
, for
must contain the coordinates of a suitable point at which to check the derivatives calculated by
funct. ‘Obvious’ settings, such as
, 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:
– int64int32nag_int scalar
-
The dimension of the array
hesl.
Constraint:
.
- 5:
– int64int32nag_int array
-
liw, the dimension of the array, must satisfy the constraint
.
This array is in the argument 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:
– double array
lw, the dimension of the array, must satisfy the constraint
.
Constraint:
.
Optional Input Parameters
- 1:
– int64int32nag_int scalar
-
Default:
the dimension of the array
x.
The number of independent variables in the objective function.
Constraint:
.
Output Parameters
- 1:
– double array
-
Unless you set
iflag negative in the first call of
funct,
contains the value of the first derivative
at the point given in
x, as calculated by
funct, for
.
- 2:
– double array
-
Unless you set
iflag negative in
h,
hesl contains the strict lower triangle of the second derivative matrix of
, as evaluated by
h at the point given in
x, stored by rows.
- 3:
– double array
-
Unless you set
iflag negative in
h,
hesd contains the diagonal elements of the second derivative matrix of
, as evaluated by
h at the point given in
x.
- 4:
– int64int32nag_int array
-
- 5:
– double array
.
Communication array, used to store information between calls to nag_opt_check_deriv2 (e04hd).
- 6:
– int64int32nag_int scalar
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
-
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.
-
-
On entry, | , |
or | , |
or | , |
or | . |
- W
-
You should check carefully the derivation and programming of expressions for the second derivatives of
, because it is very unlikely that
h is calculating them correctly.
-
An unexpected error has been triggered by this routine. Please
contact
NAG.
-
Your licence key may have expired or may not have been installed correctly.
-
Dynamic memory allocation failed.
Accuracy
ifail is set to
if
where
is set equal to
(
being the
machine precision as given by
nag_machine_precision (x02aj)) and other quantities are as defined in
Description.
Further Comments
nag_opt_check_deriv2 (e04hd) calls
h once and
funct three times.
Example
Suppose that it is intended to use
nag_opt_bounds_mod_deriv2_comp (e04lb) to minimize
The following program could be used to check the second derivatives calculated by
h required. (The call of
nag_opt_check_deriv2 (e04hd) is preceded by a call of
nag_opt_check_deriv (e04hc) to check
funct which calculates the first derivatives.)
Open in the MATLAB editor:
e04hd_example
function e04hd_example
fprintf('e04hd example results\n\n');
x = [1.46 -0.82 0.57 1.21];
n = int64(4);
lh = int64(6);
iw = [int64(0)];
w = zeros(20,1);
[g, hesl, hesd, iw, w, ifail] = ...
e04hd( ...
@funct, @hess, x, lh, iw, w);
[iflag, f, gc] = funct(n, n, x);
fprintf('The test point is: %8.2f%8.2f%8.2f%8.2f\n',x);
fprintf('\n2nd derivatives are consistent with 1st derivatives\n');
fprintf('\nAt test point, f = %7.3f\n',f)
fprintf(' 1st derivatives = %7.3f %7.3f %7.3f %7.3f\n',g)
fprintf('\nH gives the lower triangle of the Hessian matrix\n');
fprintf('%12.3e\n', hesd(1));
k = 1;
for i = 2:n
fprintf('%12.3e', hesl(k:(k+i-2)), hesd(i));
fprintf('\n');
k = k + i - 1;
end
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;
e04hd example results
The test point is: 1.46 -0.82 0.57 1.21
2nd derivatives are consistent with 1st derivatives
At test point, f = 62.273
1st derivatives = -12.855 -164.918 53.836 5.775
H gives the lower triangle of the Hessian matrix
9.500e+00
2.000e+01 2.461e+02
0.000e+00 -9.220e+01 1.944e+02
-7.500e+00 0.000e+00 -1.000e+01 1.750e+01
PDF version (NAG web site
, 64-bit version, 64-bit version)
© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015