# pgfplot: 3d function plot

After my previous question, I want to plot a 3d surface; as a simple function I tried to render the bellow plot but I was not successful.

\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,
]
\end{axis}
\end{tikzpicture}
\end{document}

-
Is this really the function you want to plot? Or do you intend to plot the function from the previous question and the one given here is just a place holder? – quinmars Jul 30 '14 at 20:44
I tried it as a sample, my functions are bigger than these and they are different from my previous question. – Enthusiastic Student Jul 30 '14 at 20:45

Use an upper case z for your function, i.e, Z and all is good. I'd also recommend reducing your samples, at least until you know that it works - using samples=200 in this context is quite a lot more memory intensive than in the two-dimensional case!

Here's the output with 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,
]
\end{axis}
\end{tikzpicture}
\end{document}

-
is it case sensitive? – Enthusiastic Student Jul 30 '14 at 21:06
@EnthusiasticStudent I believe that z means something in-built for pgfplots/tikz :) – cmhughes Jul 30 '14 at 21:08
should q function also be capital or not? I mean the secondary functions. Are they also case-sensitive? – Enthusiastic Student Jul 30 '14 at 21:09
@EnthusiasticStudent q will be treated differently from Q, but both seem ok - i.e, neither one is used inherently by pgfplots /tikz – cmhughes Jul 31 '14 at 21:00

Here is a version, which does not rely on lualatex. You have to see if you get with the actual function into the same troubles as in your previous question.

% !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,
]