Re: Mystifying Scoping of Piecewise Variable?
- To: mathgroup at smc.vnet.net
- Subject: [mg116136] Re: Mystifying Scoping of Piecewise Variable?
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Thu, 3 Feb 2011 05:29:37 -0500 (EST)
(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:
x=.
Piecewise[{{x^2,x<0},{x,x>=0}}]
\[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, \
10}
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.
Bobby
On Wed, 02 Feb 2011 05:08:08 -0600, Frank Iannarilli <frankeye at cox.net>
wrote:
> 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
>
--
DrMajorBob at yahoo.com