NAG Library Routine Document
e04hcf (check_deriv)
1
Purpose
e04hcf checks that a subroutine for evaluating an objective function and its first derivatives produces derivative values which are consistent with the function values calculated.
2
Specification
Fortran Interface
Integer, Intent (In) | :: | n, liw, lw | Integer, Intent (Inout) | :: | iw(liw), ifail | Real (Kind=nag_wp), Intent (In) | :: | x(n) | Real (Kind=nag_wp), Intent (Inout) | :: | w(lw) | Real (Kind=nag_wp), Intent (Out) | :: | f, g(n) | External | :: | funct |
|
C Header Interface
#include <nagmk26.h>
void |
e04hcf_ (const Integer *n, void (NAG_CALL *funct)(Integer *iflag, const Integer *n, const double xc[], double *fc, double gc[], Integer iw[], const Integer *liw, double w[], const Integer *lw), const double x[], double *f, double g[], Integer iw[], const Integer *liw, double w[], const Integer *lw, Integer *ifail) |
|
3
Description
Routines for minimizing a function of several variables may require you to supply a subroutine to evaluate the objective function
and its first derivatives.
e04hcf is designed to check the derivatives calculated by such user-supplied subroutines . As well as the routine to be checked (
funct), you must supply a point
at which the check will be made. Note that
e04hcf checks routines of the form required for
e04kdf and
e04lbf.
e04hcf first calls
funct to evaluate
and its first derivatives
, for
at
. The components of the user-supplied derivatives along two orthogonal directions (defined by unit vectors
and
, say) are then calculated; these will be
and
respectively. The same components are also estimated by finite differences, giving quantities
where
is a small positive scalar. If the relative difference between
and
or between
and
is judged too large, an error indicator is set.
4
References
None.
5
Arguments
- 1: – IntegerInput
-
On entry: the number of independent variables in the objective function.
Constraint:
.
- 2: – Subroutine, supplied by the user.External Procedure
-
funct must evaluate the function and its first derivatives at a given point. (The minimization routines mentioned in
Section 3 gives you the option of resetting arguments of
funct to cause the minimization process to terminate immediately.
e04hcf will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
The specification of
funct is:
Fortran Interface
Integer, Intent (In) | :: | n, liw, lw | Integer, Intent (Inout) | :: | iflag, iw(liw) | Real (Kind=nag_wp), Intent (In) | :: | xc(n) | Real (Kind=nag_wp), Intent (Inout) | :: | w(lw) | Real (Kind=nag_wp), Intent (Out) | :: | fc, gc(n) |
|
C Header Interface
#include <nagmk26.h>
void |
funct (Integer *iflag, const Integer *n, const double xc[], double *fc, double gc[], Integer iw[], const Integer *liw, double w[], const Integer *lw) |
|
- 1: – IntegerInput/Output
-
On entry: will be set to .
On exit: if you reset
iflag to a negative number in
funct and return control to
e04hcf,
e04hcf will terminate immediately with
ifail set to your setting of
iflag.
- 2: – IntegerInput
-
On entry: the number of variables.
- 3: – Real (Kind=nag_wp) arrayInput
-
On entry: the point at which and its derivatives are required.
- 4: – Real (Kind=nag_wp)Output
-
On exit: unless
funct resets
iflag,
fc must be set to the value of the function
at the current point
.
- 5: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless
funct resets
iflag,
must be set to the value of the first derivative
at the point
, for
.
- 6: – Integer arrayWorkspace
- 7: – IntegerInput
- 8: – Real (Kind=nag_wp) arrayWorkspace
- 9: – IntegerInput
-
These arguments are present so that
funct will be of the form required by the minimization routines mentioned in
Section 3.
funct is called with
e04hcf's arguments
iw,
liw,
w,
lw as these arguments. If the advice given in the minimization routine documents 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.
funct must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which
e04hcf is called. Arguments denoted as
Input must
not be changed by this procedure.
Note: funct should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by
e04hcf. If your code inadvertently
does return any NaNs or infinities,
e04hcf is likely to produce unexpected results.
- 3: – Real (Kind=nag_wp) arrayInput
-
On entry:
, for
, must be set to 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 preferable that no two elements of
x should be the same.
- 4: – Real (Kind=nag_wp)Output
-
On exit: unless you set
iflag negative in the first call of
funct,
f contains the value of the objective function
at the point given by you in
x.
- 5: – Real (Kind=nag_wp) arrayOutput
-
On exit: unless you set
iflag negative in the first call of
funct,
contains the value of the derivative
at the point given in
x, as calculated by
funct, for
.
- 6: – Integer arrayWorkspace
- 7: – IntegerInput
-
This array is in the argument list so that it can be used by other library routines for passing integer quantities to
funct. It is not examined or changed by
e04hcf. Generally, you must provide an array
iw but are advised not to use it.
Constraint:
.
- 8: – Real (Kind=nag_wp) arrayWorkspace
- 9: – IntegerInput
-
On entry: the dimension of the array
w as declared in the (sub)program from which
e04hcf is called.
Constraint:
.
- 10: – IntegerInput/Output
-
On entry:
ifail must be set to
,
. If you are unfamiliar with this argument you should refer to
Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value
is recommended. If the output of error messages is undesirable, then the value
is recommended. Otherwise, because for this routine the values of the output arguments may be useful even if
on exit, the recommended value is
.
When the value is used it is essential to test the value of ifail on exit.
On exit:
unless the routine detects an error or a warning has been flagged (see
Section 6).
6
Error Indicators and Warnings
If on entry
or
, explanatory error messages are output on the current error message unit (as defined by
x04aaf).
Note: e04hcf may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
-
A negative value of
ifail indicates an exit from
e04hcf because you have set
iflag negative in
funct. The setting of
ifail will be the same as your setting of
iflag. The check on
funct will not have been completed.
-
On entry, | , |
or | , |
or | . |
-
You should check carefully the derivation and programming of expressions for the derivatives of
, because it is very unlikely that
funct is calculating them correctly.
An unexpected error has been triggered by this routine. Please
contact
NAG.
See
Section 3.9 in How to Use the NAG Library and its Documentation for further information.
Your licence key may have expired or may not have been installed correctly.
See
Section 3.8 in How to Use the NAG Library and its Documentation for further information.
Dynamic memory allocation failed.
See
Section 3.7 in How to Use the NAG Library and its Documentation for further information.
7
Accuracy
ifail is set to
if
for
. (See
Section 3 for definitions of the quantities involved.) The scalar
is set equal to
, where
is the
machine precision as given by
x02ajf.
8
Parallelism and Performance
e04hcf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the
X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the
Users' Note for your implementation for any additional implementation-specific information.
Before using
e04hcf to check the calculation of first derivatives, you should be confident that
funct is calculating
correctly. The usual way of checking the calculation of the function is to compare values of
calculated by
funct at nontrivial points
with values calculated independently. (‘Non-trivial’ means that, as when setting
before calling
e04hcf, coordinates such as
or
should be avoided.)
e04hcf only checks the derivatives calculated when
. So, if
funct is intended for use in conjunction with a minimization routine which may set
iflag to
, you must check that, for given settings of the
,
funct produces the same values for the
when
iflag is set to
as when
iflag is set to
.
10
Example
Suppose that it is intended to use
e04kdf to minimize
The following program could be used to check the first derivatives calculated by
funct. (The tests of whether
or
in
funct are present ready for when
funct is called by
e04kdf.
e04hcf will always call
funct with
iflag set to 2.)
10.1
Program Text
Program Text (e04hcfe.f90)
10.2
Program Data
None.
10.3
Program Results
Program Results (e04hcfe.r)