MathGroup Archive 2006

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

Search the Archive

Re: Using "=" vs ":="


misha wrote:
> I'm going through Mathematic by Example, 2nd ed., (Abell and Braselton), 
> and have come across something that puzzles me.
> 
> Chapter 2, Section 2, Example 8
> Define f(x,y)=1-sin(x^2+y^2)
> 
> So I first try,
> In[1]:= f[x_, y_]:=1-Sin(x^2+y^2)
> No problem so far...
> Then,
> In[2]:= f[x,y]
> Out[2]:=1-Sin(x^2+y^2)
> Still no problem...
> Then,
> In[3]:=f[1,2]
> Out[3]:=1-5 Sin
> 
> Huh?

This has nothing to do with Set vs. SetDelayed. You are using the wrong 
syntax. See "Your First Mathematica Calculations" at 
http://documents.wolfram.com/mathematica/GettingStarted/StartingOut/YourFirstMathematicaCalculations.html

*Built-in functions are capitalized. Arguments to functions are wrapped 
with /square/ brackets. Sin[x]*

In[1]:=
f[x_, y_] := 1 - Sin[x^2 + y^2]

In[2]:=
f[x, y]

Out[2]=
          2    2
1 - Sin[x  + y ]

In[3]:=
f[1, 2]

Out[3]=
1 - Sin[5]

In[4]:=
N[%]

Out[4]=
1.95892

> I noticed that rather than using ":=" to "simply define" this function, 
> as opposed to (just) "=" to "define and compute" this function, I get 
> different subsequent behavior.  Specifically, doing the above with just 
> "=", works fine.
> In[1]:= f[x_, y_]=1-Sin(x^2+y^2)
> ....
> In[3]:=f[1,2]
> Out[3]:=1-Sin[5]
> 
> My question is, Why?  What's the difference between ":=" and "=" for 
> defining functions?
> 
> Thanks!
> Misha
> 

For example,

In[5]:=
g = Random[]

Out[5]=
0.624668

In[6]:=
{g, g, g}

Out[6]=
{0.624668, 0.624668, 0.624668}

In[7]:=
h := Random[]

In[8]:=
{h, h, h}

Out[8]=
{0.17459, 0.0433796, 0.504572}

Regards,
Jean-Marc


  • Prev by Date: Re: Using "=" vs ":="
  • Next by Date: Re: Re: Problem in loading packages in Mathematica
  • Previous by thread: Re: Using "=" vs ":="
  • Next by thread: Re: Using "=" vs ":="