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

# Syntax

C#
```public static void e04ya(
int m,
int n,
E04..::..E04YA_LSQFUN lsqfun,
double[] x,
double[] fvec,
double[,] fjac,
out int ifail
)```
Visual Basic
```Public Shared Sub e04ya ( _
m As Integer, _
n As Integer, _
lsqfun As E04..::..E04YA_LSQFUN, _
x As Double(), _
fvec As Double(), _
fjac As Double(,), _
<OutAttribute> ByRef ifail As Integer _
)```
Visual C++
```public:
static void e04ya(
int m,
int n,
E04..::..E04YA_LSQFUN^ lsqfun,
array<double>^ x,
array<double>^ fvec,
array<double,2>^ fjac,
[OutAttribute] int% ifail
)```
F#
```static member e04ya :
m : int *
n : int *
lsqfun : E04..::..E04YA_LSQFUN *
x : float[] *
fvec : float[] *
fjac : 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..::..E04YA_LSQFUN
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 methods mentioned in [Description] give you the option of resetting a parameter to terminate immediately. e04ya will also terminate immediately, without finishing the checking process, if the parameter in question is reset.)

A delegate of type E04YA_LSQFUN.

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 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.
fvec
Type: array<System..::..Double>[]()[][]
An array of size [m]
On exit: unless you set iflag negative in the first call of lsqfun, ${\mathbf{fvec}}\left[\mathit{i}-1\right]$ contains the value of ${f}_{\mathit{i}}$ at the point supplied by you in x, for $\mathit{i}=1,2,\dots ,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}}\left[\mathit{i}-1,\mathit{j}-1\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$.
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

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 method to evaluate the ${f}_{i}$ and their first derivatives. e04ya checks the derivatives calculated by such user-supplied delegates, e.g., methods of the form required for (E04GBF not in this release) e04gd e04he. As well as the method 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. e04ya is essentially identical to CHKLSJ in the NPL Algorithms Library.
e04ya 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=Fx+hpk-Fxh, 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.

# Error Indicators and Warnings

Note: e04ya 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$
A negative value of ifail indicates an exit from e04ya 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.
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{m}}<{\mathbf{n}}$, or ${\mathbf{n}}<1$,
${\mathbf{ifail}}=2$
You should check carefully the derivation and programming of expressions for the $\frac{\partial {f}_{i}}{\partial {x}_{j}}$, because it is very unlikely that lsqfun is calculating them correctly.
${\mathbf{ifail}}=-9000$
An error occured, see message report.
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-4000$
Invalid dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-8000$
Negative dimension for array $〈\mathit{\text{value}}〉$
${\mathbf{ifail}}=-6000$
Invalid Parameters $〈\mathit{\text{value}}〉$

# Accuracy

ifail is set to $2$ if
 $vk-gTpk2≥h×gTpk2+1$
for $k=1\text{​ or ​}2$. (See [Description] for definitions of the quantities involved.) The scalar $h$ is set equal to $\sqrt{\epsilon }$, where $\epsilon$ is the machine precision as given by x02aj.

# Parallelism and Performance

None.

e04ya only checks the derivatives calculated by a user-supplied method when ${\mathbf{iflag}}=2$. So, if lsqfun is intended for use in conjunction with a minimization method which may set iflag to $1$, you must check that, for given settings of the ${\mathbf{xc}}\left[j-1\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$.