+− Contents

1 Purpose

E04YAF checks that a user-supplied subroutine for evaluating a vector of functions and the matrix of their first derivatives produces derivative values which are consistent with the function values calculated.

2 Specification

3 Description

Routines for minimizing a sum of squares of m nonlinear functions (or ‘residuals’), fix1,x2,…,xn, for i=1,2,…,m and m≥n, may require you to supply a subroutine to evaluate the fi and their first derivatives. E04YAF checks the derivatives calculated by such user-supplied subroutines, e.g., routines of the form required for E04GBF, E04GDF and E04HEF. As well as the routine to be checked (LSQFUN), you must supply a point x=x1,x2,…,xnT at which the check will be made. E04YAF is essentially identical to CHKLSJ in the NPL Algorithms Library.

E04YAF first calls LSQFUN to evaluate the fix and their first derivatives, and uses these to calculate the sum of squares Fx=∑i=1mfix2,
and its first derivatives gj=∂F∂xjx, for j=1,2,…,n. The components of g 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

None.

5 Parameters

1: M – INTEGERInput

2: N – INTEGERInput

On entry: the number m of residuals, fix, and the number n of variables, xj.

Constraint:
1≤N≤M.

3: LSQFUN – SUBROUTINE, supplied by the user.External Procedure

LSQFUN must calculate the vector of values fix and their first derivatives ∂fi∂xj at any point x. (The minimization routines mentioned in Section 3 give you the option of resetting a parameter to terminate immediately. E04YAF will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)

These parameters are present so that LSQFUN will be of the form required by the minimization routines mentioned in Section 3. LSQFUN is called with the same parameters IW, LIW, W, LW as in the call to E04YAF. If the recommendation in the minimization routine document is followed, you will have no reason to examine or change the elements of IW or W. In any case, LSQFUNmust not change the first 3×N+M+M×N elements of W.

LSQFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04YAF is called. Parameters denoted as Input must not be changed by this procedure.

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 LSQFUN. ‘Obvious’ settings, such as 0 or 1, should not be used since, at such particular points, incorrect terms may take correct values (particularly zero), so that errors can go undetected. For a similar reason, it is preferable that no two elements of X should have the same value.

On exit: unless you set IFLAG negative in the first call of LSQFUN,
FJACij contains the value of the first derivative ∂fi∂xj at the point given in X, as calculated by LSQFUN, for i=1,2,…,m and j=1,2,…,n.

7: LDFJAC – INTEGERInput

On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04YAF is called.

This array appears in the parameter list purely so that, if E04YAF is called by another library routine, the library routine can pass quantities to LSQFUN via IW. IW is not examined or changed by E04YAF. In general you must provide an array IW, but are advised not to use it.

9: LIW – INTEGERInput

On entry: the dimension of the array IW as declared in the (sub)program from which E04YAF is called.

On entry: the dimension of the array W as declared in the (sub)program from which E04YAF is called.

Constraint:
LW≥3×N+M+M×N.

12: 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 IFAIL≠0 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: E04YAF may return useful information for one or more of the following detected errors or warnings.

Errors or warnings detected by the routine:

IFAIL<0

A negative value of IFAIL indicates an exit from E04YAF because you have set IFLAG negative in LSQFUN. The setting of IFAIL will be the same as your setting of IFLAG. The check on LSQFUN will not have been completed.

IFAIL=1

On entry,

M<N,

or

N<1,

or

LDFJAC<M,

or

LIW<1,

or

LW<3×N+M+M×N.

IFAIL=2

You should check carefully the derivation and programming of expressions for the ∂fi∂xj, because it is very unlikely that LSQFUN is calculating them correctly.

8 Further Comments

Before using E04YAF to check the calculation of the first derivatives, you should be confident that LSQFUN is calculating the residuals correctly.

E04YAF only checks the derivatives calculated by a user-supplied routine when IFLAG=2. So, if LSQFUN 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, LSQFUN produces the same values for the ∂fi∂xj when IFLAG is set to 1 as when IFLAG is set to 2.

9 Example

Suppose that it is intended to use E04GBF or E04GDF to find least squares estimates of x1,x2 and x3 in the model

The following program could be used to check the first derivatives calculated by LSQFUN. (The tests of whether IFLAG=0 or 1 in LSQFUN are present ready for when LSQFUN is called by E04GBF or E04GDF. E04YAF will always call LSQFUN with IFLAG set to 2.)