c05 Chapter Contents
c05 Chapter Introduction
NAG Library Manual

# NAG Library Function Documentnag_check_derivs (c05zdc)

## 1  Purpose

nag_check_derivs (c05zdc) checks the user-supplied gradients of a set of nonlinear functions in several variables, for consistency with the functions themselves. The function must be called twice.

## 2  Specification

 #include #include
 void nag_check_derivs (Integer mode, Integer m, Integer n, const double x[], const double fvec[], const double fjac[], double xp[], const double fvecp[], double err[], NagError *fail)

## 3  Description

nag_check_derivs (c05zdc) 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 nag_check_derivs (c05zdc) is its invariance under changes in scale of the variables or functions.

## 4  References

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

## 5  Arguments

1:    $\mathbf{mode}$IntegerInput
On entry: the value $1$ on the first call and the value $2$ on the second call of nag_check_derivs (c05zdc).
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 nag_zero_nonlin_eqns_deriv_easy (c05rbc), nag_zero_nonlin_eqns_deriv_expert (c05rcc) and nag_zero_nonlin_eqns_deriv_rcomm (c05rdc), ${\mathbf{m}}={\mathbf{n}}$.
Constraint: ${\mathbf{n}}\ge 1$.
4:    $\mathbf{x}\left[{\mathbf{n}}\right]$const doubleInput
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]$const doubleInput
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]$const doubleInput
Note: the $\left(i,j\right)$th element of the matrix is stored in ${\mathbf{fjac}}\left[\left(j-1\right)×{\mathbf{m}}+i-1\right]$.
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]$doubleOutput
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]$const doubleInput
On entry: if ${\mathbf{mode}}=2$, fvecp must contain the value of the functions evaluated at xp (as output by a preceding call to nag_check_derivs (c05zdc) with ${\mathbf{mode}}=1$). If ${\mathbf{mode}}=1$, fvecp is not referenced.
9:    $\mathbf{err}\left[{\mathbf{m}}\right]$doubleOutput
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), then if ${\mathbf{err}}\left[i-1\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-1\right]$ is $0.0$ the $i$th gradient is incorrect. For values of ${\mathbf{err}}\left[i-1\right]$ between $0.0$ and $1.0$ the categorisation is less certain. In general, a value of ${\mathbf{err}}\left[i-1\right]>0.5$ indicates that the $i$th gradient is probably correct.
10:  $\mathbf{fail}$NagError *Input/Output
The NAG error argument (see Section 3.6 in the Essential Introduction).

## 6  Error Indicators and Warnings

NE_ALLOC_FAIL
Dynamic memory allocation failed.
See Section 3.2.1.2 in the Essential Introduction for further information.
On entry, argument $〈\mathit{\text{value}}〉$ had an illegal value.
NE_INT
On entry, ${\mathbf{m}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{m}}\ge 1$.
On entry, ${\mathbf{mode}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{mode}}=1$ or $2$.
On entry, ${\mathbf{n}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{n}}\ge 1$.
NE_INTERNAL_ERROR
An internal error has occurred in this function. Check the function call and any array sizes. If the call is correct then please contact NAG for assistance.
See Section 3.6.6 in the Essential Introduction for further information.
NE_NO_LICENCE
Your licence key may have expired or may not have been installed correctly.
See Section 3.6.5 in the Essential Introduction for further information.

## 7  Accuracy

nag_check_derivs (c05zdc) 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 nag_machine_precision (X02AJC).

## 8  Parallelism and Performance

Not applicable.

The time required by nag_check_derivs (c05zdc) increases with m and n.

## 10  Example

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

### 10.1  Program Text

Program Text (c05zdce.c)

None.

### 10.3  Program Results

Program Results (c05zdce.r)