# Why does piecewise plot have a discontinuity when the function, first and second derivatives are equal?

There were some related questions to this one, but in this case we have the function value, the first derivative and the second derivative all equal (that was actually the problem I was solving).

We have:

f[x_] := Piecewise[{{3 x^2 - 3 x + 1, x >= 1}, {x^3, x < 1}}]
Plot[f[x], {x, 0, 1.5}]


The answer in those other questions was to use "Exclusions -> None" and it can be used here, but why is this happening since we are good in all derivative information?

Update

Some examples of the related questions are:

• I'm not sure that I should be saying this, especially since you are such an experienced user, but I think it would help if you provided the link to the other questions that you were mentioning. – Vincent Tjeng Jan 13 '14 at 4:00
• I have added three examples. Regards – Amzoti Jan 13 '14 at 4:13
• I think Mma will simply put an exclusion at predefined positions for various function types. E.g. for any HeavisideTheta it puts an exclusion to where the argument is zero; for any Piecewise 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 all Piecewise functions. Not very surprising IMO. Note that the discontinuity detection is not numerical, it's symbolic. – Szabolcs Jan 13 '14 at 5:28
• @Szabolcs f[x_] := Piecewise[{{3 x^3 - 3 x + 1, x >= 1}, {x^3, x < 1}}] + 3/10 – Dr. belisarius Jan 13 '14 at 6:22
• Also related: (35067) – Mr.Wizard Jan 13 '14 at 8:21

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


• @Mr.Wizard See here. – halirutan Feb 8 '15 at 12:13
• Sorry, I missed "add again a definition" – Mr.Wizard Feb 8 '15 at 12:16
• @Mr.Wizard I have made it more clear in the answer what happens and that the first definition of f is still required. – halirutan Feb 8 '15 at 12:16
• Since I voted for this answer a year ago I wonder if I understood this then. The declining years... – Mr.Wizard Feb 8 '15 at 12:17