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

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_numdiff_sample (d04bb)

## Purpose

nag_numdiff_sample (d04bb) generates abscissae about a target abscissa x0${x}_{0}$ for use in a subsequent call to nag_numdiff_rcomm (d04ba).

## Syntax

[xval] = d04bb(x_0, hbase)
[xval] = nag_numdiff_sample(x_0, hbase)

## Description

nag_numdiff_sample (d04bb) may be used to generate the necessary abscissae about a target abscissa x0${x}_{0}$ for the calculation of derivatives using nag_numdiff_rcomm (d04ba).
For a given x0${x}_{0}$ and h$h$, the abscissae correspond to the set {x0, x0 ± (2j1) h } $\left\{{x}_{0},{x}_{0}±\left(2\mathit{j}-1\right)h\right\}$, for j = 1,2,,10$\mathit{j}=1,2,\dots ,10$. These 21$21$ points will be returned in ascending order in xval. In particular, xval(11)${\mathbf{xval}}\left(11\right)$ will be equal to x0${x}_{0}$.

## References

Lyness J N and Moler C B (1969) Generalised Romberg methods for integrals of derivatives Numer. Math. 14 1–14

## Parameters

### Compulsory Input Parameters

1:     x_0 – double scalar
The abscissa x0${x}_{0}$ at which derivatives are required.
2:     hbase – double scalar
The chosen step size h$h$. If h < 10ε$h<10\epsilon$, where ε = x02aj()$\epsilon =\mathbf{x02aj}\left(\right)$, then the default h = ε(1 / 4)$h={\epsilon }^{\left(1/4\right)}$ will be used.

None.

None.

### Output Parameters

1:     xval(21$21$) – double array
The abscissae for passing to nag_numdiff_rcomm (d04ba).

None.

Not applicable.

## Further Comments

The results computed by nag_numdiff_rcomm (d04ba) depend very critically on the choice of the user-supplied step length h$h$. The overall accuracy is diminished as h$h$ becomes small (because of the effect of round-off error) and as h$h$ becomes large (because the discretization error also becomes large). If the process of calculating derivatives is repeated four or five times with different values of h$h$ one can find a reasonably good value. A process in which the value of h$h$ is successively halved (or doubled) is usually quite effective. Experience has shown that in cases in which the Taylor series for for the objective function about x0${x}_{0}$ has a finite radius of convergence R$R$, the choices of h > R / 19$h>R/19$ are not likely to lead to good results. In this case some function values lie outside the circle of convergence.

## Example

