# NAG Library Routine Document

## 1Purpose

c05zdf checks the user-supplied gradients of a set of nonlinear functions in several variables, for consistency with the functions themselves. The routine must be called twice.

## 2Specification

Fortran Interface
 Subroutine c05zdf ( mode, m, n, x, fvec, fjac, xp, err,
 Integer, Intent (In) :: mode, m, n Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n), fvec(m), fjac(m,n), fvecp(m) Real (Kind=nag_wp), Intent (Out) :: xp(n), err(m)
#include nagmk26.h
 void c05zdf_ (const Integer *mode, const Integer *m, const Integer *n, const double x[], const double fvec[], const double fjac[], double xp[], const double fvecp[], double err[], Integer *ifail)

## 3Description

c05zdf is based on the MINPACK routine CHKDER (see Moré et al. (1980)). It checks the $i$th gradient for consistency with the $i$th function by computing a forward-difference approximation along a suitably chosen direction and comparing this approximation with the user-supplied gradient along the same direction. The principal characteristic of c05zdf is its invariance under changes in scale of the variables or functions.

## 4References

Moré J J, Garbow B S and Hillstrom K E (1980) User guide for MINPACK-1 Technical Report ANL-80-74 Argonne National Laboratory

## 5Arguments

1:     $\mathbf{mode}$ – IntegerInput
On entry: the value $1$ on the first call and the value $2$ on the second call of c05zdf.
Constraint: ${\mathbf{mode}}=1$ or $2$.
2:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of functions.
Constraint: ${\mathbf{m}}\ge 1$.
3:     $\mathbf{n}$ – IntegerInput
On entry: $n$, the number of variables. For use with c05rbf, c05rcf and c05rdf, ${\mathbf{m}}={\mathbf{n}}$.
Constraint: ${\mathbf{n}}\ge 1$.
4:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the components of a point $x$, at which the consistency check is to be made. (See Section 7.)
5:     $\mathbf{fvec}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: if ${\mathbf{mode}}=2$, fvec must contain the value of the functions evaluated at $x$. If ${\mathbf{mode}}=1$, fvec is not referenced.
6:     $\mathbf{fjac}\left({\mathbf{m}},{\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: if ${\mathbf{mode}}=2$, fjac must contain the value of $\frac{\partial {f}_{i}}{\partial {x}_{j}}$ at the point $x$, for $\mathit{i}=1,2,\dots ,m$ and $\mathit{j}=1,2,\dots ,n$. If ${\mathbf{mode}}=1$, fjac is not referenced.
7:     $\mathbf{xp}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: if ${\mathbf{mode}}=1$, xp is set to a point neighbouring x. If ${\mathbf{mode}}=2$, xp is undefined.
8:     $\mathbf{fvecp}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: if ${\mathbf{mode}}=2$, fvecp must contain the value of the functions evaluated at xp (as output by a preceding call to c05zdf with ${\mathbf{mode}}=1$). If ${\mathbf{mode}}=1$, fvecp is not referenced.
9:     $\mathbf{err}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: if ${\mathbf{mode}}=2$, err contains measures of correctness of the respective gradients. If ${\mathbf{mode}}=1$, err is undefined. If there is no loss of significance (see Section 7), if ${\mathbf{err}}\left(i\right)$ is $1.0$ the $i$th user-supplied gradient $\frac{\partial {f}_{i}}{\partial {x}_{j}}$, for $\mathit{j}=1,2,\dots ,n$ is correct, whilst if ${\mathbf{err}}\left(i\right)$ is $0.0$ the $i$th gradient is incorrect. For values of ${\mathbf{err}}\left(i\right)$ between $0.0$ and $1.0$ the categorisation is less certain. In general, a value of ${\mathbf{err}}\left(i\right)>0.5$ indicates that the $i$th gradient is probably correct.
10:   $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation 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, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=1$ or $2$.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

c05zdf does not perform reliably if cancellation or rounding errors cause a severe loss of significance in the evaluation of a function. Therefore, none of the components of $x$ should be unusually small (in particular, zero) or any other value which may cause loss of significance. The relative differences between corresponding elements of fvecp and fvec should be at least two orders of magnitude greater than the machine precision returned by x02ajf.

## 8Parallelism and Performance

c05zdf is not threaded in any implementation.

The time required by c05zdf increases with m and n.

## 10Example

This example checks the Jacobian matrix for a problem with $15$ functions of $3$ variables (sometimes referred to as the Bard problem).

### 10.1Program Text

Program Text (c05zdfe.f90)

None.

### 10.3Program Results

Program Results (c05zdfe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017