8
$\begingroup$

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:

$\endgroup$
7
  • $\begingroup$ 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. $\endgroup$ Commented Jan 13, 2014 at 4:00
  • $\begingroup$ I have added three examples. Regards $\endgroup$
    – Amzoti
    Commented Jan 13, 2014 at 4:13
  • 3
    $\begingroup$ 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. $\endgroup$
    – Szabolcs
    Commented Jan 13, 2014 at 5:28
  • $\begingroup$ @Szabolcs f[x_] := Piecewise[{{3 x^3 - 3 x + 1, x >= 1}, {x^3, x < 1}}] + 3/10 $\endgroup$ Commented Jan 13, 2014 at 6:22
  • $\begingroup$ Also related: (35067) $\endgroup$
    – Mr.Wizard
    Commented Jan 13, 2014 at 8:21

1 Answer 1

20
$\begingroup$

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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