I wanted to give a LuaLaTeX answer to this question
Here it is the listing I wrote:
% !TEX encoding = UTF-8
% !TEX program = LuaLaTeX
% !TEX spellcheck = en_GB
\documentclass{standalone}
\usepackage{pgfplots}
\usepackage{luacode}
\begin{luacode*}
-- Code taken by
-- http://rosettacode.org/wiki/Gamma_function#Lua
function recigamma(z)
local gamma = 0.577215664901
local coeff = -0.65587807152056
local quad = -0.042002635033944
local qui = 0.16653861138228
local set = -0.042197734555571
return z+gamma*z^2+coeff*z^3+quad*z^4+qui*z^5+set*z^6
end
function gamma(z)
if z == 1 then
return 1
elseif math.abs(z) <= 0.5 then
return 1/recigamma(z)
else
return (z-1)*gamma(z-1)
end
end
-- From Wikipedia
-- http://en.wikipedia.org/wiki/Chi-squared_distribution
function chi_PF(x,k)
if x<0 then
return 0
else
return (1/(2^(k/2)*gamma(k/2)))*x^(k/2-1)*math.exp(-x/2)
end
end
-- Code to write PGFplots data as coordinates
function printData(xMin,xMax,n,k)
local delta = (xMax-xMin)/(n-1)
local x = xMin
--tex.sprint("{")
for i=1,n do
y = chi_PF(x,k)
tex.print("("..x..","..y..")\n")
x = x+delta
end
--tex.sprint("}")
end
\end{luacode*}
\def\printData#1#2#3#4{\directlua{printData(#1,#2,#3,#4)}}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot coordinates{\printData{0}{10}{10}{3}};
\end{axis}
\end{tikzpicture}
\end{document}
But the error encountered is:
! Package pgfplots Error: Sorry, I could not read the plot coordinates near '(0,0)
(1.1111111111111,0.2412971123005)
(2.2222222222222,0.19579085829747)
(3.3333333333333,0.13758254145982)
(4.4444444444444,0.091150275097439)
(5.5555555555556,0.058470697966872)
(6.6666666666667,0.036749733066543)
(7.7777777777778,0.022774710835611)
(8.8888888888889,0.013969283552714)
(10,0.0085011109038277)
'. Please check for format mistakes..
Why? How can I avoid it?