Mathematica even leaves a gap when the expressions in Piecewise
are equal, as long as Mathematica doesn't see the equality. Very simple example
test[x_] := Piecewise[{{x, x >= 1}, {Sqrt[x^2], x < 1}}]
Plot[test[x], {x, 0, 2}, PlotStyle -> Thick]
When you replace Sqrt[x^2]
by x
, no gap.
What you have to understand is that the cracks are features when you use Piecewise
because usually a piecewise function has jumps or discontinuities. Mathematica really just splits the plot, when it sees Piecewise
and cannot determine a simple equality between the expressions.
This is a bit unfortunate, because although it is correct most of the time, users will always complain why this doesn't work like they want it.
My short answer: If you don't want cracks, then use Exclusions->None
or make sure Mathematica doesn't see your Piecewise
f[x_?NumericQ] := Piecewise[{{3 x^2 - 3 x + 1, x >= 1}, {x^3, x < 1}}]
Plot[f[x], {x, 0, 1.5}]
Here, the NumericQ
hinders Mathematica to evaluate your expression for non-numeric values and the only chance it has is to put a number in and get a number out :-)
Btw, let's give Mathematica something to think about and add a second definition for f
where it sees the Piecewise
. It doesn't matter what it sees, so screw it:
f[x_] := Piecewise[{{"Ding", x < .4}, {"Dong", x < .8}, {"Boing", x < 1.2}}, "Blub"];
The important part is that Mathematica uses the above definition to see a Piecewise
and the gaps it introduces. For plotting, Mathematica has to supply real numbers into f
and then our very first definition is used. I hope this explains, why the following plot looks as it looks:
Plot[f[x], {x, 0, 1.5}]
HeavisideTheta
it puts an exclusion to where the argument is zero; for anyPiecewise
function it puts an exclusion inbetween the pieces. It won't perform additional analysis to figure out that the function is in fact continuous in your case, it just does what it would do for allPiecewise
functions. Not very surprising IMO. Note that the discontinuity detection is not numerical, it's symbolic. $\endgroup$f[x_] := Piecewise[{{3 x^3 - 3 x + 1, x >= 1}, {x^3, x < 1}}] + 3/10
$\endgroup$