# How to let Mathematica ignore complex values in Plot functions?

Consider the polar coordinates function of a circle centered at $(2,0)$ with a radius of $\sqrt 2$:

$$\rho(\theta)=\left\{\quad \begin{array}{c} 2 \cos \theta +\sqrt{2-4 \sin ^2\theta} \\ 2 \cos \theta -\sqrt{2-4 \sin ^2\theta} \\ \end{array} \right.,\quad \theta\in[\dfrac{\pi}{4},\dfrac{\pi}{4}]$$

Visualization:

PolarPlot[{2 Cos[t] + Sqrt[2 - 4 Sin[t]^2],
2 Cos[t] - Sqrt[2 - 4 Sin[t]^2]}, {t, -Pi/4, Pi/4},
PlotStyle -> {Red, Blue}, PlotRange -> {{0, 3.5}, {-1.5, 1.5}},
Epilog -> {{PointSize -> .02, Point[{{1, 1}, {1, -1}}]}, {Green,
Dashed, Line[{{0, 1}, {1, 1}, {1, -1}, {0, -1}}]}, {Purple,
Line[{{1, 1}, {0, 0}, {1, -1}}]}}, AxesStyle -> Arrowheads[.03],
PlotRangePadding -> Scaled[.05], ImageSize -> 600]


Where the blue and red arcs represents different part of the piecewise defined circle.

However, in Mathematica, when expand the plot domain of $\theta$ to $[-\pi,\pi]$, either piecewise of it gives a whole circle:

p1 = PolarPlot[{2 Cos[t] + Sqrt[2 - 4 Sin[t]^2]}, {t, -Pi, Pi},
PlotStyle -> {Red}, PlotRange -> {{0, 3.5}, {-1.5, 1.5}},
Epilog -> {{PointSize -> .02, Point[{{1, 1}, {1, -1}}]}, {Green,
Dashed, Line[{{0, 1}, {1, 1}, {1, -1}, {0, -1}}]}, {Purple,
Line[{{1, 1}, {0, 0}, {1, -1}}]}}, AxesStyle -> Arrowheads[.03],
PlotRangePadding -> Scaled[.05], ImageSize -> 400];
p2 = PolarPlot[{2 Cos[t] - Sqrt[2 - 4 Sin[t]^2]}, {t, -Pi, Pi},
PlotStyle -> {Blue}, PlotRange -> {{0, 3.5}, {-1.5, 1.5}},
Epilog -> {{PointSize -> .02, Point[{{1, 1}, {1, -1}}]}, {Green,
Dashed, Line[{{0, 1}, {1, 1}, {1, -1}, {0, -1}}]}, {Purple,
Line[{{1, 1}, {0, 0}, {1, -1}}]}}, AxesStyle -> Arrowheads[.03],
PlotRangePadding -> Scaled[.05], ImageSize -> 400];
Grid[{{p1,p2}}]


What special technique has been used in Mathematica when handling complex values of the function? How to let Mathematica ignore those complex values (not to plot them)?

• p1 = PolarPlot[{2 Cos[t] + Sqrt[2 - 4 Sin[t]^2]}, {t, -Pi, Pi}, RegionFunction -> ((2 - 4 Sin[#1]^2) > 0 &)] Commented Aug 27, 2015 at 0:29
• adding all those spurious options and code to a simple question makes it harder to read and understand. Please always try to post minimal examples Commented Aug 27, 2015 at 0:31
• Does wrapping the function in Re[...] not achieve what you want? Or using Piecewise? Commented Aug 27, 2015 at 1:07
• thank you @belisarius. This is only a simple example. Another case may have similar issue: ContourPlot3D[ z == Sin[(x z - 1/2)^2 + 2 x y^2 - z/ 10] Exp[-(x - 1/2 - Exp[z - y])^2 + y^2 - z/5 + 3], {x, -1, 7}, {y, -2, 2}, {z, -1, 2}, PlotTheme -> "Classic"] Commented Aug 27, 2015 at 1:10

Mathematica's special technique for handling complex values in a plot is to simply ignore them -- it plots nothing for at values of the domain for which the range is complex. Experiment with the following code.

Manipulate[
PolarPlot[r[t], {t, min °, max °},
PlotRange -> {{0, 3.5}, {-1.5, 1.5}},
Epilog -> {
Red, Line[{{0, 0}, r[min °] {Cos[min °], Sin[min °]}}],
Blue, Line[{{0, 0}, r[max °] {Cos[max °], Sin[max °]}}]},
ImageSize -> 400],
{{min, -180}, -180, 0, 10, Appearance -> "Labeled"},
{{max, 180}, .1, 180, 10, Appearance -> "Labeled"},
{r, None},
Initialization :> (r = (2 Cos[#] + Sqrt[2 - 4 Sin[#]^2] &))]


As you move the two sliders back and forth, you will see that the there are times when the radius vector of plot experiences discontinuities because it becomes complex-valued. There are dead zones (the background goes light red) in which nothing is plotted as the sliders are moved and pass through these zones.

Considering the OP's comment, I offer the following plot as a way of explaining how the full circle gets drawn.

Show @

• thank you! Don't know why, I got a lot error messages when changing the slider values. Another observation, both p1 and p2 above obtain whole circles. If complex variables were ignored, what we got should be only red or blue circular arc in the first figure above if my understanding is not wrong. Commented Aug 27, 2015 at 23:59
• @LCFactorization. I get no error message when moving the sliders in the above Manipulate expression. Did you copy from this answer correctly? As to your remark about p1 and p2, I am afraid you are wrong. My Manipulate should show you why. The full circle is drawn as four arcs: 0 < t < π/4, π/2 < t < π, -π < t < -π/2, -π/4 < t < 0, The arcs -π/2, < t < -π/4 and π/4 < t < π/2 are the dead zones where complex values are ignored. Commented Aug 28, 2015 at 0:51
• @LCFactorization. Your error messages come from your Epilog code. Graphics primitives don't tolerate complex values the way Plot does. Commented Aug 28, 2015 at 1:52
• I tried this code Plot[{2 - 4 Sin[t]^2, 0}, {t, -Pi, Pi}, Ticks -> {(Range[10] - 5)/4 Pi, Automatic}, Filling -> {2 -> {{1}, {Yellow, White}}}]` and it also separates real from complex regions clearly. Commented Aug 28, 2015 at 2:31