The function may be called by the names: e04hcc or nag_opt_check_deriv.
The function e04kbc for minimizing a function of several variables requires you to supply a C function to evaluate the objective function and its first derivatives. e04hcc is designed to check the derivatives calculated by such a user-supplied function. As well as the function to be checked (objfun), you must supply a point at which the check is to be made.
e04hcc first calls the supplied function objfun 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.
1: – IntegerInput
On entry: the number of independent variables in the objective function.
2: – function, supplied by the userExternal Function
objfun must evaluate the objective function and its first derivatives at a given point. (The minimization function e04kbc gives you the option of resetting an argument, , to terminate the minimization process immediately. e04hcc will also terminate immediately, without finishing the checking process, if the argument in question is reset to a negative value.)
On entry: the point at which and its derivatives are required.
3: – double *Output
On exit: objfun must set objf to the value of the objective function at the current point . If it is not possible to evaluate then objfun should assign a negative value to ; e04hcc will then terminate.
4: – doubleOutput
On exit: unless is reset to a negative number, objfun must set to the value of the first derivative at the current point for
5: – Nag_Comm *
Pointer to structure of type Nag_Comm; the following members are relevant to objfun.
flag – IntegerInput/Output
On entry: will be set to 2.
On exit: if objfun resets to some negative number then e04hcc will terminate immediately with the error indicator NE_USER_STOP. If fail is supplied to e04hcc, will be set to your setting of .
first – Nag_BooleanInput
On entry: will be set to Nag_TRUE on the first call to objfun and Nag_FALSE for all subsequent calls.
nf – IntegerInput
On entry: the number of calculations of the objective function; this value will be equal to the number of calls made to objfun including the current one.
user – double *
iuser – Integer *
p – Pointer
The type Pointer will be void * with a C compiler that defines void * and char * otherwise. Before calling e04hcc these pointers may be allocated memory and initialized with various quantities for use by objfun when called from e04hcc.
Note:objfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04hcc. If your code inadvertently does return any NaNs or infinities, e04hcc is likely to produce unexpected results.
On entry: , for , must be set to the coordinates of a suitable point at which to check the derivatives calculated by objfun. ‘Obvious’ settings, such as or , 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: – double *Output
On exit: unless you set negative in the first call of objfun, objf contains the value of the objective function at the point given in x.
5: – doubleOutput
On exit: unless you set negative in the first call of objfun, contains the value of the derivative at the point given in x, as calculated by objfun, for .
6: – Nag_Comm *Input/Output
Note:comm is a NAG defined type (see Section 3.1.1 in the Introduction to the NAG Library CL Interface).
On entry/exit: structure containing pointers for communication with the user-defined function; see the above description of objfun for details. If you do not need to make use of this communication feature the null pointer NAGCOMM_NULL may be used in the call to e04hcc; comm will then be declared internally for use in calls to objfun.
7: – NagError *Input/Output
The NAG error argument (see Section 7 in the Introduction to the NAG Library CL Interface).
6Error Indicators and Warnings
Dynamic memory allocation failed.
Large errors were found in the derivatives of the objective function.
You should check carefully the derivation and programming of expressions for the derivatives of , because it is very unlikely that objfun is calculating them correctly.
On entry, .
User requested termination, user flag value .
This exit occurs if you set to a negative value in objfun. If fail is supplied the value of will be the same as your setting of . The check on objfun will not have been completed.
for or . (See Section 3 for definitions of the quantities involved.) The scalar is set equal to , where is the machine precision as given by X02AJC.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
e04hcc is not threaded in any implementation.
The user-defined function objfun is called three times.
Before using e04hcc to check the calculation of first derivatives, you should be confident that objfun is calculating correctly. The usual way of checking the calculation of the function is to compare values of calculated by objfun at non-trivial points with values calculated independently. (‘Non-trivial’ means that, as when setting before calling e04hcc, coordinates such as or should be avoided.)
Suppose that it is intended to use e04kbc to minimize
The following program could be used to check the first derivatives calculated by the required function objfun. (The test of whether in objfun is present for when objfun is called by e04kbc. e04hcc will always call objfun with set to 2.)