# Plotting multivariable functions with dependent variables in domain

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}]
{(1/100)(50-x^2-y^2)};
(0,0)
(-1,3)
};
(0,0)
(7,-1)
};
(7,-1)
(-1,3)
};
(1,2)
};
(-3,-6)
(3,6)
};
quiver/u=-x/50,
quiver/v=-y/50,
quiver/scale arrows=0.1,
-stealth,samples=10] {1};
\end{axis}
\end{tikzpicture}
\end{document}


• 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

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}]
surf,
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}{
}

\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);

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}]
{(50-x^2-y^2)};
(0,0)
(-1,3)
};
(0,0)
(7,-1)
};
(7,-1)
(-1,3)
};
(1,2)
};
(-3,-6)
(3,6)
};
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}

• 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