Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: mathematica newbie trouble

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99319] Re: [mg99276] mathematica newbie trouble
  • From: "David Park" <djmpark at comcast.net>
  • Date: Sat, 2 May 2009 06:02:29 -0400 (EDT)
  • References: <33135270.1241172362785.JavaMail.root@n11>

It's the SetDelayed on the k definition that causes the problem. Mathematica
does not know that s is a function of a, b and c. When k is evaluated with
numerical values a, b and c will only be substitutes where they explicitly
appear in the rhs expression of the k definition. When Set is used the s
expression is evaluated first and is part of the rhs definition. 

But more basically, I think it is poor practice to write expressions such as
s = something, and then use it in subsequent definitions. It is better to
localize it to the k definition. After all, it goes with it. So I would use
something like the following:

k[a_, b_, c_] :=
 With[{s = (a + b + c)/2},
  Sqrt[s (s - a) (s - b) (s - c)]]


David Park
djmpark at comcast.net
http://home.comcast.net/~djmpark/  



From: Guapo [mailto:yangshuai at gmail.com] 


i wrote the following mathematica code:

s := (a + b + c)/2;
k[a_, b_, c_] := Sqrt[s (s - a) (s - b) (s - c)];
k[3, 4, 5]
k[5, 9, 12]

when run it, i can't get the write answer. but i change setDelayed(:=)
to set(=), everything works ok

s = (a + b + c)/2;
k[a_, b_, c_] = Sqrt[s (s - a) (s - b) (s - c)];
k[3, 4, 5]
k[5, 9, 12]

i did a google search for the difference of set and setDelayed,
however, i still can't understand it for the upper described problem,
could anyone explain it? great thanks.




  • Prev by Date: Re: Ten chess-players...
  • Next by Date: Re: mathematica newbie trouble
  • Previous by thread: Re: mathematica newbie trouble
  • Next by thread: Re: mathematica newbie trouble