gnuplot / fractal / recursive
関数の再帰定義 gnuplotでは関数を再帰的に定義することができます.再帰的定義とは, gnuplot> fac(n) = n * fac(n-1) この1行で変数Nを1つづ減少させるloopのようになりますが,このままで gnuplot> fac(n) = (n==0) ? 1 : n * fac(n-1) 等号右側の(n==0)が条件式で,Nが0に等しいときに真になり, N!を計算するには,Nが整数でなければいけません.これにはgnuplotの関 gnuplot> fac(x) = (int(x)==0) ? 1.0 : int(x) * fac(int(x)-1.0) 関数fac(x)の計算結果は,次のようになりました. gnuplot> fac(x) = (int(x)==0) ? 1.0 : int(x) * fac(int(x)-1.0) gnuplot> print fact(1) 1.0 gnuplot> print fact(5) 120.0 gnuplot> print fact(5.5) 120.0 gnuplot> print fact(20) 2.43290200817664e+18 N!の近似値はStirlingの公式で計算できることが知られています. ![]() 上で定義した関数fac(x)とStirlingの公式の結果を比較してみましょう. gnuplot> stirling(x) = sqrt(2*pi*x) * x**x * exp(-x) gnuplot> set xrange [1:15] gnuplot> set yrange [1:1e+10] gnuplot> set log y gnuplot> set sample 15 gnuplot> plot stirling(x) notitle with lines, > fact(x) notitle with points ![]() |