1
$\begingroup$

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]

2D Plot

Attempting to plot the 3D version, I tried entering this:

ContourPlot3D[f3[x, y, z], {x, -5, 5}, {y, -5, 5}, {z, -5, 20}]

and sadly received this monstrosity:

3D Plot

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):

3D Plot 2

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

$\endgroup$
5
  • $\begingroup$ What's wrong with that result? What were you expecting? $\endgroup$
    – wxffles
    Feb 12, 2014 at 22:18
  • $\begingroup$ sure you don't just want Plot3D ? Your function isn't a function of z at all.. $\endgroup$
    – george2079
    Feb 12, 2014 at 22:28
  • $\begingroup$ If you give the option contours->50 to ContourPlot3D it should be more apparent what you are plotting..(slow) $\endgroup$
    – george2079
    Feb 12, 2014 at 22:39
  • 1
    $\begingroup$ 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. $\endgroup$
    – Michael E2
    Feb 12, 2014 at 22:51
  • $\begingroup$ just edited to contain more info. accidentally copied all the graphs but its the 2nd row, 2nd column $\endgroup$
    – Sam
    Feb 12, 2014 at 22:59

1 Answer 1

6
$\begingroup$

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).

$\endgroup$
1
  • $\begingroup$ Yes!! This is what I was trying to do. Thank you so much!! Sorry if it was unclear. $\endgroup$
    – Sam
    Feb 12, 2014 at 23:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.