Re: piecewise vs which
- To: mathgroup at smc.vnet.net
- Subject: [mg60122] Re: [mg60101] piecewise vs which
- From: "David Park" <djmp at earthlink.net>
- Date: Sat, 3 Sep 2005 02:06:13 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
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 at earthlink.net http://home.earthlink.net/~djmp/ From: Bradley Stoll [mailto:BradleyS at harker.org] To: mathgroup at smc.vnet.net 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