# NAG FL Interfacee04yaf (lsq_​check_​deriv)

## ▸▿ Contents

Settings help

FL Name Style:

FL Specification Language:

## 1Purpose

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.

## 2Specification

Fortran Interface
 Subroutine e04yaf ( m, n, x, fvec, fjac, iw, liw, w, lw,
 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
#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.

## 3Description

Routines for minimizing a sum of squares of $m$ nonlinear functions (or ‘residuals’), ${f}_{\mathit{i}}\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)$, for $\mathit{i}=1,2,\dots ,m$ and $m\ge n$, may require you to supply a subroutine to evaluate the ${f}_{i}$ 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={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{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 ${f}_{i}\left(x\right)$ and their first derivatives, and uses these to calculate the sum of squares $F\left(x\right)=\sum _{i=1}^{m}{\left[{f}_{i}\left(x\right)\right]}^{2}$, and its first derivatives ${g}_{j}={\frac{\partial F}{\partial {x}_{j}}|}_{x}$, for $j=1,2,\dots ,n$. The components of $g$ along two orthogonal directions (defined by unit vectors ${p}_{1}$ and ${p}_{2}$, say) are then calculated; these will be ${g}^{\mathrm{T}}{p}_{1}$ and ${g}^{\mathrm{T}}{p}_{2}$ respectively. The same components are also estimated by finite differences, giving quantities
 $vk=F(x+hpk)-F(x)h, k=1,2$
where $h$ is a small positive scalar. If the relative difference between ${v}_{1}$ and ${g}^{\mathrm{T}}{p}_{1}$ or between ${v}_{2}$ and ${g}^{\mathrm{T}}{p}_{2}$ is judged too large, an error indicator is set.

None.

## 5Arguments

1: $\mathbf{m}$Integer Input
2: $\mathbf{n}$Integer Input
On entry: the number $m$ of residuals, ${f}_{i}\left(x\right)$, and the number $n$ of variables, ${x}_{j}$.
Constraint: $1\le {\mathbf{n}}\le {\mathbf{m}}$.
3: $\mathbf{lsqfun}$Subroutine, supplied by the user. External Procedure
lsqfun must calculate the vector of values ${f}_{i}\left(x\right)$ and their first derivatives $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ 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 ( m, n, xc, fvec, fjac, 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)
 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: $\mathbf{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: $\mathbf{m}$Integer Input
On entry: the numbers $m$ of residuals.
3: $\mathbf{n}$Integer Input
On entry: the numbers $n$ of variables.
4: $\mathbf{xc}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: $x$, the point at which the values of the ${f}_{i}$ and the $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ are required.
5: $\mathbf{fvec}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: unless iflag is reset to a negative number, ${\mathbf{fvec}}\left(\mathit{i}\right)$ must contain the value of ${f}_{\mathit{i}}$ at the point $x$, for $\mathit{i}=1,2,\dots ,m$.
6: $\mathbf{fjac}\left({\mathbf{ldfjac}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: unless iflag is reset to a negative number, ${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the point $x$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$.
7: $\mathbf{ldfjac}$Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04yaf is called.
8: $\mathbf{iw}\left({\mathbf{liw}}\right)$Integer array Workspace
9: $\mathbf{liw}$Integer Input
10: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
11: $\mathbf{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×{\mathbf{n}}+{\mathbf{m}}+{\mathbf{m}}×{\mathbf{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: $\mathbf{x}\left({\mathbf{n}}\right)$Real (Kind=nag_wp) array Input
On entry: ${\mathbf{x}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,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: $\mathbf{fvec}\left({\mathbf{m}}\right)$Real (Kind=nag_wp) array Output
On exit: unless you set iflag negative in the first call of lsqfun, ${\mathbf{fvec}}\left(\mathit{i}\right)$ contains the value of ${f}_{\mathit{i}}$ at the point supplied by you in x, for $\mathit{i}=1,2,\dots ,m$.
6: $\mathbf{fjac}\left({\mathbf{ldfjac}},{\mathbf{n}}\right)$Real (Kind=nag_wp) array Output
On exit: unless you set iflag negative in the first call of lsqfun, ${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ contains the value of the first derivative $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the point given in x, as calculated by lsqfun, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$.
7: $\mathbf{ldfjac}$Integer Input
On entry: the first dimension of the array fjac as declared in the (sub)program from which e04yaf is called.
Constraint: ${\mathbf{ldfjac}}\ge {\mathbf{m}}$.
8: $\mathbf{iw}\left({\mathbf{liw}}\right)$Integer array Workspace
9: $\mathbf{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: ${\mathbf{liw}}\ge 1$.
10: $\mathbf{w}\left({\mathbf{lw}}\right)$Real (Kind=nag_wp) array Workspace
11: $\mathbf{lw}$Integer Input
On entry: the dimension of the array w as declared in the (sub)program from which e04yaf is called.
Constraint: ${\mathbf{lw}}\ge 3×{\mathbf{n}}+{\mathbf{m}}+{\mathbf{m}}×{\mathbf{n}}$.
12: $\mathbf{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 ${\mathbf{ifail}}\ne {\mathbf{0}}$ on exit. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{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.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{ldfjac}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{ldfjac}}\ge {\mathbf{m}}$.
On entry, ${\mathbf{liw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{liw}}\ge 1$.
On entry, ${\mathbf{lw}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{lw}}\ge 3×{\mathbf{n}}+{\mathbf{m}}+{\mathbf{m}}×{\mathbf{n}}$; that is, $⟨\mathit{\text{value}}⟩$.
On entry, ${\mathbf{m}}=⟨\mathit{\text{value}}⟩$ and ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{m}}\ge {\mathbf{n}}$.
On entry, ${\mathbf{n}}=⟨\mathit{\text{value}}⟩$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=2$
It is very likely that you have made an error in forming the derivatives in lsqfun.
${\mathbf{ifail}}<0$
User requested termination by setting iflag negative in lsqfun.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{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.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

ifail is set to $2$ if
 $(vk-gTpk) 2 ≥ h× ( (gTpk) 2 +1)$
for $k=1$ or $2$. (See Section 3 for definitions of the quantities involved.) The scalar $h$ is set equal to $\sqrt{\epsilon }$, where $\epsilon$ is the machine precision as given by x02ajf.

## 8Parallelism 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.

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 ${\mathbf{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 ${\mathbf{xc}}\left(j\right)$, lsqfun produces the same values for the $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ when iflag is set to $1$ as when iflag is set to $2$.

## 10Example

Suppose that it is intended to use e04gbf or e04gdf to find least squares estimates of ${x}_{1},{x}_{2}$ and ${x}_{3}$ 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 ${\mathbf{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.1Program Text

Program Text (e04yafe.f90)

### 10.2Program Data

Program Data (e04yafe.d)

### 10.3Program Results

Program Results (e04yafe.r)