Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Summary: Which[] as Textbook Input, Plot[] Questions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg54069] Re: [mg54037] Summary: Which[] as Textbook Input, Plot[] Questions
  • From: "Igor C. Antonio" <igora at wolfram.com>
  • Date: Wed, 9 Feb 2005 09:27:53 -0500 (EST)
  • Organization: Wolfram Research, Inc.
  • References: <200502081031.FAA17755@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Matt wrote:
> Hello,
>    I apologize if the answer to this is somewhere glaringly obvious in
> the documentation, however, after at least 4 hours pawing through both
> the hardcover Mathematica 4.0 book by Wolfram and the in-program
> Mathematica 4.1 documentation, I cannot find how I would annotate a
> function that takes on different values based upon different domains.
> To wit, something like:
> 
> Clear[f];
> f[x_] := Which[x < 0, Sin[x]/x, x == 0, 1, x > 0, Sin[x]/x];
> Plot[f[x], {x, -pi, pi}, AxesLabel -> {"x", "f[x]"};
> 
> The 'Which' function is great for actually evaluating something, but I
> was looking for something along the lines of traditional mathematical
> notation (such as one would write on a chalkboard or on a sheet of
> paper), where a large left-bracket would be used and the various
> definitions of the function for the various ranges would be
> 'constrained' by the bracket.
> 
> I'll try to illustrate what I mean, where the '|'s that I will use
> should be interpreted as a single, large left-bracket:
> 
>        | Sin(x)/x, x < 0
> f(x) = | 1, x = 0
>        | Sin(x)/x, x > 0
> 
> Is there a way to do what I'm asking in Mathematica 4.1 (or even
> above)?


This functionality is now built-in Mathematica 5.1:

Piecewise construct:
http://documents.wolfram.com/mathematica/functions/Piecewise
http://documents.wolfram.com/mathematica/book/section-3.2.3

Piecewise is tightly integrated with other functions in Mathematica, so 
it's possible to use Integrate, DSolve, and others with piecewise functions.

Before Mathematica 5.1, I believe users implemented their own piecewise 
functions via If, Which, and Switch statements.

> 
> 
> As regards the Plot[] function, I'm puzzled as to why the following
> doesn't give me an error when evaluated:
> 
> Clear[g];
> g[x_] := 1/x;
> Plot[g[x], {x, -5, 5}];
> 
> It seems as though it should, considering that x at zero is undefined.
> However, Mathematica draws the graph as though the function were just
> fine.
> 
> Thanks for any light you can shed on this,

Plot, as well as other plotting functions, samples points over the range 
of the function.  In this case, x==0 is not one of them.  The default 
number of sampled points is 25 and it can be changed via the options 
PlotPoints->.

> 
> Matt

Igor C. Antonio
Wolfram Research, Inc.


  • Prev by Date: Re: Finally, a simple example of a mystery
  • Next by Date: Re: bugs in Mathematica 5.1
  • Previous by thread: Re: Summary: Which[] as Textbook Input, Plot[] Questions
  • Next by thread: Re: Summary: Which[] as Textbook Input, Plot[] Questions