RE: Define a function
- To: mathgroup at smc.vnet.net
- Subject: [mg34570] RE: [mg34556] Define a function
- From: "DrBob" <majort at cox-internet.com>
- Date: Wed, 29 May 2002 02:44:10 -0400 (EDT)
- Reply-to: <drbob at bigfoot.com>
- Sender: owner-wri-mathgroup at wolfram.com
Here's a simple solution: f[x_] := Which[ x < 10, 0, x < 20, x, True, x/2 ] Plot[f[x], {x, 0, 30}] And here's another: ClearAll[f] f[x_] /; x < 10 := 0 f[x_] /; x < 20 := x f[x_] := x/2 Plot[f[x], {x, 0, 30}] >>I have to define different functions for different ranges of the variable x. It's a subtle point perhaps, but in both cases f is ONE function, defined for all x. (REALLY all x, the way I've done it...) For instance, evaluate f["2"] f[""] f[{1,2}] What you mean is that the functional form, expression, or computation rule is different, for different ranges of the variable. Bobby Treat -----Original Message----- From: Michael Popp [mailto:popp.michael at gmx.at] To: mathgroup at smc.vnet.net Subject: [mg34570] [mg34556] Define a function Hello 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? Thanks, greetings Michael