mathematica:: 3d piecwise function problem

75 Views Asked by At

i'm trying to plot a function $$\ f(x,y)=cos(\sqrt{x^2 +y^2})$$,same as cos(r).. f(x,y)=0 for points ,which are on circle.when the points are out of this circle i want the function f(x,y) to be 0 as well. so the support of my function to be the circle.i want to plot this situation.. as i thought, my function is $$f(x,y)= \left\{ \begin{array}{c} cos(r) ,r<\pi/2\\ 0,othervise \\ \end{array} \right. $$ but my plot isn't continuous...please help me understand the problem.--> enter image description here--> enter image description here

2

There are 2 best solutions below

2
On BEST ANSWER

That is a feature to indicate that your function is not continuous there. You can use the Exclusions option to prevent it

k[x_, y_] := With[{r = Sqrt[x^2 + y^2]},
  Piecewise[{{Cos[r], r < Pi/2}}, 0]
]
Plot3D[k[x, y], {x, -5, 5}, {y, -5, 5}, Exclusions -> None]

Mathematica graphics

Edit

If you are wondering why this plot looks a bit bumpy at the bottom, the reason is that there are too few polygons used to create a sharp plot. You can try

Plot3D[k[x, y], {x, -5, 5}, {y, -5, 5}, Exclusions -> None, 
 PlotPoints -> 100, MaxRecursion -> 6]

and then you clearly see your circle at the bottom

Mathematica graphics

4
On

I think the problem is with $\texttt{Piecewise[]}$. Try this variation instead:

$\texttt{f[x_, y_] := With[{r = Sqrt[x*x + y*y]}, If[r > Pi/2, 0, Cos@r]];}$ $\texttt{Plot3D[f[x, y], {x, -5, 5}, {y, -5, 5}]}$

enter image description here

You can use the option $\texttt{PlotPoints -> 100}$ to make the base sharper, but using $\texttt{Piecewise[]}$ leaves a gap in the plot surface as you noticed.