MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: piecewise vs which

  • To: mathgroup at smc.vnet.net
  • Subject: [mg60110] Re: piecewise vs which
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Sat, 3 Sep 2005 02:05:58 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, U.K.
  • References: <df9437$620$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

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?

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/mathematica/functions/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.


  • Prev by Date: Re: Matrix/Array Simplification
  • Next by Date: scaling of ndsolve to large systems
  • Previous by thread: Re: piecewise vs which
  • Next by thread: Re: piecewise vs which