1

I'm trying to plot a 3d graph for function x=y^2 + 4z^2, but it keeps giving me error message, saying float number unreadable, the picture should look like elliptic paraboloid as the picture I attached......here is the code I have (BTW, I totally ripped off the code from pgfplot: 3d function plot Thanks for the brilliant post and answers):

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
    declare function = {
        X(\y,\z) = \y^2 + 4\z^2;
    }
]
\begin{axis}
[
axis lines=center,
enlargelimits,
tick align=inside,
domain=-1.00:1.00,
samples=20, % this was 200, but I changed it to 20 because of my slow PC
minor tick num=5,
]
\addplot3 [surf] {X(y,z)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • 2
    Try X(\y,\x) = \y^2 + 4\x^2; and \addplot3 [surf] {X(y,x)}; instead. Your original declaration X(\y,\z) = \y^2 + 4\x^2; does not make too much sense since the left-hand side depends on \y and \z whereas the right-hand side depends on \y and \x. – user121799 Feb 26 '18 at 20:08
4

Answer to the original question: The \x or \z are just place-holders, you could replace them by \funnylittlefurball if you wanted. But they need to be consistent on the left and right-hand side of the equation, and there was also a multiplication sign missing. Further, pgfplots expects the variables (i.e. placeholders to be x and y.

Answer to the revised question and question in comments: pgfplots can do two things for you: either plot a function of x and y or make a parametric plot. Your equation x=y^2+4z^2 defines for each fixed x an ellipse. You can easily confirm that y=\sqrt{x}\,\cos(t) and z=0.5*\sqrt{x}\,\sin(t) (in LaTeX notation) solve your equation. (Thanks for pointing this out!) Now, as pgfplots expects the parameters to be x and y, I just rename t to y and arrive at

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}
\begin{tikzpicture}[
    declare function = {
        X(\y,\z) = \y^2 + 4*\z^2;
    }
]
\begin{axis}
[view={135}{45},%colormap/blackwhite,
axis lines=center, axis on top,ticks=none,
set layers=default,
xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
xlabel style={anchor=south east},
ylabel style={anchor=south west},
zlabel style={anchor=south west},
enlargelimits,
tick align=inside,
domain=0:2.00,
samples=20, % this was 200, but I changed it to 20 because of my slow PC
%minor tick num=5,
z buffer=sort,
]
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=-180:0] ({x},{cos(y)*sqrt(x)},{0.5*sin(y)*sqrt(x)});
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=0:180,on layer=axis foreground] ({x},{cos(y)*sqrt(x)},{0.5*sin(y)*sqrt(x)});
\end{axis}
\end{tikzpicture}
\end{document}

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[axis lines=center, axis on top,ticks=none,
set layers=default,
xlabel={$x$}, ylabel={$y$}, zlabel={$z$},
xlabel style={anchor=south west},
ylabel style={anchor=south west},
zlabel style={anchor=south west},
domain=0:2.00,
samples=20,
z buffer=sort,
]
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=-180:0] ({x},{2*cos(y)*sqrt(x)},{sin(y)*sqrt(x)});
\addplot3 [surf,shader=interp,opacity=0.8,
domain y=0:180,on layer=axis foreground] ({x},{2*cos(y)*sqrt(x)},{sin(y)*sqrt(x)});
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Here I adjusted the view to reproduce your picture more closely, made the plot smoother using the shader=interp directive (a matter of taste) and perhaps more importantly used the methods of this answer to achieve better 3D feel. I also inserted \pgfplotsset{compat=1.15} in order to achieve proper placement of the axis labels (and was using the tips of this answer).

| improve this answer | |
  • I need z to be plotted too, it should look like ellipic paraboloid – user3390471 Feb 26 '18 at 20:49
  • @user3390471 What is an elliptic paraboloid? If you provide the defining equation, than people may help you. – user121799 Feb 26 '18 at 20:56
  • @user3390471 I did providing defining equation – user3390471 Feb 26 '18 at 21:00
  • Can you explain how do you get this: {2*cos(y)*sqrt(x)},{sin(y)*sqrt(x)}??? and the graph should be along x-axis, not y-axis – user3390471 Feb 26 '18 at 22:35
  • @user3390471 I tried to explain what I did in more detail. Could you please tell me if you understand the explanation? – user121799 Feb 26 '18 at 23:14
4

You are giving z as a parameter to the function, but apparently pgfplots does not accept this. You should change X(y,z) into X(x,y).

Also, you are missing a multiplication sign in your function declaration. It should be:

declare function = {%  ↓ Multiplication sign here
    X(\y,\z) = \y^2 + 4*\x^2;
}

After that it should work:

enter image description here

Note, however, that your function's parameters are \y and \z, but it is computed with \x and \y. So I think it should be:

declare function = {%  ↓ Multiplication sign here
    X(\x,\y) = \y^2 + 4*\x^2;
}

If I'm wrong, ignore this part of the answer.

Full code:

\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[
    declare function = {
        X(\x,\y) = \y^2 + 4*\x^2;
    }
]
\begin{axis}
[
axis lines=center,
enlargelimits,
tick align=inside,
domain=-1.00:1.00,
samples=20, % this was 200, but I changed it to 20 because of my slow PC
minor tick num=5,
]
\addplot3 [surf] {X(x,y)};
\end{axis}
\end{tikzpicture}
\end{document}

Output:

enter image description here

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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