The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Mystifying Scoping of Piecewise Variable?
Replies: 6   Last Post: Feb 3, 2011 5:33 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]

Posts: 1,448
Registered: 11/3/08
Re: Mystifying Scoping of Piecewise Variable?
Posted: Feb 3, 2011 5:29 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

(BUG ALERT. See the last line of code.)

The function

myFunc[y_] = Piecewise[{{x^2, x < 0}, {x, x >= 0}}]

depends only on y on the left, but it depends only on x on the right.

That obviously cannot work, but how, exactly, should we expect it to fail?

If x has no value, Piecewise has no numeric value:

\[Piecewise] x^2 x<0
x x>=0
0 True

so the first plot is empty.

In the second plot, x is given values across the range from -10 to +10, so
Piecewise also gets numeric values:

Table[myFunc[x], {x, -10, 10, 1}]

{100, 81, 64, 49, 36, 25, 16, 9, 4, 1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \

hence the second plot is not empty.

That doesn't mean your definition of myFunc is suddenly a good one. It's
still completely wrong.

But it works when you're passing x to the function AND varying x at the
same time.

So this plot is empty (because x still has no value):

Plot[myFunc[x], {y, -10, 10}]

But this plot is a constant:

x = -7;
Plot[myFunc[x], {y, -10, 10}]

When passing z and varying z, we see what I'd call a bug:

z =.
Plot[myFunc[z], {z, -10, 10}]

It plots the value of myFunc[-7] from the previous plot, even though x is
not involved, and z doesn't have a value.


On Wed, 02 Feb 2011 05:08:08 -0600, Frank Iannarilli <>

> Perhaps I'm tired, but this is weird:
> Clear[myFunc]
> myFunc[y_] = Piecewise[{{x^2, x < 0}, {x, x >= 0}}]
> (Same behavior below for either Set(=) or SetDelayed(:=) )
> Plot[myFunc[y], {y, -10, 10}]
> (returns empty plot)
> Plot[myFunc[x], {x, -10, 10}]
> (returns "expected" piecewise function plot)
> What is the scoping of the Piecewise variable, in this case "x"?
> How can one write a Module that creates and returns a Piecewise
> function? The scope of its argument is unclear to me.
> Thanks


Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.