home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / sci / math / symbolic / 2201 < prev    next >
Encoding:
Internet Message Format  |  1992-08-14  |  897 b 

  1. Path: sparky!uunet!decwrl!purdue!mentor.cc.purdue.edu!seaman.cc.purdue.edu
  2. From: ags@seaman.cc.purdue.edu (Dave Seaman)
  3. Newsgroups: sci.math.symbolic
  4. Subject: Re: Mathematica: defining functions. HELP!
  5. Message-ID: <56833@mentor.cc.purdue.edu>
  6. Date: 14 Aug 92 14:30:59 GMT
  7. References: <l8mkk4INN5ll@halcyon.usc.edu>
  8. Sender: news@mentor.cc.purdue.edu
  9. Lines: 20
  10.  
  11. In article <l8mkk4INN5ll@halcyon.usc.edu> rao@halcyon.usc.edu (Anil Rao)  
  12. writes:
  13. > define a function f(x) = x between x = 2 and x = 5
  14. >                        = x^2/2 for x < 2
  15. >                        = 5 x - 20 for x > 5
  16.  
  17. At least two methods come to mind. One is to qualify the definitions:
  18.  
  19.     f[x_] := x /; 2 <= x <= 5
  20.     f[x_] := x^2/2 /; x < 2
  21.     f[x_] := 5 x - 20 /; x > 5
  22.  
  23. and another way is to use a one-liner with nested conditionals:
  24.  
  25.     f[x_] := If[ x<2, x^2/2, If[ x<=5, x, 5 x - 20]]
  26.  
  27. --
  28. Dave Seaman
  29. ags@seaman.cc.purdue.edu
  30.