Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

piecewise vs which
Consider defining a function in Mathematica (v. 5.2) in two different
ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
either function.  However, if I ask for f[2], Mathematica returns 0 and if I
 ask
for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the dom
ain?
While I have eyes, there is another question regarding limits.  Is it
the case that Limit[f[x],x->2] defaulted as
Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
the above example.  I'm using Mathematica in my calculus class and would
like to explain why Mathematica does certain things.  It doesn't seem
that it would've been too difficult to just have two subroutines (a
right and left hand limit) to determine whether a 'full' limit actually
existed.
Thanks!

Bradley


Report this thread to moderator Post Follow-up to this message
Old Post
Bradley Stoll
09-02-05 08:58 AM


Re: piecewise vs which
Bradley Stoll wrote:
> Consider defining a function in Mathematica (v. 5.2) in two different
> ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
> g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
> either function.  However, if I ask for f[2], Mathematica returns 0 and if
 I ask
> for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
> Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the domain?[/
color]

Hi Bradley,

Mathematica is "correct" by returning 0 for f[2] since this is the
expected behavior as documented in the documentation for *Piecewise*

"Piecewise[{{val_1, cond_1}, ...}, val] uses default value _val_ if none
of the cond_i apply. *The default for _val_ is 0.*"

http://documents.wolfram.com/mathem...tions/Piecewise

So it may be better to define your function in the following way

In[1]:=
f[x_] = Piecewise[{{x^2, x < 2}, {3*x, x > 2}}, "Undefined"];
f[2]

Out[1]=
Undefined

Best regards,
/J.M.


Report this thread to moderator Post Follow-up to this message
Old Post
Jean-Marc Gulliet
09-03-05 08:57 AM


Re: piecewise vs which
The answer you s is in the documentation for Piecewise. All
Piecewise functions have a default value of zero on their undefined
intervals. You can change this.

On 9/2/05, Bradley Stoll <BradleyS@harker.org> wrote:
> Consider defining a function in Mathematica (v. 5.2) in two different
> ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
> g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
> either function.  However, if I ask for f[2], Mathematica returns 0 and if
 I ask
> for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
> Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the d
omain?
> While I have eyes, there is another question regarding limits.  Is it
> the case that Limit[f[x],x->2] defaulted as
> Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
> the above example.  I'm using Mathematica in my calculus class and would
> like to explain why Mathematica does certain things.  It doesn't seem
> that it would've been too difficult to just have two subroutines (a
> right and left hand limit) to determine whether a 'full' limit actually
> existed.
> Thanks!
>
> Bradley
>
>


--
Chris Chiasson
http://chrischiasson.com/
1 (810) 265-3161


Report this thread to moderator Post Follow-up to this message
Old Post
Chris Chiasson
09-03-05 08:57 AM


Re: piecewise vs which
This is not a bug.  By default Piecewise gives a value of 0 if none of
the conditions apply. But you can add an extra argument to change that
default.

In[1]:=
f[x_]:=Piecewise[{{x^2,x<2},{3x,x>2}},Null];
In[2]:=
f[2]
In[3]:=
g[x_]=Which[x<2,x^2,x>2,3x];
In[4]:=
g[2]


Report this thread to moderator Post Follow-up to this message
Old Post
dkr
09-03-05 08:57 AM


Re: piecewise vs which
Bradley Stoll wrote:

>Consider defining a function in Mathematica (v. 5.2) in two different
>ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
>g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
>either function.  However, if I ask for f[2], Mathematica returns 0 and if 
I ask
>for g[2] Mathematica (correctly) returns nothing.
>

If you look at the help on Piecewise it comes with the following disclosure

Piecewise[{{a, a}, … }, val] uses default value val if none of the a
apply. The default for val is 0.

Hmm......The other problem might be that this may not be a piecewise
continuous function

Limit[f[x],x->2,Direction->-1]
6
Limit[f[x],x->2,Direction->1]
4

Thanks for bringing this to my attention, I have been using piecewise
with impunity in my codes....may be I should look at them again

Pratik

>  Is this a bug with
>Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the do
main?
>While I have eyes, there is another question regarding limits.  Is it
>the case that Limit[f[x],x->2] defaulted as
>Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
>the above example.  I'm using Mathematica in my calculus class and would
>like to explain why Mathematica does certain things.  It doesn't seem
>that it would've been too difficult to just have two subroutines (a
>right and left hand limit) to determine whether a 'full' limit actually
>existed.
>Thanks!
>
>Bradley
>
>
>


--
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134



Report this thread to moderator Post Follow-up to this message
Old Post
Pratik Desai
09-03-05 08:57 AM


