Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_roots_withdraw_sys_deriv_check (c05za)

## Purpose

nag_roots_withdraw_sys_deriv_check (c05za) 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.
Note: this function is scheduled to be withdrawn, please see c05za in Advice on Replacement Calls for Withdrawn/Superseded Routines..

## Syntax

[xp, err] = c05za(x, fvec, fjac, fvecp, mode, 'm', m, 'n', n)
[xp, err] = nag_roots_withdraw_sys_deriv_check(x, fvec, fjac, fvecp, mode, 'm', m, 'n', n)

## Description

nag_roots_withdraw_sys_deriv_check (c05za) is based on the MINPACK routine CHKDER (see Moré et al. (1980)). It checks the i$i$th gradient for consistency with the i$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_roots_withdraw_sys_deriv_check (c05za) is its invariance under changes in scale of the variables or functions.

## 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

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
The components of a point x$x$, at which the consistency check is to be made. (See Section [Further Comments].)
2:     fvec(m) – double array
When mode = 2 ${\mathbf{mode}}=2$, fvec must contain the functions evaluated at x$x$.
3:     fjac(ldfjac,n) – double array
ldfjac, the first dimension of the array, must satisfy the constraint ldfjacm $\mathit{ldfjac}\ge {\mathbf{m}}$.
When mode = 2 ${\mathbf{mode}}=2$, fjac must contain the user-supplied gradients. (The i$i$th row of fjac must contain the gradient of the i$i$th function evaluated at the point x$x$.)
4:     fvecp(m) – double array
When mode = 2 ${\mathbf{mode}}=2$, fvecp must contain the functions evaluated at xp.
5:     mode – int64int32nag_int scalar
The value 1$1$ on the first call and the value 2$2$ on the second call of nag_roots_withdraw_sys_deriv_check (c05za).

### Optional Input Parameters

1:     m – int64int32nag_int scalar
Default: The dimension of the arrays fvec, fvecp and the first dimension of the array fjac. (An error is raised if these dimensions are not equal.)
The number of functions.
2:     n – int64int32nag_int scalar
Default: The dimension of the array x and the second dimension of the array fjac. (An error is raised if these dimensions are not equal.)
The number of variables. For use with nag_roots_withdraw_sys_deriv_easy (c05pb) and nag_roots_withdraw_sys_deriv_expert (c05pc), m = n ${\mathbf{m}}={\mathbf{n}}$.

ldfjac

### Output Parameters

1:     xp( : $:$) – double array
Note: the dimension of the array xp must be at least n${\mathbf{n}}$ if mode = 1${\mathbf{mode}}=1$, and at least 1$1$ otherwise.
When mode = 1 ${\mathbf{mode}}=1$, xp is set to a neighbouring point to x.
2:     err( : $:$) – double array
Note: the dimension of the array err must be at least m${\mathbf{m}}$ if mode = 2${\mathbf{mode}}=2$, and at least 1$1$ otherwise.
When mode = 2 ${\mathbf{mode}}=2$, err contains measures of correctness of the respective gradients. If there is no loss of significance (see Section [Further Comments]), then if err(i) ${\mathbf{err}}\left(i\right)$ is 1.0$1.0$ the i$i$th user-supplied gradient is correct, whilst if err(i) ${\mathbf{err}}\left(i\right)$ is 0.0$0.0$ the i$i$th gradient is incorrect. For values of err(i) ${\mathbf{err}}\left(i\right)$ between 0.0$0.0$ and 1.0$1.0$ the categorisation is less certain. In general, a value of err(i) > 0.5 ${\mathbf{err}}\left(i\right)>0.5$ indicates that the i$i$th gradient is probably correct.

## Error Indicators and Warnings

If an error is detected in an input parameter nag_roots_withdraw_sys_deriv_check (c05za) will act as if a soft noisy exit has been requested (see Section [Soft Fail Option] in the (essin)).

## Accuracy

The time required by nag_roots_withdraw_sys_deriv_check (c05za) increases with m and n.
nag_roots_withdraw_sys_deriv_check (c05za) 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$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.

## Example

```function nag_roots_withdraw_sys_deriv_check_example
% Point at which to check gradients:
x = [0.92, 0.13, 0.54];

fvec  = zeros(15, 1);
fjac  = zeros(15, 3);
fvecp = zeros(15, 1);

y = 0.01*[14, 18, 22, 25, 29, 32, 35, 39, 47, 58, 73, 96, 134, 210, 439];

[xp, err] = nag_roots_withdraw_sys_deriv_check(x, fvec, fjac, fvecp, int64(1));

for i=1:15
u = i;
v = 16 - i;
w = min(u, v);
fvec(i)  = y(i) - (x(1)+u/(v*x(2)+w*x(3)));
fvecp(i) = y(i) - (xp(1)+u/(v*xp(2)+w*xp(3)));
denom = (v*x(2)+w*x(3))^(-2);
fjac(i,:) = [-1, u*v*denom, u*w*denom];
end

[xp, err] = nag_roots_withdraw_sys_deriv_check(x, fvec, fjac, fvecp, int64(2));

fprintf('\nAt point %12.4f %12.4f %12.4f\n', x);
if any(err <= 0.5)
for i=1:15
if err(i) <= 0.5
fprintf('Suspicious gradient number %d with error measure %12.4f\n', i, err(i));
end
end
else
end
```
```

At point       0.9200       0.1300       0.5400

```
```function c05za_example
% Point at which to check gradients:
x = [0.92, 0.13, 0.54];

fvec  = zeros(15, 1);
fjac  = zeros(15, 3);
fvecp = zeros(15, 1);

y = 0.01*[14, 18, 22, 25, 29, 32, 35, 39, 47, 58, 73, 96, 134, 210, 439];

[xp, err] = c05za(x, fvec, fjac, fvecp, int64(1));

for i=1:15
u = i;
v = 16 - i;
w = min(u, v);
fvec(i)  = y(i) - (x(1)+u/(v*x(2)+w*x(3)));
fvecp(i) = y(i) - (xp(1)+u/(v*xp(2)+w*xp(3)));
denom = (v*x(2)+w*x(3))^(-2);
fjac(i,:) = [-1, u*v*denom, u*w*denom];
end

[xp, err] = c05za(x, fvec, fjac, fvecp, int64(2));

fprintf('\nAt point %12.4f %12.4f %12.4f\n', x);
if any(err <= 0.5)
for i=1:15
if err(i) <= 0.5
fprintf('Suspicious gradient number %d with error measure %12.4f\n', i, err(i));
end
end
else
end
```
```

At point       0.9200       0.1300       0.5400