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_kei_vector (s19ar)

## Purpose

nag_specfun_kelvin_kei_vector (s19ar) returns an array of values for the Kelvin function keix$\mathrm{kei}x$.

## Syntax

[f, ivalid, ifail] = s19ar(x, 'n', n)
[f, ivalid, ifail] = nag_specfun_kelvin_kei_vector(x, 'n', n)

## Description

nag_specfun_kelvin_kei_vector (s19ar) evaluates an approximation to the Kelvin function keixi$\mathrm{kei}{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, so we need only consider x0$x\ge 0$.
The function is based on several Chebyshev expansions:
For 0x1$0\le x\le 1$,
 keix = − π/4f(t) + (x2)/4[ − g(t)log(x) + v(t)] $kei⁡x=-π4f(t)+x24[-g(t)log(x)+v(t)]$
where f(t)$f\left(t\right)$, g(t)$g\left(t\right)$ and v(t)$v\left(t\right)$ are expansions in the variable t = 2x41$t=2{x}^{4}-1$;
For 1 < x3$1,
 keix = exp( − (9/8)x) u(t) $kei⁡x=exp(-98x) u(t)$
where u(t)$u\left(t\right)$ is an expansion in the variable t = x2$t=x-2$;
For x > 3$x>3$,
 keix = sqrt(π/(2x))e − x / sqrt(2) [(1 + 1/x)c(t)sinβ + 1/xd(t)cosβ] $kei⁡x=π 2x e-x/2 [ (1+1x) c(t)sin⁡β+1xd(t)cos⁡β]$
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$.
For x < 0$x<0$, the function is undefined, and hence the function fails and returns zero.
When x$x$ is sufficiently close to zero, the result is computed as
 keix = − π/4 + (1 − γ − log(x/2)) (x2)/4 $kei⁡x=-π4+(1-γ-log(x2) ) x24$
and when x$x$ is even closer to zero simply as
 keix = − π/4. $kei⁡x=-π4.$
For large x$x$, keix$\mathrm{kei}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)\ge 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
keixi$\mathrm{kei}{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_kei (s19ad), as defined in the Users' Note for your implementation.
ivalid(i) = 2${\mathbf{ivalid}}\left(i\right)=2$
xi < 0.0${x}_{i}<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, and δ$\delta$ be the relative error in the argument. If δ$\delta$ is somewhat larger than the machine representation error, then we have:
 E ≃ |x/(sqrt(2))( − ker1x + kei1x)|δ. $E≃ | x2 ( - ker1⁡x+ kei1⁡x ) |δ.$
For small x$x$, errors are attenuated 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 absolute accuracy of 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, keix$\mathrm{kei}x$, which is asymptotically given by 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 therefore 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 keix$\mathrm{kei}x$, below the limit which causes a failure with ${\mathbf{ifail}}={\mathbf{1}}$.

## Example

```function nag_specfun_kelvin_kei_vector_example
x = [0; 0.1; 1; 2.5; 5; 10; 15];
[f, ivalid, ifail] = nag_specfun_kelvin_kei_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
0.000e+00  -7.854e-01    0
1.000e-01  -7.769e-01    0
1.000e+00  -4.950e-01    0
2.500e+00  -1.107e-01    0
5.000e+00   1.119e-02    0
1.000e+01  -3.075e-04    0
1.500e+01   7.963e-06    0

```
```function s19ar_example
x = [0; 0.1; 1; 2.5; 5; 10; 15];
[f, ivalid, ifail] = s19ar(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
0.000e+00  -7.854e-01    0
1.000e-01  -7.769e-01    0
1.000e+00  -4.950e-01    0
2.500e+00  -1.107e-01    0
5.000e+00   1.119e-02    0
1.000e+01  -3.075e-04    0
1.500e+01   7.963e-06    0

```