5.17 The gamma function: A commonly occurring function in physics calculations
is the gamma function \(\Gamma(a)\), which is defined by the integral
$$
\Gamma(a)=\int_{0}^{\infty} x^{n-1} \mathrm{e}^{-x} \mathrm{~d} x .
$$
There is no closed-form expression for the gamma function, but one can
calculate its value for given \(a\) by performing the integral above
numerically. You have to be careful how you do it, however, if you wish to get
an accurate answer.
a) Write a program to make a graph of the value of the integrand \(x^{n-1}
\mathrm{e}^{-x}\) as a function of \(x\) from \(x=0\) to \(x=5\), with three separate
curves for \(a=2,3\), and 4 , all on the same axes. You should find that the
integrand starts at zero, rises to a maximum, and then decays again for each
curve.
b) Show analytically that the maximum falls at \(x=a-1\).
c) Most of the area under the integrand falls near the maximum, so to get an
accurate value of the gamma function we need to do a good job of this part of
the integral. We can change the integral from 0 to \(\infty\) to one over a
finite range from 0 to 1 using the change of variables in Eq. (5.67), but this
tends to squash the peak towards the edge of the \([0,1]\) range and does a poor
job of evaluating the integral accurately. We can do a better job by making a
different change of variables that puts the peak in the middle of the
integration range, around \(\frac{1}{2}\). We will use the change of variables
given in Eq. (5.69), which we repeat here for convenience:
$$
z=\frac{x}{c+x} .
$$
For what value of \(x\) does this change of variables give \(z=\frac{1}{2}\) ?
Hence what is the appropriate choice of the parameter \(c\) that puts the peak
of the integrand for the gamma function at \(z=\frac{1}{2}\) ?
d) Before we can calculate the gamma function, there is another detail we need
to attend to. The integrand \(x^{n-1} \mathrm{e}^{-x}\) can be difficult to
evaluate because the factor \(x^{2-1}\) can become very large and the factor
\(\mathrm{e}^{-x}\) very small, causing numerical overflow or underflow, or
both, for some values of \(x\). Write \(x^{n-1}=\mathrm{e}^{(a-1) \ln x}\) to
derive an alternative expression for the integrand that does not suffer from
these problems (or at least not so much). Explain why your new expression is
better than the old one.
e) Now, using the change of variables above and the value of \(c\) you have
chosen, write a user-defined function gamma (a) to calculate the gamma
function for arbitrary argument \(a\). Use whatever integration method you feel
is appropriate. Test your function by using it to calculate and print the
value of \(\Gamma\left(\frac{3}{2}\right)\), which is known to be equal to
\(\frac{1}{2} \sqrt{\pi} \simeq 0.886\).
f) For integer values of \(a\) it can be shown that \(\Gamma(a)\) is equal to the
factorial of \(a-\) 1. Use your Python function to calculate \(\Gamma(3),
\Gamma(6)\), and \(\Gamma(10)\). You should get answers closely equal to \(2 !=2,5
!=120\), and \(9 !=362880\).