# Make Plot3D only the real values of a function [closed]

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] Here the plot of the imaginary part of the function

 Plot3D[Im[f], {x, 0, 2 Pi}, {y, 0, 2 Pi}, PlotPoints -> 300] 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, gpapSep 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.

• 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. – Alexei Boulbitch Sep 7 '18 at 12:23
• 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. – Alexei Boulbitch Sep 7 '18 at 12:25
• 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? – Brett Champion Sep 7 '18 at 14:04
• 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. – LastStarDust Sep 9 '18 at 1:58
• Can you provide the Wolfram Language code required for people to do this on their own........... – user6014 Sep 9 '18 at 2:09

## 2 Answers

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

• Thank you very much for the answer! It is exactly what I needed. – LastStarDust Sep 9 '18 at 8:37
• @LastStarDust, my pleasure. Thank you for the accept. – 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

Clear[f]
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]

• This answer is correct too. – LastStarDust Sep 9 '18 at 8:44