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_specfun_kelvin_ker_vector (s19aq)

## Purpose

nag_specfun_kelvin_ker_vector (s19aq) returns an array of values for the Kelvin function kerx$\mathrm{ker}x$.

## Syntax

[f, ivalid, ifail] = s19aq(x, 'n', n)
[f, ivalid, ifail] = nag_specfun_kelvin_ker_vector(x, 'n', n)

## Description

nag_specfun_kelvin_ker_vector (s19aq) evaluates an approximation to the Kelvin function kerxi$\mathrm{ker}{x}_{i}$ for an array of arguments xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
Note:  for x < 0$x<0$ the function is undefined and at x = 0$x=0$ it is infinite so we need only consider x > 0$x>0$.
The function is based on several Chebyshev expansions:
For 0 < x1$0,
 kerx = − f(t)log(x) + π/16x2g(t) + y(t) $ker⁡x=-f(t)log(x)+π16x2g(t)+y(t)$
where f(t)$f\left(t\right)$, g(t)$g\left(t\right)$ and y(t)$y\left(t\right)$ are expansions in the variable t = 2x41$t=2{x}^{4}-1$.
For 1 < x3$1,
 kerx = exp( − (11/16)x) q(t) $ker⁡x=exp(-1116x) q(t)$
where q(t)$q\left(t\right)$ is an expansion in the variable t = x2$t=x-2$.
For x > 3$x>3$,
 kerx = sqrt(π/(2x))e − x / sqrt(2) [(1 + 1/xc(t))cosβ − 1/xd(t)sinβ] $ker⁡x=π 2x e-x/2 [ (1+1xc(t)) cos⁡β-1xd(t)sin⁡β]$
where β = x/(sqrt(2)) + π/8 $\beta =\frac{x}{\sqrt{2}}+\frac{\pi }{8}$, and c(t)$c\left(t\right)$ and d(t)$d\left(t\right)$ are expansions in the variable t = 6/x1$t=\frac{6}{x}-1$.
When x$x$ is sufficiently close to zero, the result is computed as
 kerx = − γ − log(x/2) + (π − (3/8)x2) (x2)/16 $ker⁡x=-γ-log(x2)+(π-38x2) x216$
and when x$x$ is even closer to zero, simply as kerx = γlog(x/2) $\mathrm{ker}x=-\gamma -\mathrm{log}\left(\frac{x}{2}\right)$.
For large x$x$, kerx$\mathrm{ker}x$ is asymptotically given by sqrt(π/(2x))ex / sqrt(2)$\sqrt{\frac{\pi }{2x}}{e}^{-x/\sqrt{2}}$ and this becomes so small that it cannot be computed without underflow and the function fails.

## References

Abramowitz M and Stegun I A (1972) Handbook of Mathematical Functions (3rd Edition) Dover Publications

## Parameters

### Compulsory Input Parameters

1:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n0${\mathbf{n}}\ge 0$.
The argument xi${x}_{\mathit{i}}$ of the function, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
Constraint: x(i) > 0.0${\mathbf{x}}\left(\mathit{i}\right)>0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the array x.
n$n$, the number of points.
Constraint: n0${\mathbf{n}}\ge 0$.

None.

### Output Parameters

