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


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


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

  • $\begingroup$ thanks, now i have this plot.but don't understand why isn't support circle? my solution must be my plot just without breaking..and this looks a little , different cause i don't see the circle on the xoy flat $\endgroup$ – Tamar Beridze Apr 6 '18 at 16:03
  • $\begingroup$ looks exactly what i needed, thank you very much . $\endgroup$ – Tamar Beridze Apr 6 '18 at 17:16

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.

  • $\begingroup$ thank you for your comment , but still got the same plot :( $\endgroup$ – Tamar Beridze Apr 6 '18 at 15:20
  • $\begingroup$ thanks for uploading..but as i thought f(x,y) must me nonzero on the circle with radius pi/2..my problem is to see this picture ..but this plot doesn't seem so..am i wrong with constructing the function? $\endgroup$ – Tamar Beridze Apr 6 '18 at 16:09
  • $\begingroup$ You are wrong as the plot shows. If $r=\pi/2$ then $\cos(r)=0$. $\endgroup$ – Somos Apr 6 '18 at 16:16
  • $\begingroup$ r=π/2 is the points (x,y) which satisfy the x^2+y^2=(π/2)^2 so they are the circle points....and the cos must be zero on this circle points.. $\endgroup$ – Tamar Beridze Apr 6 '18 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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