Piecewise Function, Explanation of Extra Case



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.


Posted 2015-12-17T04:15:50.593

Reputation: 14 143

2It'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 – 2015-12-17T04:20:24.840


As for your update, see this post.

– march – 2015-12-17T05:09:44.937

2What happens when you let x be a complex number? It will default to 0 in that case. – Chip Hurst – 2015-12-17T05:11:11.200

@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 – 2015-12-17T05:37:28.903

1I 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 – 2015-12-17T06:09:24.560

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 ennui – 2015-12-17T06:43:35.590

@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 – 2015-12-17T20:26:30.350

@J.M. I made an update to my original post. Look what happened when I substituted a complex number. – David – 2015-12-19T00:59:55.303



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

Robert H

Posted 2015-12-17T04:15:50.593

Reputation: 162