Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageBradley 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.
Post Follow-up to this messageThe answer you sis 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
Post Follow-up to this messageThis 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]
Post Follow-up to this messageBradley 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
Post Follow-up to this messageHi, 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
Post Follow-up to this messageBradley 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/
Post Follow-up to this messageBradley,
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
Post Follow-up to this messageBradley 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
Post Follow-up to this messageCurtis 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.