# A discontinuity in a plot of a continuous function

I plot a continuous, though not a smooth function:

Plot[If[a >= 0.5, 0, Sqrt[0.5 - a]], {a, -1, 1}, PlotRange -> All]


which returns the following

One can see the discontinuity at a=0.5. It should not be there. Any idea of how to remove it and why it shows up?

• @Manu Thank you. What does it do? – Alexei Boulbitch Mar 13 '17 at 13:45
• Any mathematical software graphics a function for points. In the case of functions with sharp graphics you must obtain the software to be more refined, thickening in fact the jersey of points where evaluate the function. – TeM Mar 13 '17 at 13:48
• Alternative: changing PlotRange -> All with Exclusions -> None (I think is the appropriate command to the case). – TeM Mar 13 '17 at 13:55
• I cannot reproduce your problem (neither in a notebook, not after exporting). What's your \$Version? – corey979 Mar 13 '17 at 14:02
• Recommend that you use Piecewise rather than If for mathematical functions. Plot[Piecewise[{{Sqrt[1/2 - a], a < 1/2}}], {a, -1, 1}, Exclusions -> None] – Bob Hanlon Mar 13 '17 at 14:34

Basically, the automatic expansion of Exclusions is evolving. Currently (V11.0.1 for me), it seems that when a function changes formulas -- for example, through Piecewise, UnitStep, if, etc. -- the changes are treated as singularities without checking whether the function is finite at the "singular" point or whether the two formulas are continuous with each other. Consequently, such points are excluded. The difficulty is, I assume, in determining the two values the function approaches as the input approaches the singular point from each side. It can be tricky, given that the user supplies the code for the function and there isn't always a convenient way to determine the limits of the function. (Related: Plot a piecewise function with black and white disks marking discontinuities).

The "issue" arises because If[] was originally a programming construct and Piecewise[] was a function construct. In earlier versions of Mathematica, the discontinuity procession of If[] was not as complete as that for Piecewise[]. I suspect it is because users are familiar with "if" from other programming languages, and consequently many use If[] instead of Piecewise[], that If[] is becoming more and more equivalent to Piecewise[].

So what you see in the new(er) versions is the extension of discontinuity processing in plots of If[cond, e1, e2] to exclude points where cond changes value.

The principal solution is to manually override Exclusions, as suggested by Manu and Bob Hanlon:

Plot[If[a >= 0.5, 0, Sqrt[0.5 - a]], {a, -1, 1},
Exclusions -> None, PlotRange -> {-0.01, 0.1}]


Note that MaxRecursion -> 15 makes the discontinuity "disappear" by excessive refinement, but the discontinuity is still present if you zoom in:

Plot[If[a >= 0.5, 0, Sqrt[0.5 - a]], {a, -1, 1},
MaxRecursion -> 15, PlotRange -> {-0.001, 0.01}]


In V11, Exclusions -> userExclusions is handled internally by a new function

VisualizationExpandExclusions[{functions}, {variables}, userExclusions]


In V10, V9 and perhaps earlier, Exclusions are handled by

VisualizationVisualizationDiscontinuities[{functions}, {variables}]


VisualizationExpandExclusions expands the discontinuities of the OP's function:

VisualizationExpandExclusions[{If[a >= 0.5, 0, Sqrt[0.5 - a]]}, {a}, Automatic]
(*  {{0.5 - a == 0, True}, {-0.5 + a == 0, True}}  *)


VisualizationVisualizationDiscontinuities does not (in V9-11):

VisualizationVisualizationDiscontinuities[{If[a >= 0.5, 0, Sqrt[0.5 - a]]}, {a}]
(*  {}  *)


But it will if the If[] is converted to a Piecewise function:

VisualizationVisualizationDiscontinuities[{
PiecewiseExpand@If[a >= 0.5, 0, Sqrt[0.5 - a]]}, {a}]
(*  {{0.5 - Re[a] <= 0, -Im[a] == 0}, {True, -0.5 + a == 0}, {True, 0.5 - a == 0}}  *)


In this last case, in V9+, the plot of PiecewiseExpand@If[..] will contain a gap at a == 0.5. However, it does not appear that VisualizationExpandExclusions uses PiecewiseExpand, so it may not be equivalent.