|
[Date Index]
[Thread Index]
[Author Index]
Re: Fuction definition
- To: mathgroup at smc.vnet.net
- Subject: [mg73686] Re: Fuction definition
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Sat, 24 Feb 2007 02:26:20 -0500 (EST)
- Organization: The Open University, Milton Keynes, UK
- References: <erme23$i63$1@smc.vnet.net>
bar at ANTYSPAM.ap.krakow.pl wrote:
> Hi
>
> When I try :
> ------------------
> p = 1;
> f1[x_] := p Sin[Pi x];
> p = 2;
> f2[x_] := p Sin[Pi x];
>
> f1[0.5]
> f2[0.5]
>
> Out[26]=2.
>
> Out[27]=2.
> -----------------
> When I use = instead of := it works OK.
> Why ?
SetDelayed, :=, evaluates the body of the definition only when the the
function is called. Therefore, f1 looks at the value of p only when it
is called by f1[0.5]. At that time, the current value of p is 2, not 1.
In[1]:=
p = 1;
f1[x_] := p*Sin[Pi*x];
f1[0.5]
Out[3]=
1.
In[4]:=
p = 2;
f2[x_] := p*Sin[Pi*x];
f1[0.5]
f2[0.5]
Out[6]=
2.
Out[7]=
2.
> It is safely to use "=" in function definition ?
The usual way for defining a function is to use SetDelayed, not Set.
> I have to integrate and differentiate such function in the future.
> In my case function depends on about 20 parameters, which are calculated
> three times, (I want to obtain three functions)
You could define your functions in the following way:
In[8]:=
f3[p_][x_] := p*Sin[Pi*x];
f3[4][0.5]
Out[9]=
4.
> I'm not sure is this good idea to use '=' ?
Short answer: no, not for defining a function.
> Regards , Olaf
Regards,
Jean-Marc
Prev by Date:
Re: Fw: 2
Next by Date:
Re: Fuction definition
Previous by thread:
Re: Fw: Re: Fw: 2
Next by thread:
Re: Fuction definition
|