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?

  • $\begingroup$ @Manu Thank you. What does it do? $\endgroup$ – Alexei Boulbitch Mar 13 '17 at 13:45
  • 1
    $\begingroup$ 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. $\endgroup$ – TeM Mar 13 '17 at 13:48
  • 4
    $\begingroup$ Alternative: changing PlotRange -> All with Exclusions -> None (I think is the appropriate command to the case). $\endgroup$ – TeM Mar 13 '17 at 13:55
  • $\begingroup$ I cannot reproduce your problem (neither in a notebook, not after exporting). What's your $Version? $\endgroup$ – corey979 Mar 13 '17 at 14:02
  • 2
    $\begingroup$ 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] $\endgroup$ – 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}]

Mathematica graphics

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}]

Mathematica graphics

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

Visualization`ExpandExclusions[{functions}, {variables}, userExclusions]

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

Visualization`VisualizationDiscontinuities[{functions}, {variables}]

Visualization`ExpandExclusions expands the discontinuities of the OP's function:

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

Visualization`VisualizationDiscontinuities does not (in V9-11):

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

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

  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 Visualization`ExpandExclusions uses PiecewiseExpand, so it may not be equivalent.


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.