
pgfplots 명령을 사용하여 재귀 함수를 그릴 수 있습니까 \addplot
?
나는 이렇게 시도했다;
\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}
결과는 다음과 같습니다.