RE: Define a function
- To: mathgroup at smc.vnet.net
- Subject: [mg34595] RE: [mg34556] Define a function
- From: "David Park" <djmp at earthlink.net>
- Date: Wed, 29 May 2002 02:45:09 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Michael, The best method for defining piecewise functions is to use UnitStep. f[x_] := x(UnitStep[x - 10] - UnitStep[x - 20]) + x/2UnitStep[x - 20] This will not only plot... Plot[f[x], {x, 0, 30}]; but you can also do things like integrate. g[x_] = Integrate[f[x], x] (-(1/4))*(-400 + x^2)*UnitStep[-20 + x] + (-50 + x^2/2)*UnitStep[-10 + x] Plot[g[x], {x, 0, 30}]; The Mathematica Book, which has not really been updated properly to reflect the UnitStep function often leads users to using conditional definitions. Clear[f] f[x_] /; x < 10 := 0; f[x_] /; 10 <= x <= 20 := x; f[x_] /; 20 < x := x/2; This works for plotting... Plot[f[x], {x, 0, 30}]; but won't integrate or perform other calculus functions. Integrate[f[x], x] Integrate[f[x], x] So, the UnitStep method is far superior. David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ > -----Original Message----- > From: Michael Popp [mailto:popp.michael at gmx.at] To: mathgroup at smc.vnet.net > Sent: Monday, May 27, 2002 1:17 AM > Subject: [mg34595] [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 > > > >