Re: piecewise vs which
Hi, Bradley,

Some similar questions -- but with limits of +/- Infinity  -- have
been bantered about on this group recently regarding limits.

The Piecewise function returns 0 as a default value when a domain
isn't given for an argument. I found this from the command ?Piecewise,
but it's also in the fuller Help Index documentation, of course. If you
make a larger "gap", say,

f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 10}}]


then Piecewise will return 0 for values for 2<= x <= 10.

For the students' sake you might define f to return Null at
"unspecified" arguments, with

f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 10}}, Null]


This should plot and behave more as you expect. Also, the Limit function
now works as you'd expect, including directed limits: try

Limit[f[x], x -> 2, Direction -> 1]

vs.

Limit[f[x], x -> 2].


Regards,
Curtis O.



Bradley Stoll wrote:

>Consider defining a function in Mathematica (v. 5.2) in two different
>ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
>g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
>either function.  However, if I ask for f[2], Mathematica returns 0 and if 
I ask
>for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
>Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the do
main?
>While I have eyes, there is another question regarding limits.  Is it
>the case that Limit[f[x],x->2] defaulted as
>Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
>the above example.  I'm using Mathematica in my calculus class and would
>like to explain why Mathematica does certain things.  It doesn't seem
>that it would've been too difficult to just have two subroutines (a
>right and left hand limit) to determine whether a 'full' limit actually
>existed.
>Thanks!
>
>Bradley
>
>
>
>

--
PGP Key ID: 0x235FDED1
Please avoid sending me Word or PowerPoint attachments.
http://www.gnu.org/philosophy/no-word-attachments.html


Report this thread to moderator Post Follow-up to this message
Old Post
Curtis Osterhoudt
09-03-05 08:57 AM


Re: piecewise vs which
Bradley Stoll schrieb:
> Consider defining a function in Mathematica (v. 5.2) in two different
> ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
> g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
> either function.  However, if I ask for f[2], Mathematica returns 0 and if
 I ask
> for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
> Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the d
omain?
> While I have eyes, there is another question regarding limits.  Is it
> the case that Limit[f[x],x->2] defaulted as
> Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
> the above example.  I'm using Mathematica in my calculus class and would
> like to explain why Mathematica does certain things.  It doesn't seem
> that it would've been too difficult to just have two subroutines (a
> right and left hand limit) to determine whether a 'full' limit actually
> existed.
> Thanks!
>
> Bradley
>
Hi Bradley,

if you have a look at the documentation, you'll see that Piecewise has
a default value 0. If you want f[2] to return Null (as g[2] does, enter

In[1]:=
f[x_] = Piecewise[{{x^2, x < 2}, {3*x, x > 2}},Null];
g[x_] = Which[x < 2, x^2, x > 2, 3*x];
In[3]:=
f[2] == g[2]
Out[3]=
True
--
Peter Pein, Berlin
GnuPG Key ID: 0xA34C5A82
http://people.freenet.de/Peter_Berlin/


Report this thread to moderator Post Follow-up to this message
Old Post
Peter Pein
09-03-05 08:57 AM


Re: piecewise vs which
Bradley,

If you examine the Help for Piecewise you will see that it is possible to
include, as a last argument, a return value if none of the conditions are
True, and that the default value for this return value is 0. So the routine
was behaving exactly as advertised.

You could define it this way.

f[x_] := Piecewise[{{x^2, x < 2}, {3x, x > 2}}, Indeterminate]

f[2]
Indeterminate

In fact, this is probably a good example for students in computer algebra.
One has to define things completely and if this is not done one is at the
mercy of others.

The Limit question is another good Mathematica question. Mathematica already
has thousands of commands. Most of them are fairly well designed but some of
them aren't and Limit seems to be one of these because people are always
asking and complaining about the Direction option. In any case, students and
teachers will often have to write routines to specialize to their case, or
to implement a more favorable usage. It would be just as well that students
learn this fact, and get used to writing definitions. So here are routines
you could use. (Students should learn how to write usage messages also.)

ForwardLimit::usage =
"ForwardLimit[expr, point, xvar:x] is the same as Limit[expr, xvar -> \
point, Direction -> 1]";
ForwardLimit[expr_, point_, xvar_:x] :=
Limit[expr, xvar -> point, Direction -> 1]

BackwardLimit::usage =
"BackwardLimit[expr, point, xvar:x] is the same as Limit[expr, xvar -> \
point, Direction -> -1]";
BackwardLimit[expr_, point_, xvar_:x] :=
Limit[expr, xvar -> point, Direction -> -1]

{ForwardLimit[f[x], 2], BackwardLimit[f[x], 2]}
{4, 6}

David Park
djmp@earthlink.net
http://home.earthlink.net/~djmp/




From: Bradley Stoll [mailto:BradleyS@harker.org]


Consider defining a function in Mathematica (v. 5.2) in two different
ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
either function.  However, if I ask for f[2], Mathematica returns 0 and if I
ask
for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the
domain?
While I have eyes, there is another question regarding limits.  Is it
the case that Limit[f[x],x->2] defaulted as
Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
the above example.  I'm using Mathematica in my calculus class and would
like to explain why Mathematica does certain things.  It doesn't seem
that it would've been too difficult to just have two subroutines (a
right and left hand limit) to determine whether a 'full' limit actually
existed.
Thanks!

