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_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$. (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]

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

- 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)={\displaystyle \sum _{i=1}^{m}}{\left[{f}_{i}\left(x\right)\right]}^{2}$,
and its first derivatives ${g}_{j}={\left.\frac{\partial F}{\partial {x}_{j}}\right|}_{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

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.

$${v}_{k}=\frac{F\left(x+h{p}_{k}\right)-F\left(x\right)}{h}\text{, \hspace{1em}}k=1,2$$ |

# References

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$

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

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.

$${\left({v}_{k}-{g}^{\mathrm{T}}{p}_{k}\right)}^{2}\ge h\times \left({\left({g}^{\mathrm{T}}{p}_{k}\right)}^{2}+1\right)$$ |

# Parallelism and Performance

None.

# Further Comments

Before using e04ya to check the calculation of the first derivatives, you should be confident that lsqfun is calculating the residuals correctly.

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