1:     f(n) – double array
kerxi$\mathrm{ker}{x}_{i}$, the function values.
2:     ivalid(n) – int64int32nag_int array
ivalid(i)${\mathbf{ivalid}}\left(\mathit{i}\right)$ contains the error code for xi${x}_{\mathit{i}}$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,{\mathbf{n}}$.
ivalid(i) = 0${\mathbf{ivalid}}\left(i\right)=0$
No error.
ivalid(i) = 1${\mathbf{ivalid}}\left(i\right)=1$
xi${x}_{i}$ is too large, the result underflows. f(i)${\mathbf{f}}\left(\mathit{i}\right)$ contains zero. The threshold value is the same as for ${\mathbf{ifail}}={\mathbf{1}}$ in nag_specfun_kelvin_ker (s19ac), as defined in the Users' Note for your implementation.
ivalid(i) = 2${\mathbf{ivalid}}\left(i\right)=2$
xi0.0${x}_{i}\le 0.0$, the function is undefined. f(i)${\mathbf{f}}\left(\mathit{i}\right)$ contains 0.0$0.0$.
3:     ifail – int64int32nag_int scalar
${\mathrm{ifail}}={\mathbf{0}}$ unless the function detects an error (see [Error Indicators and Warnings]).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W ifail = 1${\mathbf{ifail}}=1$
On entry, at least one value of x was invalid.
ifail = 2${\mathbf{ifail}}=2$
Constraint: n0${\mathbf{n}}\ge 0$.

## Accuracy

Let E$E$ be the absolute error in the result, ε$\epsilon$ be the relative error in the result and δ$\delta$ be the relative error in the argument. If δ$\delta$ is somewhat larger than the machine precision, then we have:
 E ≃ |x/(sqrt(2))(ker1x + kei1x)|δ, $E≃ | x2 ( ker1⁡x+ kei1⁡x ) |δ,$
 ε ≃ |x/(sqrt(2))( ker1x + kei1x )/(kerx)| δ. $ε ≃ | x2 ker1⁡x + kei1⁡x ker⁡x | δ.$
For very small x$x$, the relative error amplification factor is approximately given by 1/(|log(x)|) $\frac{1}{|\mathrm{log}\left(x\right)|}$, which implies a strong attenuation of relative error. However, ε$\epsilon$ in general cannot be less than the machine precision.
For small x$x$, errors are damped by the function and hence are limited by the machine precision.
For medium and large x$x$, the error behaviour, like the function itself, is oscillatory, and hence only the absolute accuracy for the function can be maintained. For this range of x$x$, the amplitude of the absolute error decays like sqrt((πx)/2)ex / sqrt(2)$\sqrt{\frac{\pi x}{2}}{e}^{-x/\sqrt{2}}$ which implies a strong attenuation of error. Eventually, kerx$\mathrm{ker}x$, which asymptotically behaves like sqrt(π/(2x))ex / sqrt(2)$\sqrt{\frac{\pi }{2x}}{e}^{-x/\sqrt{2}}$, becomes so small that it cannot be calculated without causing underflow, and the function returns zero. Note that for large x$x$ the errors are dominated by those of the standard function exp.

Underflow may occur for a few values of x$x$ close to the zeros of kerx$\mathrm{ker}x$, below the limit which causes a failure with ${\mathbf{ifail}}={\mathbf{1}}$.

## Example

```function nag_specfun_kelvin_ker_vector_example
x = [0.1; 1; 2.5; 5; 10; 15];
[f, ivalid, ifail] = nag_specfun_kelvin_ker_vector(x);
fprintf('\n    X           Y\n');
for i=1:numel(x)
fprintf('%12.3e%12.3e%5d\n', x(i), f(i), ivalid(i));
end
```
```

X           Y
1.000e-01   2.420e+00    0
1.000e+00   2.867e-01    0
2.500e+00  -6.969e-02    0
5.000e+00  -1.151e-02    0
1.000e+01   1.295e-04    0
1.500e+01  -1.514e-08    0

```
```function s19aq_example
x = [0.1; 1; 2.5; 5; 10; 15];
[f, ivalid, ifail] = s19aq(x);
fprintf('\n    X           Y\n');
for i=1:numel(x)
fprintf('%12.3e%12.3e%5d\n', x(i), f(i), ivalid(i));
end
```
```

X           Y
1.000e-01   2.420e+00    0
1.000e+00   2.867e-01    0
2.500e+00  -6.969e-02    0
5.000e+00  -1.151e-02    0
1.000e+01   1.295e-04    0
1.500e+01  -1.514e-08    0

```