Bradley





Report this thread to moderator Post Follow-up to this message
Old Post
David Park
09-03-05 08:57 AM


Re: piecewise vs which
Bradley Stoll wrote:
> Consider defining a function in Mathematica (v. 5.2) in two different
> ways:  f[x_]=Piecewise[{{x^2,x<2},{3x,x>2}}] and
> g[x_]=Which[x<2,x^2,x>2,3x].  Notice that 2 is not in the domain of
> either function.  However, if I ask for f[2], Mathematica returns 0 and if
 I ask
> for g[2] Mathematica (correctly) returns nothing.  Is this a bug with
> Mathematica (that Mathematica returns 0 for f[2]), since 2 is not in the d
omain?
> While I have eyes, there is another question regarding limits.  Is it
> the case that Limit[f[x],x->2] defaulted as
> Limit[f[x],x->2,Direction->-1] (a right hand limit)?  Both return 6 in
> the above example.  I'm using Mathematica in my calculus class and would
> like to explain why Mathematica does certain things.  It doesn't seem
> that it would've been too difficult to just have two subroutines (a
> right and left hand limit) to determine whether a 'full' limit actually
> existed.

Limit does indeed default to Direction->-1. Try, for example,
Limit[Abs[x]/x,x->0]

I don't like this at all. For purposes of teaching calculus students,
where we are only concerned with real numbers and are not taking limits
in the complex plane, I would like Limit to check from both directions.

--
Helen Read
University of Vermont


Report this thread to moderator Post Follow-up to this message
Old Post
Helen Read
09-03-05 08:57 AM


Re: piecewise vs which
Curtis Osterhoudt wrote:
> Hi, Bradley,
>
>    Some similar questions -- but with limits of +/- Infinity  -- have
> been bantered about on this group recently regarding limits.
>
>    The Piecewise function returns 0 as a default value when a domain
> isn't given for an argument. I found this from the command ?Piecewise,
> but it's also in the fuller Help Index documentation, of course. If you
> make a larger "gap", say,
>
> f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 10}}]
>
>
> then Piecewise will return 0 for values for 2<= x <= 10.
>
> For the students' sake you might define f to return Null at
> "unspecified" arguments, with
>
> f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 10}}, Null]
>
>
> This should plot and behave more as you expect. Also, the Limit function
> now works as you'd expect, including directed limits: try
>
>  Limit[f[x], x -> 2, Direction -> 1]
>
> vs.
>
>  Limit[f[x], x -> 2].
>
Hi Curtis,

I might have miss something but the issue about the limit does not seem
to be solved if the function is only discontinuous at one point, say 2
as in the original question, rather than not being defined over an
interval. Compare

In[1]:=
Clear[f]
f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 10}}, Null]
Limit[f[x], x -> 2, Direction -> 1]
Limit[f[x], x -> 2, Direction -> -1]
Limit[f[x], x -> 2]

Out[3]=
4

versus

In[6]:=
Clear[f]
f[x_] := Piecewise[{{x^2, x < 2}, {3*x, x > 2}}, Null]
Limit[f[x], x -> 2, Direction -> 1]
Limit[f[x], x -> 2, Direction -> -1]
Limit[f[x], x -> 2]

Out[8]=
4

Out[9]=
6

Out[10]=
6

In[11]:=
$Version

Out[11]=
"5.2 for Microsoft Windows (June 20, 2005)"

I believe, although I may be wrong, that the limit can be handled
correctly only by defining your own limit function (see for example
David Park's message in this thread).

Best regards,
/J.M.


Report this thread to moderator Post Follow-up to this message
Old Post
Jean-Marc Gulliet
09-04-05 08:57 AM


Sponsored Links




Last Thread Next Thread Next
Pages (2): [1] 2 »
Search this forum -> 
Post New Thread

Mathematica archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:25 AM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.