Re: Function definition
- To: mathgroup at smc.vnet.net
- Subject: [mg15786] Re: Function definition
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sun, 7 Feb 1999 02:04:03 -0500 (EST)
- References: <79ea4l$9hm@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Rangachari Kidambi wrote in message <79ea4l$9hm at smc.vnet.net>...
>
>Hi,
>
>I need to define a function f(theta,phi) as
>
>f(theta,phi) = f1 if 0 <= theta <= theta0, 0 <= phi <= phi0 f(theta,phi)
>= f2 otherwise
>
>where theta, phi are spherical polar coordinates, <= is less than or
>equal to and theta0 and phi0 are constants.
>
>I have tried using Do loops and If statements but haven't been
>successful.
>
>Thanks for any help
>Ranga Kidambi
>
>
Ranga:
Here are three ways. I have put 2 for theta0 and 3 for phi0 so that I
can test the definitions.
In[1]:=
Clear[f]
(Way 1)
In[2]:=
f[theta_?(0<=#<=2 &),
phi_?(0<=#<=3&)]:= f1[theta, phi];
f[theta_,phi_]:= f2[theta, phi];
In[3]:=
{f[1,2],f[3,2]}
Out[3]=
{f1[1,2],f2[3,2]}
In[4]:=
Clear[f]
(Way 2)
In[5]:=
f[theta_/;(0<=theta<=2 ),
phi_/;(0<=phi<=3)]:= f1[theta, phi]; f[theta_,phi_]:= f2[theta, phi];
In[6]:=
{f[1,2],f[3,2]}
Out[6]=
{f1[1,2],f2[3,2]}
In[7]:=
Clear[f];
(Way 3)
In[8]:=
f[theta_,phi_]:=
Which[
(0<=theta<=2 )&&(0<=phi<=3), f1[theta, phi],
True,f2[theta, phi]
]
In[9]:=
{f[1,2],f[3,2]}
Out[9]=
{f1[1,2],f2[3,2]}
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565