Anda di halaman 1dari 4

Stirlings Approximation

Lab 2
PHYS 344
Siobhan Morris
September 30, 2015
Instructor:

Professor Karine Lebris

Abstract
Many fields in mathematics and physics require use and calculation of large numbers
like N !, where N may be on the order of 1023 or higher. Computation of numbers of this
scale often takes up a large amount of time. Stirlings approximation offers a quicker route
for accurately approximating numbers of this scale, and as such serves a useful purpose in
these fields. We evaluate Stirlings approximation (including and excluding various orders
of terms) in order to determine the level of accuracy it offers for various values of N. We
find that at lower values of N, Stirlings approximation tends to be a less accurate choice
than simply evaluating the factorial as is. However, at high values of N the approximation
serves us quite well and offers faster computation time as well as a more robust handling
of large values.

Introduction

In mathematics, Stirlings approximation is a powerful approximation for factorials. It leads


to very accurate results, even for small values of N. Within the field of statistical mechanics,
oftentimes it is necessary to deal with very large numbers like N!, where N may be of a very
large order. In this case, it is incredibly useful to have an accurate approximation which allows
for the calculation of numbers like N!.
Stirlings approximation is given by:


N ! = N N eN 2N 1 +


1
+ ...
12N

(1)

As Stirlings approximation plays such an important role in many large-scale calculations that
happen in various scientific fields, in this lab we will test this approximation to observe its
strengths and limitations.

Numerical Procedure(Theory/Methods)

In order to compare Stirlings approximation, we first code our own n f act function, which
calculates N ! forthright.
N ! = N (N 1) (N 2) (N 3) . . . 3 2 1

(2)

The code for this function may be found on page 2 of the attached files.
We then also code a Stirling function using expression(3) from the lab manual (found on page
4).



1
N ! = N N eN 2N 1 +
+ ...
(3)
12N
These two functions are compared for various values of N, to quantify how well Stirlings
approximation does - including when it becomes accurate, and whether it becomes a better
approximation for large N .
When dealing with particularly large values of N , we make use of lnN ! in order to adequately
evaluate and compare the two functions. For these purposes, we script a n f act ln(N ) function
to compute lnN !, and make use of this function when using values that are too large for our
n f act or Stirling functions to handle. This function can be found on page 3 of the attached
functions.

1
Finally, we evaluate how dropping the
and subsequently the 2N term from Stirlings
12N
approximation expression affect the accuracy of the approximation. We present these results
in a graph to be discussed in the section below.

Results and Discussion

We take our two functions, n fact and Stirling, and test these functions for various values of N.
These values are compared in the table below:
Table 1: Comparison of MATLAB nfact and Stirling functions
Input value
3
6
8
10
15
20
100
500

n fact
3
720
40320
3628800
1.3077e+12
2.4329e+18
9.3326e+157
Inf

Stirling
5.9983
719.9404
4.031e+04
3.6287e+06
1.3077e+12
2.4329e+18
9.3326e+157
Inf

It is clear that for smaller N, the factorial function gives more accurate results than the Stirling
function. However, it appears that from N=15 and onward, Stirlings approximation gives the
same result as our factorial function, and in faster time.
Interestingly enough, the Stirling function fails to handle values higher than N = 143, whereas
the factorial function can handle values up to N = 170. Above both these values, we must
resort to comparing ln(N !) rather than N !. These results are summarized in the table below:
Table 2: Large values of N
Input value n fact
1000
Inf
500
Inf
200
Inf
150
5.7124e+284
170
4.7147e+284
171
Inf
143

144

145

for nfact and Stirling functions


Stirling
n fact ln(N)
Nan

Inf

Inf

Inf

711.7147
3.8544e+247
Inf

580.0343

We then compare various forms of Stirlings function with our regular factorial function N! on
a logarithmic scale. This graph can be found on page one of the attached MATLAB code. Four
different functions are compared:
a. N!


b. Stirling : N ! = N N eN 2N 1 +


1
+ ...
12N

c. Stirling2 : N ! = N N eN 2N
d. Stirling3: N ! = N N eN
As can be seen from the graph, Stirling3 appears to be quite sufficient when N is very large.
However, the Stirling function in its entirety (with no terms dropped) does slightly better at
properly modelling N! for a greater range of values. Interesting to note is the fact that our N!,
coded in lab, diverges significantly from our approximations at larger values of N. If we had
used the implicit function f actorial in MATLAB, we would have perhaps observed different
results here.

Conclusion

In this lab, we have determined the strengths and limitations of Stirlings approximation for
both small and large N. Through comparison of Stirlings approximation and the factorial
function, we can observe how accurate the approximation
remains to varying degrees.

While N ! = N ! = N N eN and N ! = N ! = N N eN 2N remain sufficient approximations for





1
very large N , the expression N ! = N N eN 2N 1 +
+ ... in its entirely serves as the
12N
best possible approximation for N ! at values exceeding N=15, being able to handle both larger
values, and in faster time.

References

Schroeder, D. (2000). An introduction to thermal physics. San Francisco, CA: Addison Wesley.
Lebris, K. (2015). PHYS 344 Thermal Physics Laboratory Manual. Antigonish, NS: StFX.

Anda mungkin juga menyukai