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

# Syntax

C# |
---|

public static void e04yb( int m, int n, E04..::..E04YB_LSQFUN lsqfun, E04..::..E04YB_LSQHES lsqhes, double[] x, double[] fvec, double[,] fjac, double[] b, out int ifail ) |

Visual Basic |
---|

Public Shared Sub e04yb ( _ m As Integer, _ n As Integer, _ lsqfun As E04..::..E04YB_LSQFUN, _ lsqhes As E04..::..E04YB_LSQHES, _ x As Double(), _ fvec As Double(), _ fjac As Double(,), _ b As Double(), _ <OutAttribute> ByRef ifail As Integer _ ) |

Visual C++ |
---|

public: static void e04yb( int m, int n, E04..::..E04YB_LSQFUN^ lsqfun, E04..::..E04YB_LSQHES^ lsqhes, array<double>^ x, array<double>^ fvec, array<double,2>^ fjac, array<double>^ b, [OutAttribute] int% ifail ) |

F# |
---|

static member e04yb : m : int * n : int * lsqfun : E04..::..E04YB_LSQFUN * lsqhes : E04..::..E04YB_LSQHES * x : float[] * fvec : float[] * fjac : float[,] * b : float[] * ifail : int byref -> unit |

#### Parameters

- m
- Type: System..::..Int32
*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}}$.

- n
- Type: System..::..Int32
*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}}$.

- lsqfun
- Type: NagLibrary..::..E04..::..E04YB_LSQFUNlsqfun 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$. (e04he gives you the option of resetting parameters of lsqfun to cause the minimization process to terminate immediately. e04yb will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)
A delegate of type E04YB_LSQFUN.

- lsqhes
- Type: NagLibrary..::..E04..::..E04YB_LSQHESlsqhes must calculate the elements of the symmetric matrixat 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.)
$$B\left(x\right)=\sum _{i=1}^{m}{f}_{i}\left(x\right){G}_{i}\left(x\right)\text{,}$$ A delegate of type E04YB_LSQHES.

- x
- Type: array<System..::..Double>[]()[][]An array of size [n]
*On entry*: ${\mathbf{x}}\left[\mathit{j}-1\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.

- fvec
- Type: array<System..::..Double>[]()[][]An array of size [m]

- fjac
- Type: array<System..::..Double,2>[,](,)[,][,]An array of size [dim1, n]
**Note:**dim1 must satisfy the constraint: $\mathrm{dim1}\ge {\mathbf{m}}$*On exit*: unless you set iflag negative in the first call of lsqfun, ${\mathbf{fjac}}[\mathit{i}-1,\mathit{j}-1]$ 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$.

- b
- Type: array<System..::..Double>[]()[][]An array of size [lb]
*On exit*: unless you set iflag negative in lsqhes, ${\mathbf{b}}\left[\mathit{j}\times \left(\mathit{j}-1\right)/2+\mathit{k}-1\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}$.

- ifail
- Type: System..::..Int32%
*On exit*: ${\mathbf{ifail}}={0}$ unless the method detects an error or a warning has been flagged (see [Error Indicators and Warnings]).

# Description

Methods 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 method to evaluate the quantities

for $j=1,2,\dots ,n$ and $k=1,2,\dots ,j$. e04yb is designed to check the ${b}_{jk}$ calculated by such user-supplied delegates. As well as the method to be checked (lsqhes), you must supply a method (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 e04yb checks methods of the form required by e04he. e04yb is essentially identical to CHKLSH in the NPL Algorithms Library.

$${b}_{jk}=\sum _{i=1}^{m}{f}_{i}\frac{{\partial}^{2}{f}_{i}}{\partial {x}_{j}\partial {x}_{k}}$$ |

e04yb first calls user-supplied delegates 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,

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

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.

$$G={J}^{\mathrm{T}}J+B\text{,}$$ |

$$\begin{array}{c}p=\left({y}^{\mathrm{T}}g\left(x+hy\right)-{y}^{\mathrm{T}}g\left(x\right)\right)/h\text{\hspace{1em} and}\\ q=\left({z}^{\mathrm{T}}g\left(x+hz\right)-{z}^{\mathrm{T}}g\left(x\right)\right)/h\end{array}$$ |

# References

None.

# Error Indicators and Warnings

**Note:**e04yb may return useful information for one or more of the following detected errors or warnings.

Errors or warnings detected by the method:

Some error messages may refer to parameters that are dropped from this interface
(LDFJAC, IW, LIW, W, LW) In these
cases, an error in another parameter has usually caused an incorrect value to be inferred.

- ${\mathbf{ifail}}<0$

- ${\mathbf{ifail}}=1$
On entry, ${\mathbf{m}}<{\mathbf{n}}$, or ${\mathbf{n}}<1$, or ${\mathbf{lb}}<\left({\mathbf{n}}+1\right)\times {\mathbf{n}}/2$,

- ${\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.

- ${\mathbf{ifail}}=-9000$
- An error occured, see message report.
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-4000$
- Invalid dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-8000$
- Negative dimension for array $\u2329\mathit{\text{value}}\u232a$
- ${\mathbf{ifail}}=-6000$
- Invalid Parameters $\u2329\mathit{\text{value}}\u232a$

# Accuracy

ifail is set to $2$ if

where $h$ is set equal to $\sqrt{\epsilon}$ ($\epsilon $ being the machine precision as given by x02aj) and other quantities are defined as in [Description].

$$\begin{array}{ll}\left|{y}^{\mathrm{T}}Gy-p\right|\ge \sqrt{h}\left(\left|{y}^{\mathrm{T}}Gy\right|+1.0\right)& \text{or}\\ \left|{z}^{\mathrm{T}}Gz-q\right|\ge \sqrt{h}\left(\left|{z}^{\mathrm{T}}Gz\right|+1.0\right)& \end{array}$$ |

# Parallelism and Performance

None.