
\addplot
pgfplotsコマンドを使用して再帰関数をプロットすることは可能ですか?
私はこのように試しました;
\documentclass{article}
\usepackage{pgfplots}
\pgfmathdeclarefunction{fac}{1}{
\pgfmathparse{(#1<=1)+
(#1>1)*#1*fac(#1-1)
}
}
\begin{document}
fac(5) is \pgfmathparse{fac(5)}\pgfmathresult
\end{document}
しかし、関数全体が評価されるため、結果は無限再帰になるようです。
答え1
最終的には、Heiko Oberdiek の提案に従って、値を外部で計算することにしました (この場合は、LuaLatex 内で luacode を使用します)。
\documentclass{article}
\usepackage{pgfplots}
\usepackage{luacode}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\begin{luacode*}
function fib(n)
local function inner(m)
if m < 2 then
return m
end
return inner(m-1) + inner(m-2)
end
return inner(n)
end
local points = "";
for i=0,15,1 do
points = points .. " (" .. i .. "," .. fib(i) .. ")";
end
tex.print("\\addplot [mark=none] coordinates { " .. points .. "};");
\end{luacode*}
\end{axis}
\end{tikzpicture}
\end{document}
その結果は次のようになります: