后我之前的问题,我想绘制一个 3d 曲面;作为一个简单的函数,我试图渲染下面的图,但没有成功。
\documentclass{standalone}
\usepackage{blindtext}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\directlua{
q = function(x)
return x-1
end
z = function(x,y)
return x^2+y^2+q(x)
end
}
\pgfmathdeclarefunction{z}{2}{%
\edef\pgfmathresult{\directlua{tex.print(z(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}}%
}%
\begin{axis}
[
axis lines=center,
enlargelimits,
tick align=inside,
domain=-1:1,
samples=200,
minor tick num=5,
]
\addplot3 [surf] {z(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
z
为函数使用大写字母,即,Z
一切正常。我还建议减少你的samples
,至少直到你知道它有效为止 -samples=200
在这种情况下使用比在二维情况下占用更多的内存!
以下是输出samples=30
% arara: lualatex
\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}
\directlua{
q = function(x)
return x-1
end
Z = function(x,y)
return x^2+y^2+q(x)
end
}
\pgfmathdeclarefunction{Z}{2}{%
\edef\pgfmathresult{\directlua{tex.print(Z(\pgfmathfloatvalueof{#1},\pgfmathfloatvalueof{#2}))}}%
}%
\begin{axis}
[
axis lines=center,
enlargelimits,
tick align=inside,
domain=-1:1,
samples=30,
minor tick num=5,
]
\addplot3 [surf] {Z(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
这是一个不依赖于的版本lualatex
。您必须查看实际函数是否与您上一个问题中遇到的相同麻烦。
% !TeX program=pdflatex
\documentclass{standalone}
\usepackage{blindtext}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[
declare function = {
q(\x) = \x - 1;
Z(\x,\y) = \x^2 + \y^2 + q(\x);
}
]
\begin{axis}
[
axis lines=center,
enlargelimits,
tick align=inside,
domain=-1:1,
samples=20, % this was 200, but I changed it to 20 because of my slow PC
minor tick num=5,
]
\addplot3 [surf] {Z(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}