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_gamma_incomplete (s14ba)

## Purpose

nag_specfun_gamma_incomplete (s14ba) computes values for the incomplete gamma functions $P\left(a,x\right)$ and $Q\left(a,x\right)$.

## Syntax

[p, q, ifail] = s14ba(a, x, tol)
[p, q, ifail] = nag_specfun_gamma_incomplete(a, x, tol)

## Description

nag_specfun_gamma_incomplete (s14ba) evaluates the incomplete gamma functions in the normalized form
 $Pa,x=1Γa ∫0xta-1e-tdt,$
 $Qa,x=1Γ a ∫x∞ta- 1e-t dt,$
with $x\ge 0$ and $a>0$, to a user-specified accuracy. With this normalization, $P\left(a,x\right)+Q\left(a,x\right)=1$.
Several methods are used to evaluate the functions depending on the arguments $a$ and $x$, the methods including Taylor expansion for $P\left(a,x\right)$, Legendre's continued fraction for $Q\left(a,x\right)$, and power series for $Q\left(a,x\right)$. When both $a$ and $x$ are large, and $a\simeq x$, the uniform asymptotic expansion of Temme (1987) is employed for greater efficiency – specifically, this expansion is used when $a\ge 20$ and $0.7a\le x\le 1.4a$.
Once either $P$ or $Q$ is computed, the other is obtained by subtraction from $1$. In order to avoid loss of relative precision in this subtraction, the smaller of $P$ and $Q$ is computed first.
This function is derived from the function GAM in Gautschi (1979b).

## References

Gautschi W (1979a) A computational procedure for incomplete gamma functions ACM Trans. Math. Software 5 466–481
Gautschi W (1979b) Algorithm 542: Incomplete gamma functions ACM Trans. Math. Software 5 482–489
Temme N M (1987) On the computation of the incomplete gamma functions for large values of the parameters Algorithms for Approximation (eds J C Mason and M G Cox) Oxford University Press

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{a}$ – double scalar
The argument $a$ of the functions.
Constraint: ${\mathbf{a}}>0.0$.
2:     $\mathrm{x}$ – double scalar
The argument $x$ of the functions.
Constraint: ${\mathbf{x}}\ge 0.0$.
3:     $\mathrm{tol}$ – double scalar
The relative accuracy required by you in the results. If nag_specfun_gamma_incomplete (s14ba) is entered with tol greater than $1.0$ or less than machine precision, then the value of machine precision is used instead.

None.

### Output Parameters

1:     $\mathrm{p}$ – double scalar
2:     $\mathrm{q}$ – double scalar
The values of the functions $P\left(a,x\right)$ and $Q\left(a,x\right)$ respectively.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{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:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{a}}\le 0.0$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{x}}<0.0$.
${\mathbf{ifail}}=3$
Convergence of the Taylor series or Legendre continued fraction fails within $600$ iterations. This error is extremely unlikely to occur; if it does, contact NAG.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.

## Accuracy

There are rare occasions when the relative accuracy attained is somewhat less than that specified by argument tol. However, the error should never exceed more than one or two decimal places. Note also that there is a limit of $18$ decimal places on the achievable accuracy, because constants in the function are given to this precision.

The time taken for a call of nag_specfun_gamma_incomplete (s14ba) depends on the precision requested through tol, and also varies slightly with the input arguments $a$ and $x$.

## Example

This example reads values of the argument $a$ and $x$ from a file, evaluates the function and prints the results.
```function s14ba_example

fprintf('s14ba example results\n\n');

a = [ 2  7   0.5  20  21];
x = [ 3  1  99.0  21  20];
n = size(x,2);
p = x;
q = x;
tol = x02aj;

for j=1:n
[p(j), q(j), ifail] = s14ba(a(j), x(j), tol);
end

disp('     a      x     P(a,x)    Q(a,x)');
fprintf('%7.1f%7.1f%10.4f%10.4f\n',[a; x; p; q]);

s14ba_plot;

function s14ba_plot
x = [0:0.5:20];
a = [0.1:0.1:0.4,0.5:0.5:20];
p = zeros(numel(a),numel(x));
q = p;

tol = x02aj;
for i=1:numel(a)
for j=1:numel(x)
[p(i,j), q(i,j), ifail] = s14ba(a(i), x(j), tol);
end
end

fig1 = figure;
hold on
mesh(x, a, p, 'FaceColor','r');
mesh(x, a, q, 'FaceColor','g');
xlabel('x');
ylabel('a');
title('Incomplete Gamma Functions');
legend('P(a,x)','Q(a,x)');
view(-35, 46);
hold off;

```
```s14ba example results

a      x     P(a,x)    Q(a,x)
2.0    3.0    0.8009    0.1991
7.0    1.0    0.0001    0.9999
0.5   99.0    1.0000    0.0000
20.0   21.0    0.6157    0.3843
21.0   20.0    0.4409    0.5591
``` 