E04 Chapter Contents
E04 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentE04YBF

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.

## 1  Purpose

E04YBF checks that a user-supplied subroutine for evaluating the second derivative term of the Hessian matrix of a sum of squares is consistent with a user-supplied subroutine for calculating the corresponding first derivatives.

## 2  Specification

 SUBROUTINE E04YBF ( M, N, LSQFUN, LSQHES, X, FVEC, FJAC, LDFJAC, B, LB, IW, LIW, W, LW, IFAIL)
 INTEGER M, N, LDFJAC, LB, IW(LIW), LIW, LW, IFAIL REAL (KIND=nag_wp) X(N), FVEC(M), FJAC(LDFJAC,N), B(LB), W(LW) EXTERNAL LSQFUN, LSQHES

## 3  Description

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 quantities
 $bjk=∑i=1mfi ∂2fi ∂xj∂xk$
for $j=1,2,\dots ,n$ and $k=1,2,\dots ,j$. E04YBF is designed to check the ${b}_{jk}$ calculated by such user-supplied subroutines. As well as the routine to be checked (LSQHES), you must supply a subroutine (LSQFUN) to evaluate the ${f}_{i}$ and their first derivatives, and a point $x={\left({x}_{1},{x}_{2},\dots ,{x}_{n}\right)}^{\mathrm{T}}$ at which the checks will be made. Note that E04YBF checks routines of the form required by E04HEF. E04YBF is essentially identical to CHKLSH in the NPL Algorithms Library.
E04YBF first calls user-supplied subroutines LSQFUN and LSQHES to evaluate the first derivatives and the ${b}_{jk}$ at $x$. Let $J$ denote the $m$ by $n$ matrix of first derivatives of the residuals. The Hessian matrix of the sum of squares,
 $G=JTJ+B,$
is calculated and projected onto two orthogonal vectors $y$ and $z$ to give the scalars ${y}^{\mathrm{T}}Gy$ and ${z}^{\mathrm{T}}Gz$ respectively. The same projections of the Hessian matrix are also estimated by finite differences, giving
 $p=yTgx+hy-yTgx/h and q=zTgx+hz-zTgx/h$
respectively, where $g\left(\right)$ denotes the gradient vector of the sum of squares at the point in brackets and $h$ is a small positive scalar. If the relative difference between $p$ and ${y}^{\mathrm{T}}Gy$ or between $q$ and ${z}^{\mathrm{T}}Gz$ is judged too large, an error indicator is set.

None.

## 5  Parameters

