Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?


Some examples of the related questions are:

share|improve this question
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 – belisarius has settled Jan 13 '14 at 6:22
Also related: (35067) – Mr.Wizard Jan 13 '14 at 8:21

1 Answer 1

up vote 13 down vote accepted

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]

Mathematica graphics

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

Mathematica graphics

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

Mathematica graphics

share|improve this answer
@Mr.Wizard See here. – halirutan Feb 8 at 12:13
Sorry, I missed "add again a definition" – Mr.Wizard Feb 8 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 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 at 12:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.