# How to use ContourPlot3D for Piecewise 3D function

How would I plot in 3D: $\begin{cases} (4 - z^2) = x^2 + y^2, 2 \le z \le 4 \\ x^2 + y^2 = 4, -2 \le z \le 2 \end{cases}$.

ContourPlot3D[ Piecewise[{(4 - z)^2 == x ^2 + y^2, 2 \le z \le 4}, {x^2 + y^2 = 4, -2 \le z \le 2}], {x, -3, 3}, {y, -3 , 3}, {z, -2, 4}]

I realise there's tex in my code but I don't know how otherwise to symbolise my attempt.

• Your code omits the function Piecewise -- how did you try to use it? Also, your code mixes Mathematica with TeX -- I don't think that's going to work. – Michael E2 Apr 5 '14 at 15:23
• Your first equation has only one point of trueness, so just delete it and do ContourPlot3D[4 == x^2 + y^2, {x, -3, 3}, {y, -3, 3}, {z, -2, 2}] – Coolwater Apr 5 '14 at 15:24
• @MichaelE2: Thanks. I emended my question. Piecewise still doesn't function though. – NNOX Apps Apr 5 '14 at 16:11
• @Coolwater: Would you please explain "one true point of trueness"? – NNOX Apps Apr 5 '14 at 16:11
• I thought your first equation would give an empty contour plot, because only one point in R^3 satisfy it. The thing is I was looking at your latex equations which says (4-z^2) rather than (4-z)^2 – Coolwater Apr 5 '14 at 19:40

I would plot each separately and combine them:

Show[
ContourPlot3D[(4 - z)^2 == (x^2 + y^2), {x, -3, 3}, {y, -3, 3}, {z, 2, 4}],
ContourPlot3D[x^2 + y^2 == 4, {x, -3, 3}, {y, -3, 3}, {z, -2, 2}],
PlotRange -> All
] Piecewise version :

ContourPlot3D[ x^2 + y^2 ==
Piecewise[{{(4 - z)^2, 2 <= z <= 4}, {4, -2 <= z <= 2}}],
{x, -3, 3}, {y, -3, 3}, {z, -2, 4}, PlotRange -> All ]