A discontinuity in a plot of a continuous function
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
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:
Visualization`VisualizationDiscontinuities[{
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.
The answer is :
Plot[If[a >= 0.5, 0, Sqrt[0.5 - a]], {a, -1, 1}, PlotRange -> All, PlotPoints -> 2000]
The resulted plot is :
Increasing the PlotPoints
attribute may help the Plot
to draw a smooth curve over discontinuities in piecewise functions.