```function nag_numdiff_sample_example
fprintf('\nFind the derivatives of the polygamma (psi) function\n');
fprintf('using function values generated by nag_specfun_psi_deriv_real.\n\n');
fprintf('Demonstrate the effect of successively reducing hbase.\n\n');
hbase = 0.0025;

% Set the target location and calculate the objective value
x_0 = 0.05;
[f_0, ifail] = nag_specfun_psi_deriv_real(x_0, int64(0));

% Compute the actual derivatives using nag_specfun_psi_deriv_real for comparison
actder = zeros(3, 1);
for j=1:3
[actder(j), ifail] = nag_specfun_psi_deriv_real(x_0, int64(j));
end

der_comp = zeros(14*4, 3);
% Attempt 4 applications, reducing hbase by factor 0.1 each time
for j=1:4
% Generate the abscissa xval using nag_numdiff_sample
[xval] = nag_numdiff_sample(x_0, hbase);

% Calculate the corresponding objective function values
fval(11) = f_0;
for k=1:10
[fval(k),    ifail] = nag_specfun_psi_deriv_real(xval(k),    int64(0));
[fval(k+11), ifail] = nag_specfun_psi_deriv_real(xval(k+11), int64(0));
end

% Call nag_numdiff_rcomm to calculate the derivative estimates
[der, erest, ifail] = nag_numdiff_rcomm(xval, fval);
if ifail ~= 0
break;
end

% Store results in der_comp
der_comp(j:4:14*4, 1) = hbase;
der_comp(j:4:14*4, 2) = der;
der_comp(j:4:14*4, 3) = erest;

% Decrease hbase for next loop
hbase = hbase*0.1;
end

% Display results for first 3 derivatives
if ifail == 0
for i=1:3
fprintf('\nderivative %d calculated using nag_specfun_psi_deriv_real: %11.4e\n', i, actder(i));
fprintf('Derivative and error estimates for derivative %d\n', i);
fprintf('      hbase        der(%d)      erest(%d)\n', i, i);
for j=1:4
fprintf('  %12.4e %12.4e %12.4e\n', der_comp(4*(i-1)+j,:));
end
end
else
fprintf('\nnag_numdiff_rcomm failed with ifail = %d\n', ifail);
end
```
```

Find the derivatives of the polygamma (psi) function
using function values generated by nag_specfun_psi_deriv_real.

Demonstrate the effect of successively reducing hbase.

derivative 1 calculated using nag_specfun_psi_deriv_real:  4.0153e+02
Derivative and error estimates for derivative 1
hbase        der(1)      erest(1)
2.5000e-03   4.0204e+02   1.3940e+02
2.5000e-04   4.0153e+02   4.1666e-11
2.5000e-05   4.0153e+02   2.1799e-10
2.5000e-06   4.0153e+02   1.2417e-09

derivative 2 calculated using nag_specfun_psi_deriv_real: -1.6002e+04
Derivative and error estimates for derivative 2
hbase        der(2)      erest(2)
2.5000e-03  -1.6022e+04   5.5760e+03
2.5000e-04  -1.6002e+04   1.3738e-07
2.5000e-05  -1.6002e+04   6.0436e-06
2.5000e-06  -1.6002e+04   9.5762e-04

derivative 3 calculated using nag_specfun_psi_deriv_real:  9.6001e+05
Derivative and error estimates for derivative 3
hbase        der(3)      erest(3)
2.5000e-03   9.1465e+05  -7.3750e+06
2.5000e-04   9.6001e+05   2.1981e-04
2.5000e-05   9.6001e+05   9.4922e-02
2.5000e-06   9.6001e+05   5.9679e+01

```
```function d04bb_example
fprintf('\nFind the derivatives of the polygamma (psi) function\n');
fprintf('using function values generated by s14ae.\n\n');
fprintf('Demonstrate the effect of successively reducing hbase.\n\n');
hbase = 0.0025;

% Set the target location and calculate the objective value
x_0 = 0.05;
[f_0, ifail] = s14ae(x_0, int64(0));

% Compute the actual derivatives using s14ae for comparison
actder = zeros(3, 1);
for j=1:3
[actder(j), ifail] = s14ae(x_0, int64(j));
end

der_comp = zeros(14*4, 3);
% Attempt 4 applications, reducing hbase by factor 0.1 each time
for j=1:4
% Generate the abscissa xval using d04bb
[xval] = d04bb(x_0, hbase);

% Calculate the corresponding objective function values
fval(11) = f_0;
for k=1:10
[fval(k),    ifail] = s14ae(xval(k),    int64(0));
[fval(k+11), ifail] = s14ae(xval(k+11), int64(0));
end

% Call d04ba to calculate the derivative estimates
[der, erest, ifail] = d04ba(xval, fval);
if ifail ~= 0
break;
end

% Store results in der_comp
der_comp(j:4:14*4, 1) = hbase;
der_comp(j:4:14*4, 2) = der;
der_comp(j:4:14*4, 3) = erest;

% Decrease hbase for next loop
hbase = hbase*0.1;
end

% Display results for first 3 derivatives
if ifail == 0
for i=1:3
fprintf('\nderivative %d calculated using s14ae: %11.4e\n', i, actder(i));
fprintf('Derivative and error estimates for derivative %d\n', i);
fprintf('      hbase        der(%d)      erest(%d)\n', i, i);
for j=1:4
fprintf('  %12.4e %12.4e %12.4e\n', der_comp(4*(i-1)+j,:));
end
end
else
fprintf('\nd04ba failed with ifail = %d\n', ifail);
end
```
```

Find the derivatives of the polygamma (psi) function
using function values generated by s14ae.

Demonstrate the effect of successively reducing hbase.

derivative 1 calculated using s14ae:  4.0153e+02
Derivative and error estimates for derivative 1
hbase        der(1)      erest(1)
2.5000e-03   4.0204e+02   1.3940e+02
2.5000e-04   4.0153e+02   4.1666e-11
2.5000e-05   4.0153e+02   2.1799e-10
2.5000e-06   4.0153e+02   1.2417e-09

derivative 2 calculated using s14ae: -1.6002e+04
Derivative and error estimates for derivative 2
hbase        der(2)      erest(2)
2.5000e-03  -1.6022e+04   5.5760e+03
2.5000e-04  -1.6002e+04   1.3738e-07
2.5000e-05  -1.6002e+04   6.0436e-06
2.5000e-06  -1.6002e+04   9.5762e-04

derivative 3 calculated using s14ae:  9.6001e+05
Derivative and error estimates for derivative 3
hbase        der(3)      erest(3)
2.5000e-03   9.1465e+05  -7.3750e+06
2.5000e-04   9.6001e+05   2.1981e-04
2.5000e-05   9.6001e+05   9.4922e-02
2.5000e-06   9.6001e+05   5.9679e+01

```

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013