## 1Purpose

s18adf returns the value of the modified Bessel function ${K}_{1}\left(x\right)$, via the function name.

## 2Specification

Fortran Interface
 Real (Kind=nag_wp) :: s18adf Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x
#include <nag.h>
 double s18adf_ (const double *x, Integer *ifail)
The routine may be called by the names s18adf or nagf_specfun_bessel_k1_real.

## 3Description

s18adf evaluates an approximation to the modified Bessel function of the second kind ${K}_{1}\left(x\right)$.
Note:  ${K}_{1}\left(x\right)$ is undefined for $x\le 0$ and the routine will fail for such arguments.
The routine is based on five Chebyshev expansions:
For $0,
 $K1x=1x+xln⁡x∑′r=0arTrt-x∑′r=0brTrt, where ​ t=2x2-1.$
For $1,
 $K1x=e-x∑′r=0crTrt, where ​t=2x-3.$
For $2,
 $K1x=e-x∑′r=0drTrt, where ​t=x-3.$
For $x>4$,
 $K1x=e-xx ∑′r=0erTrt, where ​t=9-x 1+x .$
For $x$ near zero, ${K}_{1}\left(x\right)\simeq \frac{1}{x}$. This approximation is used when $x$ is sufficiently small for the result to be correct to machine precision. For very small $x$ on some machines, it is impossible to calculate $\frac{1}{x}$ without overflow and the routine must fail.
For large $x$, where there is a danger of underflow due to the smallness of ${K}_{1}$, the result is set exactly to zero.
NIST Digital Library of Mathematical Functions

## 5Arguments

1: $\mathbf{x}$Real (Kind=nag_wp) Input
On entry: the argument $x$ of the function.
Constraint: ${\mathbf{x}}>0.0$.
2: $\mathbf{ifail}$Integer Input/Output
On entry: ifail must be set to $0$, . If you are unfamiliar with this argument you should refer to Section 4 in the Introduction to the NAG Library FL Interface for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6Error Indicators and Warnings

If on entry ${\mathbf{ifail}}=0$ or $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}>0.0$.
${K}_{0}$ is undefined and the function returns zero.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{x}}=〈\mathit{\text{value}}〉$.
Constraint: ${\mathbf{x}}>〈\mathit{\text{value}}〉$.
x is too small, there is a danger of overflow and the function returns approximately the largest representable value.
${\mathbf{ifail}}=-99$
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.

## 7Accuracy

Let $\delta$ and $\epsilon$ be the relative errors in the argument and result respectively.
If $\delta$ is somewhat larger than the machine precision (i.e., if $\delta$ is due to data errors etc.), then $\epsilon$ and $\delta$ are approximately related by:
 $ε≃ x K0x- K1x K1x δ.$
Figure 1 shows the behaviour of the error amplification factor
 $xK0x - K1 x K1x .$
However, if $\delta$ is of the same order as the machine precision, then rounding errors could make $\epsilon$ slightly larger than the above relation predicts.
For small $x$, $\epsilon \simeq \delta$ and there is no amplification of errors.
For large $x$, $\epsilon \simeq x\delta$ and we have strong amplification of the relative error. Eventually ${K}_{1}$, which is asymptotically given by $\frac{{e}^{-x}}{\sqrt{x}}$, becomes so small that it cannot be calculated without underflow and hence the routine will return zero. Note that for large $x$ the errors will be dominated by those of the standard function exp. Figure 1

None.

## 10Example

This example reads values of the argument $x$ from a file, evaluates the function at each value of $x$ and prints the results.
{"ft_lang_label":"__label__en","ft_lang_prob":0.884071,"math_prob":0.9984751,"size":3159,"snap":"2021-31-2021-39","text_gpt3_token_len":655,"char_repetition_ratio":0.120126784,"word_repetition_ratio":0.073033705,"special_character_ratio":0.20386198,"punctuation_ratio":0.12478921,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9997321,"pos_list":[0,1,2],"im_url_duplicate_count":[null,4,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-30T05:15:45Z\",\"WARC-Record-ID\":\"<urn:uuid:6860e5bb-46c1-4779-96d7-e63f163227b1>\",\"Content-Length\":\"40274\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5f47c8b2-a0f5-4deb-abb2-dbfdf780b538>\",\"WARC-Concurrent-To\":\"<urn:uuid:7523bcab-def3-41b0-85d3-d9331dff7da4>\",\"WARC-IP-Address\":\"78.129.168.4\",\"WARC-Target-URI\":\"https://www.nag.com/numeric/nl/nagdoc_27/flhtml/s/s18adf.html\",\"WARC-Payload-Digest\":\"sha1:335DRURFGSQ2LUQFKT64T2OM3QMYH6QG\",\"WARC-Block-Digest\":\"sha1:GVDMH4GRRSMKHRRBPWQR3CXXPYVE76PK\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046153931.11_warc_CC-MAIN-20210730025356-20210730055356-00101.warc.gz\"}"}