3

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,
    ]
    \addplot3 [surf] {z(x,y)};
    \end{axis}
\end{tikzpicture}
\end{document}
enthu
  • 3,795
  • 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. – enthu Jul 30 '14 at 20:45

2 Answers2

5

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

screenshot

% 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}
cmhughes
  • 100,947
5

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,
    ]
    \addplot3 [surf] {Z(x,y)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

quinmars
  • 5,477
  • 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? – enthu 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. – enthu 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
  • I really like that I am writing my thesis with TeX. And I am not using/choosing packages with conscious (I mean, for example, a package's output was great and chose it). Now, the only way I know to produce my plots is that separate process... (poor me :)) ) but I really like to become professional in TeX (although I don't know how). By the way, thank you very much. You helped me indeed. – enthu Jul 30 '14 at 21:47
  • 1
    The pgfplots manual is good resource. You are wellcome. – quinmars Jul 30 '14 at 21:56