2

Consider:

enter image description here Can someone explain why the 0, True occurs?

As a second question, I like to fill the endpoints as follows:

Plot[f[x], {x, -2, 2},
 Epilog -> {Blue, PointSize[Large],
   Point[{{-1, 3}, {-1, -1}, {1, 1}, {1, 0}}],
   White, PointSize[Medium],
   Point[{{-1, 3}, {1, 1}}]
   }]

enter image description here

Does anyone use an easier method for filled and unfilled endpoints?

Update: A lot of folks mentioned the possibility that x could be a complex number. I gave that a try. Watch what happened.

enter image description here

So I still am not sure how to explain this 0, True situation to my students.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
David
  • 14,883
  • 4
  • 44
  • 117
  • 2
    It's a default added automatically to the end of Piecewise. If none of the conditions above it evaluate to True, then the last condition automatically evaluates to True, and the function spits out a 0. You can change that default by explicitly putting in, say {-1, True}. Piecewise tests its arguments in order: for example, ponder on the output when you evaluate Piecewise[{{-1, True}, {1, x > 0}}]. – march Dec 17 '15 at 04:20
  • 1
    As for your update, see this post. – march Dec 17 '15 at 05:09
  • 2
    What happens when you let x be a complex number? It will default to 0 in that case. – Greg Hurst Dec 17 '15 at 05:11
  • @march All, I understand that, but consider f[x_] = Piecewise[{{2 - x, x < -1}, {x, -1 <= x < 1}, {(x - 1)^2, x >= 1}}]. The conditions $x<-1$, $-1\le x<1$, and $x\ge 1$ cover every possible value of $x$, so I wonder why an extra condition is needed. – David Dec 17 '15 at 05:37
  • 1
    I can only speculate, but I imagine it's because there might be some problems with checking to see if all the conditions cover every possible value of x. There might also be something about the implementation of Piecewise that makes that sort of thing difficult. For possible hints of that, take your f[x], Simplify it, and look at the output. – march Dec 17 '15 at 06:09
  • 4
    "every possible value of $x$" - I don't see your construction covering complex arguments; recall that Mathematica assumes everything is complex unless told otherwise. – J. M.'s missing motivation Dec 17 '15 at 06:43
  • @J.M. Yep, complex numbers are not covered. That does explain it. I also carefully read: If all preceding $val_i$ yield False, then the $val_i$ corresponding to the first $cond_i$ that yields True is returned as the value of the piecewise function. So, if the first two return false, the third was will always return true. I'm sure it's organized like this due to its use in so many other Mathematica functions. I tried two other things:: f[x_] = Piecewise[{{2 - x, x < -1}, {x, -1 <= x < 1}}, (x - 1)^2] and f[x_] = PiecewiseExpand[f[x], Assumptions -> Reals]. – David Dec 17 '15 at 20:26
  • @J.M. I made an update to my original post. Look what happened when I substituted a complex number. – David Dec 19 '15 at 00:59

1 Answers1

6

Help says:

Piecewise[conds] automatically evaluates to Piecewise[conds,0].

This implies that the default {0, True} item is added to the Piecewise. It is the same as if you wrote

f[x_] = Piecewise[{{2 - x, x < -1}, {x, -1 <= x < 1}, {(x - 1)^2, x >= 1}, {0, True}}]
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Robert H
  • 162
  • 1
  • 3