Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
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 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 at 20:45

2 Answers 2

up vote 4 down vote accepted

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}
share|improve this answer
    
is it case sensitive? –  Enthusiastic Student Jul 30 at 21:06
    
@EnthusiasticStudent I believe that z means something in-built for pgfplots/tikz :) –  cmhughes Jul 30 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 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 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,
    ]
    \addplot3 [surf] {Z(x,y)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

share|improve this answer
    
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 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 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 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 at 21:38
1  
The pgfplots manual is good resource. You are wellcome. –  quinmars Jul 30 at 21:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.