# Problem With 3D Contour Plot

So I am very new to Mathematica and I'm using version 9. I need to plot a function $f(x,y)$ such that $f(x,y)$ is equal to $y^2$ when $x > y$ and equal to $10 \sin(x)$ when $x < y$. I want two contour plots, one in 2D and one in 3D. So far, I have plotted the 2D version without a problem in the following way:

f3[x_, y_, z_] := If[x > y, y^2, If[x < y, 10*Sin[x]]];
ContourPlot[f3[x, y, z], {x, -5, 5}, {y, -5, 5}, Contours -> 20, ColorFunction -> "Rainbow", PlotRangePadding -> None] Attempting to plot the 3D version, I tried entering this:

ContourPlot3D[f3[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -5, 20}] Any idea why? Any and all help would be greatly appreciated, I'm hoping it's a simple fix considering I've only been using the software for a few days.

EDIT: In response to some of the comments, this is the example that was provided (e.g. what it's supposed to look like): I understand that my function depends only on x and y, I only added the z to make the plot work. To clarify my question, how do I make it look like the second plot? Thanks for the help so far by the way

• What's wrong with that result? What were you expecting? – wxffles Feb 12 '14 at 22:18
• sure you don't just want Plot3D ? Your function isn't a function of z at all.. – george2079 Feb 12 '14 at 22:28
• If you give the option contours->50 to ContourPlot3D it should be more apparent what you are plotting..(slow) – george2079 Feb 12 '14 at 22:39
• To put wxffles' comment another way, the plot looks correct. Also, your words say $f(x,y)$, but your code defines $f(x,y,z)$. Probably you want f[x_, y_]:=... and Plot3D[f[x, y], {x,...}, {y,...}] and so forth. – Michael E2 Feb 12 '14 at 22:51
• just edited to contain more info. accidentally copied all the graphs but its the 2nd row, 2nd column – Sam Feb 12 '14 at 22:59

If I understand your question, this is what you are looking for:

f3[x_, y_] := Piecewise[{{y^2, x > y}, {10 Sin[x], x <= y}}]

Plot3D[f3[x, y], {x, -5, 5}, {y, -5, 5}, ColorFunction -> "Rainbow", MeshFunctions -> {#3 &}] These are the changes I made:

• $f(x,y)$ is a two-variable function (i.e. $f: \mathbb{R}^2 \rightarrow \mathbb{R}$), so define it as such. Do not use z in the argument list.

• ContourPlot3D is for plotting implicit surfaces defined by $f(x,y,z) = 0$. If I understand you correctly, you just wanted to plot $f(x,y)$ with contour lines corresponding to the values of $f$. So use Plot3D for plotting and set the MeshFunctions option to #3 & to have the contour lines. #3 means "third argument" which for Plot3D'd mesh function is the height, i.e. the value of $f$. The meaning of mesh function arguments are different for each plotting function and you'll find them in the documentation.

• Finally, I replaced If by Piecewise. The benefit is that Mathematica will be able to recognize the break at the line $x=y$ and make sure that the cut between the two regions is smooth. See Exclusions for details. Generally, use If as a programming construct and use Piecewise to define mathematical functions meant for symbolic operations.

• Optionally you might add MaxRecursion -> 4 for much smoother contour lines (see MaxRecursion).

• Yes!! This is what I was trying to do. Thank you so much!! Sorry if it was unclear. – Sam Feb 12 '14 at 23:44