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


-
Sorry, I am too basic. What was the use of Lua in the previous question. You have plotted the function without any trouble here. Is using Lua a "must" in plotting functions? I mean, couldn't I plot the previous question with your method too? – Enthusiastic Student Jul 30 '14 at 21:15
No, in general you do not need lua. The pgf math library is very good. In your special case in the previous question, there seems to be an accuracy problem. I guess it is something like subtracting very large values, which differes only minimal. Here accuracy becomes very important. BUT this is the first time I read about those problems with pgfmath, so it's not a problem you face often. – quinmars Jul 30 '14 at 21:22
And one last question. In writing thesis or papers, is it normal to produce plots separately and then include them in the main file or they should be coded inline the main paper tex file? I am writing my thesis. I produce plots in separate tex files, and then include them in my main thesis tex file. – Enthusiastic Student Jul 30 '14 at 21:24
That are two questions :). For papers, you usally have to provide the figures as separated image files. Most journals do only provide a basic set of packages. For your thesis you can do it as you want. I prefer to have them inside of the thesis, because all definitions of colors and fonts, and package options are automatically shared. Downside is that the compilation takes some (long) time. – quinmars Jul 30 '14 at 21:38
The pgfplots manual is good resource. You are wellcome. – quinmars Jul 30 '14 at 21:56