NAG FL Interface
e04yaf (lsq_​check_​deriv)

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

Fortran Interface
Subroutine e04yaf ( m, n, lsqfun, x, fvec, fjac, ldfjac, iw, liw, w, lw, ifail)
Integer, Intent (In) :: m, n, ldfjac, liw, lw
Integer, Intent (Inout) :: iw(liw), ifail
Real (Kind=nag_wp), Intent (In) :: x(n)
Real (Kind=nag_wp), Intent (Inout) :: fjac(ldfjac,n), w(lw)
Real (Kind=nag_wp), Intent (Out) :: fvec(m)
External :: lsqfun
C Header Interface
#include <nag.h>
void  e04yaf_ (const Integer *m, const Integer *n,
void (NAG_CALL *lsqfun)(Integer *iflag, const Integer *m, const Integer *n, const double xc[], double fvec[], double fjac[], const Integer *ldfjac, Integer iw[], const Integer *liw, double w[], const Integer *lw),
const double x[], double fvec[], double fjac[], const Integer *ldfjac, Integer iw[], const Integer *liw, double w[], const Integer *lw, Integer *ifail)
The routine may be called by the names e04yaf or nagf_opt_lsq_check_deriv.

3 Description

Routines for minimizing a sum of squares of m nonlinear functions (or ‘residuals’), fix1,x2,,xn, for i=1,2,,m and mn, 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,,xn T 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=1 m fix2, and its first derivatives gj = F x j x , 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 Arguments

1: m Integer Input
2: n Integer Input
On entry: the number m of residuals, fix, and the number n of variables, xj.
Constraint: 1nm.
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 an argument to terminate immediately. e04yaf will also terminate immediately, without finishing the checking process, if the argument in question is reset.)
The specification of lsqfun is:
Fortran Interface
Subroutine lsqfun ( iflag, m, n, xc, fvec, fjac, ldfjac, iw, liw, w, lw)
Integer, Intent (In) :: m, n, ldfjac, liw, lw
Integer, Intent (Inout) :: iflag, iw(liw)
Real (Kind=nag_wp), Intent (In) :: xc(n)
Real (Kind=nag_wp), Intent (Inout) :: fjac(ldfjac,n), w(lw)
Real (Kind=nag_wp), Intent (Out) :: fvec(m)
C Header Interface
void  lsqfun_ (Integer *iflag, const Integer *m, const Integer *n, const double xc[], double fvec[], double fjac[], const Integer *ldfjac, Integer iw[], const Integer *liw, double w[], const Integer *lw)
1: iflag Integer Input/Output
On entry: to lsqfun, iflag will be set to 2.
On exit: if you reset iflag to some negative number in lsqfun and return control to e04yaf, the routine will terminate immediately with ifail set to your setting of iflag.
2: m Integer Input
On entry: the numbers m of residuals.
3: n Integer Input
On entry: the numbers n of variables.
4: xcn Real (Kind=nag_wp) array Input
On entry: x, the point at which the values of the fi and the fi xj are required.
5: fvecm Real (Kind=nag_wp) array Output
On exit: unless iflag is reset to a negative number, fveci must contain the value of fi at the point x, for i=1,2,,m.
6: fjacldfjacn Real (Kind=nag_wp) array Output
On exit: unless iflag is reset to a negative number, fjacij must contain the value of fi xj at the point x, for i=1,2,,m and j=1,2,,n.
7: ldfjac Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04yaf is called.
8: iwliw Integer array Workspace
9: liw Integer Input
10: wlw Real (Kind=nag_wp) array Workspace
11: lw Integer Input
These arguments 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 arguments 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, lsqfun must 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. Arguments denoted as Input must not be changed by this procedure.
Note: lsqfun should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by e04yaf. If your code inadvertently does return any NaNs or infinities, e04yaf is likely to produce unexpected results.
4: xn Real (Kind=nag_wp) array Input
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.
5: fvecm Real (Kind=nag_wp) array Output
On exit: unless you set iflag negative in the first call of lsqfun, fveci contains the value of fi at the point supplied by you in x, for i=1,2,,m.
6: fjacldfjacn Real (Kind=nag_wp) array Output
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 Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04yaf is called.
Constraint: ldfjacm.
8: iwliw Integer array Workspace
9: liw Integer Input
This array appears in the argument 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.
On entry: the dimension of the array iw as declared in the (sub)program from which e04yaf is called.
Constraint: liw1.
10: wlw Real (Kind=nag_wp) array Workspace
11: lw Integer Input
On entry: the dimension of the array w as declared in the (sub)program from which e04yaf is called.
Constraint: lw3×n+m+m×n.
12: ifail Integer Input/Output
On entry: ifail must be set to 0, -1 or 1 to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of 0 causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of -1 means that an error message is printed while a value of 1 means that it is not.
If halting is not appropriate, the value -1 or 1 is recommended. If message printing is undesirable, then the value 1 is recommended. Otherwise, the value -1 is recommended since useful values can be provided in some output arguments even when ifail0 on exit. 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).
Errors or warnings detected by the routine:
Note: in some cases e04yaf may return useful information.
ifail=1
On entry, ldfjac=value and m=value.
Constraint: ldfjacm.
On entry, liw=value.
Constraint: liw1.
On entry, lw=value.
Constraint: lw3×n+m+m×n; that is, value.
On entry, m=value and n=value.
Constraint: mn.
On entry, n=value.
Constraint: n1.
ifail=2
It is very likely that you have made an error in forming the derivatives in lsqfun.
ifail<0
User requested termination by setting iflag negative in lsqfun.
ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
ifail=-399
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
ifail=-999
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

7 Accuracy

ifail is set to 2 if
vk - gT pk 2 h× gT pk 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 Parallelism and Performance

e04yaf 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.

9 Further Comments

e04yaf calls lsqfun three times.
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.

10 Example

Suppose that it is intended to use e04gbf or e04gdf to find least squares estimates of x1,x2 and x3 in the model
y=x1+t1x2t2+x3t3  
using the 15 sets of data given in the following table.
y t1 t2 t3 0.14 1.0 15.0 1.0 0.18 2.0 14.0 2.0 0.22 3.0 13.0 3.0 0.25 4.0 12.0 4.0 0.29 5.0 11.0 5.0 0.32 6.0 10.0 6.0 0.35 7.0 9.0 7.0 0.39 8.0 8.0 8.0 0.37 9.0 7.0 7.0 0.58 10.0 6.0 6.0 0.73 11.0 5.0 5.0 0.96 12.0 4.0 4.0 1.34 13.0 3.0 3.0 2.10 14.0 2.0 2.0 4.39 15.0 1.0 1.0  
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.)

10.1 Program Text

Program Text (e04yafe.f90)

10.2 Program Data

Program Data (e04yafe.d)

10.3 Program Results

Program Results (e04yafe.r)