I would like to plot only the real values of a function of two variables $ z=f(x,y) $, i.e. $ z\in\mathbb{R} $. I am not interested in the region over which the image of the function is complex, i.e. $ z\in\mathbb{C} $.

The problem is that $ f $ is real only in a certain "hard-to-define" region of the $ (x,y) $ plane and imaginary in the remaining region. So it would be much simpler to tell Plot3D to only plot real values and ignore the complex ones. What happens with the default settings is that Plot3D plots the real part of $ f $ and ignore the complex one.

EDIT: The function that I am trying to plot is quite complicated: $f(x,y)=\frac{1}{3} \left(1-\frac{\cos (4 x)+\cos (4 y)-2}{ \sin ^2\left(2 \arcsin \left(\sqrt{\sin ^2(x)+\sin ^2(y)}\right)\right)}\right)$

f = 1/3 (1 - (-2 + Cos[4 x] + Cos[4 y])/Sin[2 ArcSin[Sqrt[Sin[x]^2 + Sin[y]^2]]]^2)

Here the plot of the function "as is":

 Plot3D[f, {x, 0, 2 Pi}, {y, 0, 2 Pi}, PlotPoints -> 300]

enter image description here

Here the plot of the imaginary part of the function

 Plot3D[Im[f], {x, 0, 2 Pi}, {y, 0, 2 Pi}, PlotPoints -> 300]

enter image description here

As you can see the imaginary part is non-null inside the "squares". I would like to know how to make Plot3D ignore those regions when plotting. Thanks


closed as off-topic by Bob Hanlon, m_goldberg, LLlAMnYP, MarcoB, gpap Sep 17 '18 at 12:17

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Bob Hanlon, MarcoB, gpap
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    $\begingroup$ This you get automatically. Just try this: f[x_, y_] := Sqrt[x^2 - y^2]; Plot3D[f[x, y], {x, -1, 1}, {y, -1, 1}]. In the domains where the function f[x,y] is complex Plot3D builds nothing at all. $\endgroup$ – Alexei Boulbitch Sep 7 '18 at 12:23
  • $\begingroup$ I propose to close this question as an off-topic. The reason is that it is based on the lack of the basic knowledge of Mma functions described in the documentation. $\endgroup$ – Alexei Boulbitch Sep 7 '18 at 12:25
  • 2
    $\begingroup$ Can you give us more information about the function? As Alexei says, Plot3D generally only plots the real-valued portion of a function. But... Something like Plot3D[x + 10^-14 I y, {x, 0, 1}, {y, 0, 1}] will plot a surface because "small" imaginary parts are quietly discarded since they can frequently arise as numerical errors during calculations. Is this what you're running into? $\endgroup$ – Brett Champion Sep 7 '18 at 14:04
  • $\begingroup$ I added further details to the answer. I would like to stress that I have already searched the documentation and on google for an answer but I couldn't anything specific to my case. $\endgroup$ – LastStarDust Sep 9 '18 at 1:58
  • $\begingroup$ Can you provide the Wolfram Language code required for people to do this on their own........... $\endgroup$ – user6014 Sep 9 '18 at 2:09

You can use ConditionalExpression or RegionFunction to focus on part of the domain on which f is real:

Plot3D[ConditionalExpression[f, Im[f] == 0], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
 PlotPoints -> 50, PlotRange -> {0, 3}]

enter image description here

f2[x_, y_] := Evaluate@f;  
Plot3D[f2[x, y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, 
  PlotPoints -> 100, RegionFunction -> (Im[f2[#, #2]] == 0 &)]

enter image description here

  • $\begingroup$ Thank you very much for the answer! It is exactly what I needed. $\endgroup$ – LastStarDust Sep 9 '18 at 8:37
  • $\begingroup$ @LastStarDust, my pleasure. Thank you for the accept. $\endgroup$ – kglr Sep 9 '18 at 8:40

The real values correspond to the region where Im[Sin[2 ArcSin[Sqrt[Sin[x]^2 + Sin[y]^2]]]^2] == 0. To define the function only in this region, use Condition, aka /;, like this

f[x_, y_] := 
 1/3 (1 - (-2 + Cos[4 x] + Cos[4 y]) /
      Sin[2 ArcSin[Sqrt[Sin[x]^2 + Sin[y]^2]]]^2) /; 
  Im[Sin[2 ArcSin[Sqrt[Sin[x]^2 + Sin[y]^2]]]^2] == 0

Plot3D[f[x, y], {x, 0, 2 Pi}, {y, 0, 2 Pi}, PlotPoints -> 50]
  • $\begingroup$ This answer is correct too. $\endgroup$ – LastStarDust Sep 9 '18 at 8:44

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