How to fill a solid defined by x^2+y^2<=9, z<=16-x^2-y^2 and z>=0 using PGFPlots

I would like to draw the body D defined by x^2+y^2<=9, z<=16-x^2-y^2 and z>=0.

We have to graph x^2+y^2=9, z=16-x^2-y^2 and z=0 with less opacity, and then fill (or use patterns) the intersections to create the body D defined by the previous expressions:

However, I am not able to complete the task. Here is a MWE:

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

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

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.25,.81)},anchor=north east},
legend cell align={left},
view={135}{25}
]
\addplot3[orange,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({r*cos(t)},{r*sin(t)},{16-r*r});   % z=16-x^2-y^2
\addlegendentry{$$16-x^2-y^2$$}
\addplot3[green,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({3*cos(t)},{3*sin(t)},{0});   % x^2+y^2=9
\addlegendentry{$$x^2+y^2=9$$}
\fill[cyan,opacity=.5] (3,3,0) -- (3,-3,0) -- (-3,-3,0)
-- (-3,3,0);    % z=0
\addlegendentry{$$z=0$$}    % This label is not showing because we used 'fill', not 'addplot3'
% Here should be the code of the intersection of the three surfaces...
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


EDIT. Many thanks to marmot who helped me partially complete what I want:

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

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

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.45,.85)},anchor=north east},
legend cell align={left},
view={135}{25}
]
\addlegendentry{$$z=16-x^2-y^2$$}
\addlegendentry{$$x^2+y^2=9$$}
%\fill[cyan,opacity=.5] (4,4,0) -- (4,-4,0) -- (-4,-4,0) -- (-4,4,0);    % z=0
\addlegendentry{$$z=0$$}
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


However, we need to:

• Fix the output of z=0 (I do not want to use fill because then we cannot add a legend since it is not a plot).
• Fill the solid D using patterns: or filling it: What requires less programming time.

Thanks!!

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

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

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.45,.85)},anchor=north east},
legend cell align={left},
view={135}{25}
]
\addlegendentry{$$z=16-x^2-y^2$$}
\addlegendentry{$$x^2+y^2=9$$}
%\fill[cyan,opacity=.5] (4,4,0) -- (4,-4,0) -- (-4,-4,0) -- (-4,4,0);    % z=0
colormap={dull}{color=(cyan) color=(cyan)},opacity=0.5] {0};
\addlegendentry{$$z=0$$}
buffer=sort,samples=31,samples y=41]
({min(y,3)*cos(x)},{min(y,3)*sin(x)},{16-y*y});
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}


? It might make a nice cage for nasty ducks. ;-)

This shows the surface. I do not know what you want to do with the original plots.

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

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

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.25,.81)},anchor=north east},
legend cell align={left},
view={135}{25}
]
%             \addplot3[orange,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({r*cos(t)},{r*sin(t)},{16-r*r});   % z=16-x^2-y^2
%             \addlegendentry{$$16-x^2-y^2$$}
%             \addplot3[green,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({3*cos(t)},{3*sin(t)},{0});   % x^2+y^2=9
%             \addlegendentry{$$x^2+y^2=9$$}
%             \fill[cyan,opacity=.5] (3,3,0) -- (3,-3,0) -- (-3,-3,0)
%         -- (-3,3,0);    % z=0
%             %\addlegendentry{$$z=0$$}    % This label is not showing because we used 'fill', not 'addplot3'
% Here should be the code of the intersection of the three surfaces...
({3*cos(x)},{3*sin(x)},{y});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


This looks like a poisonous mushroom to me:

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}

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

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.25,.81)},anchor=north east},
legend cell align={left},
view={135}{25}
]
%             \addplot3[orange,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({r*cos(t)},{r*sin(t)},{16-r*r});   % z=16-x^2-y^2
%             \addlegendentry{$$16-x^2-y^2$$}
%             \addplot3[green,opacity=.5,samples=51,samples y=21,variable=t,variable y=r,domain=0:360,domain y=0:2*pi,restrict z to domain=0:16] ({3*cos(t)},{3*sin(t)},{0});   % x^2+y^2=9
%             \addlegendentry{$$x^2+y^2=9$$}
%             \fill[cyan,opacity=.5] (3,3,0) -- (3,-3,0) -- (-3,-3,0)
%         -- (-3,3,0);    % z=0
%             %\addlegendentry{$$z=0$$}    % This label is not showing because we used 'fill', not 'addplot3'
% Here should be the code of the intersection of the three surfaces...
buffer=sort,colormap=
{greenblack}{color=(green!50!black) color=(green)}]
({3*cos(x)},{3*sin(x)},{y});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


You can also draw it in one stretch.

\documentclass{article}
\usepackage[a4paper,margin=1in,footskip=0.25in]{geometry}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\begin{document}

\begin{center}
\begin{tikzpicture}
\begin{axis} [
axis on top,
axis lines=center,
xlabel=$x$,
ylabel=$y$,
zlabel=$z$,
xmin=-5,
ymin=-5,
zmin=-1,
xmax=5,
ymax=5,
zmax=17,
xtick={-4,-3,0,3,4},
xticklabels={$-4$,$-3$,$0$,$3$,$4$},
ytick={-4,-3,0,3,4},
yticklabels={$-4$,$-3$,$0$,$3$,$4$},
ztick={0,16},
zticklabels={$0$,$16$},
ticklabel style={font=\tiny},
legend pos=outer north east,
legend style={at={(1.25,.81)},anchor=north east},
legend cell align={left},
view={135}{25}
]
% Here should be the code of the intersection of the three surfaces...
buffer=sort,colormap=
{greenorange}{color=(green!80!black) color=(orange)},samples=31,samples
y=41]
({min(y,3)*cos(x)},{min(y,3)*sin(x)},{16-y*y});
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}


• It looks very nice! I would like to draw the cylinder in green, the upside-down paraboloid in orange and the plane in cyan. Then we need to show the body, probably using intersections and patterns or fill, I do not which is the best. Commented Feb 25, 2019 at 15:43
• @manooooh green-orange ....
– user121799
Commented Feb 25, 2019 at 15:55
• It is perfect!! How does ({min(y,3)*cos(x)},{min(y,3)*sin(x)},{16-y*y}) work? Have you used any change of variables? Commented Feb 25, 2019 at 23:07
• @manooooh No, this is just a trick to be able to draw the thing in one stretch. If you move along the z axis from 16 to lower values, the radius of the shape will increas until it hits its maximum value of 3.
– user121799
Commented Feb 25, 2019 at 23:44
• @manooooh This depends only on one variable. Why do you expect a mesh? You only draw a circle in the xy plane. Where is the mesh supposed to be? R U looking 4 \addplot3[mesh,color=gray,ultra thin,domain=0:360,,domain y=0:3, samples=31,samples y=31] ({y*cos(x)},{y*sin(x)},{0});?
– user121799
Commented Feb 26, 2019 at 1:44