5

Trying to plot a hyperboloid with domain x^2+y^2 <= 50. For now I have set the domain to [-5,5], but this yields ugly plots, see below. Any advice on how to get around this? I tried setting the domain in terms of x and y (and x domain in terms of y and vice versa), but to no avail.

Ideally I would like to apply the same solution to the gradient vector field, should there be one.

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\xdefinecolor{lightgrey}{RGB}{220,220,220}
\xdefinecolor{goldenrod}{RGB}{255,223,66}
\xdefinecolor{newblue}{RGB}{57,106,177}
\xdefinecolor{newred}{RGB}{204,37,41}
\xdefinecolor{newgreen}{RGB}{132,186,91}
\xdefinecolor{newpurple}{RGB}{144,103,167}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal, view={0}{90}]
\addplot3[surf,shader=interp,opacity=0.5,domain=-5:5]
{(1/100)(50-x^2-y^2)};
\addplot [domain=0:2*pi,samples=50]({cos(deg(x))},{sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({2*cos(deg(x))},{2*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({3*cos(deg(x))},{3*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({4*cos(deg(x))},{4*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({5*cos(deg(x))},{5*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({6*cos(deg(x))},{6*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({7*cos(deg(x))},{7*sin(deg(x))});
\addplot[newpurple,double=newpurple,->] plot coordinates {
    (0,0)
    (-1,3)
};
\addplot[newblue,double=newblue,->] plot coordinates {
    (0,0)
    (7,-1)
};
\addplot[newred,double=newred,->] plot coordinates {
    (7,-1)
    (-1,3)
};
\addplot+[newred,double=newred] plot coordinates {
    (1,2)
};
\addplot[newgreen,double=newgreen] plot coordinates {
    (-3,-6)
    (3,6)
};
\addplot3[blue,/pgfplots/quiver,
    quiver/u=-x/50,
    quiver/v=-y/50,
    quiver/scale arrows=0.1,
    -stealth,samples=10] {1};          
\end{axis}
\end{tikzpicture}
\end{document}

Plot

3
  • It makes the plot show up incorrectly (x^2+y^2 is allowed to grow to 98, distorting the "heat map"), so no.
    – Jay
    Feb 9, 2014 at 1:45
  • Please always post complete examples (starting from \documentclass) instead of snippets. It's no fun to first have to clean up the code (what's newblue, newred, etc.?) before one can start to work on a solution.
    – Jake
    Feb 10, 2014 at 16:36
  • Sorry, forgot about them! Will update!
    – Jay
    Feb 10, 2014 at 21:09

1 Answer 1

8
+50

You can use data cs=polar to provide the equations in polar coordinates. Also, you can use \foreach loops to simplify the code:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}


\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal image, view={0}{90}]
\addplot3 [
    surf,
    shader=interp,
    opacity=0.5,
    domain=0:360,
    y domain=0:7,
    samples=100, samples y=7,
    data cs=polar
] {(50-y^2)};

\foreach \i in {1,...,7}{
    \addplot [domain=0:360,samples=100, data cs=polar] {\i};
}

\draw [purple, ultra thick, ->] (axis cs:0,0) -- (axis cs:-1,3);
\draw [blue, ultra thick, ->] (axis cs:0,0) -- (axis cs:7,-1);
\draw [red, ultra thick, ->] (axis cs:7,-1) -- (axis cs:-1,3);

\addplot3[blue,/pgfplots/quiver,
    quiver/u=-cos(x),
    quiver/v=-sin(x),
    quiver/scale arrows=0.1,
    -stealth,samples=10,
    domain=0:360,
    y domain=0:sqrt(50),
    data cs=polar] {1};          
\end{axis}
\end{tikzpicture}
\end{document}

If you want to stick with cartesian coordinates for specifying the function, you can limit the range of the color map using point meta min=0, and cover the unwanted bits of the surface plot using \fill [white] (rel axis cs:0,0) rectangle (rel axis cs:1,1) (axis cs:0,0) circle [radius=7]; (this requires \pgfplotsset{compat=1.4} or higher).

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}
\begin{document}
\xdefinecolor{lightgrey}{RGB}{220,220,220}
\xdefinecolor{goldenrod}{RGB}{255,223,66}
\xdefinecolor{newblue}{RGB}{57,106,177}
\xdefinecolor{newred}{RGB}{204,37,41}
\xdefinecolor{newgreen}{RGB}{132,186,91}
\xdefinecolor{newpurple}{RGB}{144,103,167}
\begin{tikzpicture}[scale=1.2]
\begin{axis}[axis equal, view={0}{90}]
\addplot3[surf,shader=interp,opacity=0.5,domain=-7:7, point meta min=1]
{(50-x^2-y^2)};
\addplot [domain=0:2*pi,samples=50]({cos(deg(x))},{sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({2*cos(deg(x))},{2*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({3*cos(deg(x))},{3*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({4*cos(deg(x))},{4*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({5*cos(deg(x))},{5*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({6*cos(deg(x))},{6*sin(deg(x))});
\addplot [domain=0:2*pi,samples=50]({7*cos(deg(x))},{7*sin(deg(x))});
\addplot[newpurple,double=newpurple,->] plot coordinates {
    (0,0)
    (-1,3)
};
\addplot[newblue,double=newblue,->] plot coordinates {
    (0,0)
    (7,-1)
};
\addplot[newred,double=newred,->] plot coordinates {
    (7,-1)
    (-1,3)
};
\addplot+[newred,double=newred] plot coordinates {
    (1,2)
};
\addplot[newgreen,double=newgreen] plot coordinates {
    (-3,-6)
    (3,6)
};
\addplot3[blue,/pgfplots/quiver,
    quiver/u=-x/50,
    quiver/v=-y/50,
    quiver/scale arrows=0.1,
    -stealth,samples=10] {1};

\fill [white] (rel axis cs:0,0) rectangle (rel axis cs:1,1) (axis cs:0,0) circle [radius=7];         
\end{axis}
\end{tikzpicture}
\end{document}
3
  • Ah, the loop is smart, will implement that! The polar coordinates trick works but leaves a squiggly boundary and kind of obfuscates the math, at least for my untrained eyes. This being my math notes for class, I like being able to go back and see how I plotted things in retrospect.
    – Jay
    Feb 11, 2014 at 7:05
  • @Puffton: I wouldn't call it a "polar coordinates trick": for radially symmetric functions, using polar coordinates is much more straightforward. The squiggly boundary appeared because I used a different number of samples for the surface plot and for the line plots. I changed the code now.
    – Jake
    Feb 11, 2014 at 7:23
  • Thanks a lot Jake! Perfect answer, I will cherry pick both solutions for something that looks good and that I feel comfortable with!
    – Jay
    Feb 12, 2014 at 1:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .