MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Define a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg34582] Re: [mg34556] Define a function
  • From: BobHanlon at aol.com
  • Date: Wed, 29 May 2002 02:44:29 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

In a message dated 5/27/02 2:19:21 AM, popp.michael at gmx.at writes:

>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?
>

Clear[f];
f[x_ /; x<10] = 0;
f[x_ /; 10<=x<20] := x;
f[x_ /; 20<=x] := x/2;

Plot[f[x], {x,5,40}];

Clear[f];
f[x_] := x*(UnitStep[x-10]-UnitStep[x-20]/2);

Plot[f[x], {x,5,40}];


Bob Hanlon
Chantilly, VA  USA


  • Prev by Date: Re: Define a function
  • Next by Date: Re: basic operations in matrices
  • Previous by thread: Re: Define a function
  • Next by thread: RE: Define a function