4
$\begingroup$

I want to plot to show how accurate is Stirling approximation in Mathematica by plotting the Percentage error against n. I want to take the value of n at the interval of 10 starting from 10 to 1000. So, I basically used the Listplot function to plot all these datas, which has been rather tedious. Is there any way simpler method to plot this approximation $\ln n!=n\ln n-n$

$\endgroup$

2 Answers 2

8
$\begingroup$

Although n is allowed to take non integer values, you probably intend it to take only integer values, so this is a job for DiscretePlot

DiscretePlot[Log[n!]/(n Log[n] - n), {n, 10, 1000, 10}, 
      PlotRange -> All, Frame -> True]

Mathematica graphics

$\endgroup$
4
  • $\begingroup$ As an addendum, you can see how fast the relative error tends to zero with Simplify[Series[1 - Log[n!]/(n Log[n] - n), {n, \[Infinity], 4}], n > 0] $\endgroup$
    – Greg Hurst
    Nov 27, 2014 at 22:16
  • $\begingroup$ Can we manipulate the plot by using manipulate function ? $\endgroup$ Nov 27, 2014 at 23:22
  • $\begingroup$ In principle, yes, but I don't see a useful free parameter to manipulate here. $\endgroup$ Nov 27, 2014 at 23:33
  • 1
    $\begingroup$ There is no reason to restrict n to be an integer so you could also use Plot. Manipulate[Plot[Log[n!]/(n Log[n] - n), {n, nmin, nmax}, PlotRange -> All, Axes -> False, Frame -> True], {{nmin, 10}, 10, 999, 1, Appearance -> "Labeled"}, {{nmax, 1000}, nmin + 1, 1000, 1, Appearance -> "Labeled"}] $\endgroup$
    – Bob Hanlon
    Nov 27, 2014 at 23:40
5
$\begingroup$
Plot[Log[n!] - (n Log[n] - n), {n, 1, 1000}]

enter image description here

shows the error between the two functions. You can divide by n to get percentage error...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.