home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!decwrl!purdue!mentor.cc.purdue.edu!seaman.cc.purdue.edu
- From: ags@seaman.cc.purdue.edu (Dave Seaman)
- Newsgroups: sci.math.symbolic
- Subject: Re: Mathematica: defining functions. HELP!
- Message-ID: <56833@mentor.cc.purdue.edu>
- Date: 14 Aug 92 14:30:59 GMT
- References: <l8mkk4INN5ll@halcyon.usc.edu>
- Sender: news@mentor.cc.purdue.edu
- Lines: 20
-
- In article <l8mkk4INN5ll@halcyon.usc.edu> rao@halcyon.usc.edu (Anil Rao)
- writes:
- > define a function f(x) = x between x = 2 and x = 5
- > = x^2/2 for x < 2
- > = 5 x - 20 for x > 5
- >
-
- At least two methods come to mind. One is to qualify the definitions:
-
- f[x_] := x /; 2 <= x <= 5
- f[x_] := x^2/2 /; x < 2
- f[x_] := 5 x - 20 /; x > 5
-
- and another way is to use a one-liner with nested conditionals:
-
- f[x_] := If[ x<2, x^2/2, If[ x<=5, x, 5 x - 20]]
-
- --
- Dave Seaman
- ags@seaman.cc.purdue.edu
-