Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting factorial of x function in gnuplot?

I'm trying to plot the x! function in gnuplot, I defined first the factorial function and then called it, but I'm always getting stack overflow, no matter what bounds I set to the graph.

This what I've tried:

gnuplot>  fac(n) = (n==0) ? 1 : n * fac(n-1)  
gnuplot> plot [0:10][0:10] log(fac(x)), log(x**x)  
stack overflow
like image 318
diegoaguilar Avatar asked Sep 04 '13 05:09

diegoaguilar


2 Answers

I should define the fac function to be integer. Otherwise, when plotting all along the x-axis, it'll evaluate a long range of real numbers, a huge one actually, which will always cause the overflow.

So, the function should be defined:

fac(x) = (int(x)==0) ? 1.0 : int(x) * fac(int(x)-1.0)
like image 70
diegoaguilar Avatar answered Sep 28 '22 20:09

diegoaguilar


Another soultion would be to use the Gamma function (intrinsic function in gnuplot).

N!=gamma(N+1)

gnuplot> plot [0:10][0:10] log(gamma(x+1)), log(x**x)
like image 39
Alasanid Avatar answered Sep 28 '22 19:09

Alasanid