RE: Define a function
- To: mathgroup at smc.vnet.net
- Subject: [mg34567] RE: [mg34556] Define a function
- From: "Annetts, Dave (E&M, North Ryde)" <David.Annetts at csiro.au>
- Date: Wed, 29 May 2002 02:44:06 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Hi Michael,
> I want to define a function in Mathematica. But just
> f[x_]:=... does not
> work, because I have to define different functions for
> different ranges of
> the variable x.
> For example:
>
> x < 10: f(x) = 0
> 10 < x < 20: f(x) = x
> 20 < x: f(x) = x/2
>
> How to do this?
What about the value of f at 10 & 20?
In any case, something like ...
f1[x_] := 0 /; x < 10;
f1[x_] := x /; (10 <= x < 20);
f1[x_] := x/2 /; x >= 20;
should work.
You can also try using nested If[...]'s, Which[...] or Switch ....
Regards,
Dave.
--------------------------------------------------------
Dr. David Annetts EM Modelling Analyst
Tel: (+612) 9490 5416 CSIRO DEM, North Ryde
Fax: (+612) 9490 5467 David.Annetts at csiro.au
Include "usual_disclaimers"
--------------------------------------------------------