E04HCF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

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

REAL (KIND=nag_wp)  X(N), F, G(N), W(LW)

3  Description

Routines for minimizing a function of several variables may require you to supply a subroutine to evaluate the objective function Fx1,x2,,xn 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 x=x1,x2,,xnT 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 F and its first derivatives gj= F xj , for j=1,2,,n at x. The components of the user-supplied derivatives along two orthogonal directions (defined by unit vectors p1 and p2, say) are then calculated; these will be gTp1 and gTp2 respectively. The same components are also estimated by finite differences, giving quantities
vk=Fx+hpk-Fxh,  k=1,2
where h is a small positive scalar. If the relative difference between v1 and gTp1 or between v2 and gTp2 is judged too large, an error indicator is set.

4  References


5  Parameters

1:     N – INTEGERInput
On entry: the number n of independent variables in the objective function.
Constraint: N1.
2:     FUNCT – 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 parameters of FUNCT to cause the minimization process to terminate immediately. E04HCF will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
The specification of FUNCT is:
REAL (KIND=nag_wp)  XC(N), FC, GC(N), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: will be set to 2.
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:     N – INTEGERInput
On entry: the number n of variables.
3:     XC(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point x at which F and its derivatives are required.
4:     FC – REAL (KIND=nag_wp)Output
On exit: unless FUNCT resets IFLAG, FC must be set to the value of the function F at the current point x.
5:     GC(N) – REAL (KIND=nag_wp) arrayOutput
On exit: unless FUNCT resets IFLAG, GCj must be set to the value of the first derivative F xj  at the point x, for j=1,2,,n.
6:     IW(LIW) – INTEGER arrayWorkspace
7:     LIW – INTEGERInput
8:     W(LW) – REAL (KIND=nag_wp) arrayWorkspace
9:     LW – INTEGERInput
These parameters 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 parameters IW, LIW, W, LW as these parameters. 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 3×N 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. Parameters denoted as Input must not be changed by this procedure.
3:     X(N) – REAL (KIND=nag_wp) arrayInput
On entry: Xj, for j=1,2,,n, must be set to the coordinates of a suitable point at which to check the derivatives calculated by FUNCT. ‘Obvious’ settings, such as 0.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 preferable that no two elements of X should be the same.
4:     F – 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 Fx at the point given by you in X.
5:     G(N) – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in the first call of FUNCT, Gj contains the value of the derivative F xj  at the point given in X, as calculated by FUNCT, for j=1,2,,n.
6:     IW(LIW) – INTEGER arrayCommunication Array
This array is in the parameter 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.
7:     LIW – INTEGERInput
On entry: the dimension of the array IW as declared in the (sub)program from which E04HCF is called.
Constraint: LIW1.
8:     W(LW) – REAL (KIND=nag_wp) arrayCommunication Array
9:     LW – INTEGERInput
On entry: the dimension of the array W as declared in the (sub)program from which E04HCF is called.
Constraint: LW3×N.
10:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, because for this routine the values of the output parameters may be useful even if IFAIL0 on exit, the recommended value is -1. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, 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,N<1,
You should check carefully the derivation and programming of expressions for the derivatives of Fx, because it is very unlikely that FUNCT is calculating them correctly.

7  Accuracy

IFAIL is set to 2 if
vk-gTpk 2h× gTpk 2+1
for k=1​ or ​2. (See Section 3 for definitions of the quantities involved.) The scalar h is set equal to ε, where ε is the machine precision as given by X02AJF.

8  Further Comments

FUNCT is called 3 times.
Before using E04HCF to check the calculation of first derivatives, you should be confident that FUNCT is calculating F correctly. The usual way of checking the calculation of the function is to compare values of Fx calculated by FUNCT at nontrivial points x with values calculated independently. (‘Non-trivial’ means that, as when setting x before calling E04HCF, coordinates such as 0.0 or 1.0 should be avoided.)
E04HCF only checks the derivatives calculated when IFLAG=2. So, if FUNCT is intended for use in conjunction with a minimization routine which may set IFLAG to 1, you must check that, for given settings of the XCj, FUNCT produces the same values for the GCj when IFLAG is set to 1 as when IFLAG is set to 2.

9  Example

Suppose that it is intended to use E04KDF to minimize
F= x1+10x2 2+5 x3-x4 2+ x2-2x3 4+10 x1-x4 4.
The following program could be used to check the first derivatives calculated by FUNCT. (The tests of whether IFLAG=0 or 1 in FUNCT are present ready for when FUNCT is called by E04KDF. E04HCF will always call FUNCT with IFLAG set to 2.)

9.1  Program Text

Program Text (e04hcfe.f90)

9.2  Program Data


9.3  Program Results

Program Results (e04hcfe.r)

E04HCF (PDF version)
E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012