Re: Define a function
- To: mathgroup at smc.vnet.net
- Subject: [mg34590] Re: Define a function
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Wed, 29 May 2002 02:44:47 -0400 (EDT)
- References: <acshar$ke2$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Michael ( I change some < to <= to get a complete definitio)
f[x_/;x<10]:= 0
f[x_/;10<=x<20]:=x
f[x_/; 20<=x]:= x/t
We can simplify to
f[x_ /; x < 10] := 0
f[x_ /; x < 20] := x
f[x_] := x/2
since the rules are tested in order for applicability.
Another way is to use Which:
f[x_] :=
Which[
x < 10, 0,
x < 20, x,
True, x/2
]
--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
"Michael Popp" <popp.michael at gmx.at> wrote in message
news:acshar$ke2$1 at smc.vnet.net...
> 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
>
>
>
>