1:     M – INTEGERInput
2:     N – INTEGERInput
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:     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$. (E04HEF gives you the option of resetting parameters of LSQFUN to cause the minimization process to terminate immediately. E04YBF will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
The specification of LSQFUN is:
 SUBROUTINE LSQFUN ( IFLAG, M, N, XC, FVEC, FJAC, LDFJAC, IW, LIW, W, LW)
 INTEGER IFLAG, M, N, LDFJAC, IW(LIW), LIW, LW REAL (KIND=nag_wp) XC(N), FVEC(M), FJAC(LDFJAC,N), W(LW)
1:     IFLAG – INTEGERInput/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 E04YBF, the routine will terminate immediately with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: the numbers $m$ of residuals.
3:     N – INTEGERInput
On entry: the numbers $n$ of variables.
4:     XC(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point $x$ at which the values of the ${f}_{i}$ and the $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ are required.
5:     FVEC(M) – REAL (KIND=nag_wp) arrayOutput
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:     FJAC(LDFJAC,N) – REAL (KIND=nag_wp) arrayOutput
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:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04YBF is called.
8:     IW(LIW) – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
These parameters are present so that LSQFUN will be of the form required by E04HEF. LSQFUN is called with E04YBF's parameters IW, LIW, W, LW as these parameters. If the recommendation in E04HEF 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 $5×{\mathbf{N}}+{\mathbf{M}}+{\mathbf{M}}×{\mathbf{N}}+{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2$ (or $6+2×{\mathbf{M}}$ if ${\mathbf{N}}=1$) elements of W.
LSQFUN must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04YBF is called. Parameters denoted as Input must not be changed by this procedure.
Note:  E04YAF should be used to check the first derivatives calculated by LSQFUN before E04YBF is used to check the ${b}_{jk}$ since E04YBF assumes that the first derivatives are correct.
4:     LSQHES – SUBROUTINE, supplied by the user.External Procedure
LSQHES must calculate the elements of the symmetric matrix
 $Bx=∑i=1mfixGix,$
at any point $x$, where ${G}_{i}\left(x\right)$ is the Hessian matrix of ${f}_{i}\left(x\right)$. (As with LSQFUN, a parameter can be set to cause immediate termination.)
The specification of LSQHES is:
 SUBROUTINE LSQHES ( IFLAG, M, N, FVEC, XC, B, LB, IW, LIW, W, LW)
 INTEGER IFLAG, M, N, LB, IW(LIW), LIW, LW REAL (KIND=nag_wp) FVEC(M), XC(N), B(LB), W(LW)
1:     IFLAG – INTEGERInput/Output
On entry: is set to a non-negative number.
On exit: if LSQHES resets IFLAG to some negative number, E04YBF will terminate immediately, with IFAIL set to your setting of IFLAG.
2:     M – INTEGERInput
On entry: the numbers $m$ of residuals.
3:     N – INTEGERInput
On entry: the numbers $n$ of variables.
4:     FVEC(M) – REAL (KIND=nag_wp) arrayInput
On entry: the value of the residual ${f}_{\mathit{i}}$ at the point $x$, for $\mathit{i}=1,2,\dots ,m$, so that the values of the ${f}_{\mathit{i}}$ can be used in the calculation of the elements of B.
5:     XC(N) – REAL (KIND=nag_wp) arrayInput
On entry: the point $x$ at which the elements of B are to be evaluated.
6:     B(LB) – REAL (KIND=nag_wp) arrayOutput
On exit: unless IFLAG is reset to a negative number B must contain the lower triangle of the matrix $B\left(x\right)$, evaluated at the point in XC, stored by rows. (The upper triangle is not needed because the matrix is symmetric.) More precisely, ${\mathbf{B}}\left(\mathit{j}\left(\mathit{j}-1\right)/2+\mathit{k}\right)$ must contain $\sum _{\mathit{i}=1}^{m}{f}_{\mathit{i}}\frac{{\partial }^{2}{f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}\partial {x}_{\mathit{k}}}$ evaluated at the point $x$, for $\mathit{j}=1,2,\dots ,n$ and $\mathit{k}=1,2,\dots ,\mathit{j}$.
7:     LB – INTEGERInput
On entry: gives the length of the array B.
8:     IW(LIW) – INTEGER arrayWorkspace
9:     LIW – INTEGERInput
10:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
11:   LW – INTEGERInput
As in LSQFUN, these parameters correspond to the parameters IW, LIW, W, LW of E04YBF. LSQHES must not change the first $5×{\mathbf{N}}+{\mathbf{M}}×{\mathbf{N}}+{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2$ (or $6+2×{\mathbf{M}}$ if ${\mathbf{N}}=1$) elements of W.
LSQHES must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which E04YBF is called. Parameters denoted as Input must not be changed by this procedure.
5:     X(N) – REAL (KIND=nag_wp) arrayInput
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 ${b}_{jk}$ calculated by LSQHES. ‘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 could go undetected. For a similar reason, it is preferable that no two elements of X should have the same value.
6:     FVEC(M) – REAL (KIND=nag_wp) arrayOutput
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$.
7:     FJAC(LDFJAC,N) – REAL (KIND=nag_wp) arrayOutput
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$.
8:     LDFJAC – INTEGERInput
On entry: the first dimension of the array FJAC as declared in the (sub)program from which E04YBF is called.
Constraint: ${\mathbf{LDFJAC}}\ge {\mathbf{M}}$.
9:     B(LB) – REAL (KIND=nag_wp) arrayOutput
On exit: unless you set IFLAG negative in LSQHES, ${\mathbf{B}}\left(\mathit{j}×\left(\mathit{j}-1\right)/2+\mathit{k}\right)$ contains the value of ${b}_{\mathit{j}\mathit{k}}$ at the point given in X as calculated by LSQHES, for $\mathit{j}=1,2,\dots ,n$ and $\mathit{k}=1,2,\dots ,\mathit{j}$.
10:   LB – INTEGERInput
On entry: the dimension of the array B as declared in the (sub)program from which E04YBF is called.
Constraint: ${\mathbf{LB}}\ge \left({\mathbf{N}}+1\right)×{\mathbf{N}}/2$.
11:   IW(LIW) – INTEGER arrayWorkspace
This array appears in the parameter list purely so that, if E04YBF is called by another library routine, the library routine can pass quantities to user-supplied subroutines LSQFUN and LSQHES via IW. IW is not examined or changed by E04YBF. In general you must provide an array IW, but are advised not to use it.
12:   LIW – INTEGERInput
On entry: the actual length of IW as declared in the subroutine from which E04YBF is called.
Constraint: ${\mathbf{LIW}}\ge 1$.
13:   W(LW) – REAL (KIND=nag_wp) arrayWorkspace
14:   LW – INTEGERInput
On entry: the actual length of W as declared in the subroutine from which E04YBF is called.
Constraints:
• if ${\mathbf{N}}>1$, ${\mathbf{LW}}\ge 5×{\mathbf{N}}+{\mathbf{M}}+{\mathbf{M}}×{\mathbf{N}}+{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2$;
• if ${\mathbf{N}}=1$, ${\mathbf{LW}}\ge 6+2×{\mathbf{M}}$.
15:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ 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\text{​ 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 ${\mathbf{IFAIL}}\ne {\mathbf{0}}$ on exit, the recommended value is $-1$. When the value $-\mathbf{1}\text{​ or ​}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).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Note: E04YBF may return useful information for one or more of the following detected errors or warnings.
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}<0$
A negative value of IFAIL indicates an exit from E04YBF because you have set IFLAG negative in user-supplied subroutines LSQFUN or LSQHES. The setting of IFAIL will be the same as your setting of IFLAG. The check on LSQHES will not have been completed.
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{M}}<{\mathbf{N}}$, or ${\mathbf{N}}<1$, or ${\mathbf{LDFJAC}}<{\mathbf{M}}$, or ${\mathbf{LB}}<\left({\mathbf{N}}+1\right)×{\mathbf{N}}/2$, or ${\mathbf{LIW}}<1$, or ${\mathbf{LW}}<5×{\mathbf{N}}+{\mathbf{M}}+{\mathbf{M}}×{\mathbf{N}}+{\mathbf{N}}×\left({\mathbf{N}}-1\right)/2$, if ${\mathbf{N}}>1$, or ${\mathbf{LW}}<6+2×{\mathbf{M}}$, if ${\mathbf{N}}=1$.
${\mathbf{IFAIL}}=2$
You should check carefully the derivation and programming of expressions for the ${b}_{jk}$, because it is very unlikely that LSQHES is calculating them correctly.

## 7  Accuracy

IFAIL is set to $2$ if
 $yTGy-p≥hyTGy+1.0 or zTGz-q≥hzTGz+1.0$
where $h$ is set equal to $\sqrt{\epsilon }$ ($\epsilon$ being the machine precision as given by X02AJF) and other quantities are defined as in Section 3.

E04YBF calls LSQHES once and LSQFUN three times.

## 9  Example

Suppose that it is intended to use E04HEF to find least squares estimates of ${x}_{1},{x}_{2}$ and ${x}_{3}$ in the model
 $y = x1 + t1 x2 t2+ x3 t3$
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$
This example program could be used to check the ${b}_{jk}$ calculated by LSQHES required. (The call of E04YBF is preceded by a call of E04YAF to check LSQFUN which calculates the first derivatives.)

### 9.1  Program Text

Program Text (e04ybfe.f90)

### 9.2  Program Data

Program Data (e04ybfe.d)

### 9.3  Program Results

Program Results (e04ybfe.r)