MathGroup Archive 2003

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

Search the Archive

RE: Confusion between rules and variables

  • To: mathgroup at smc.vnet.net
  • Subject: [mg42418] RE: [mg42416] Confusion between rules and variables
  • From: "David Park" <djmp at earthlink.net>
  • Date: Sun, 6 Jul 2003 06:57:01 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Jonathan,

Clear[x];
FindRoot[Sin[x] == x, {x, 0.5}]
x = x /. %

{x -> 0.0129087}
0.0129087

(/. is the shortcut notation for ReplaceAll)

But the reason that Mathematica returns rules is that they are convenient to
use and it avoids setting values to simple variables. Setting values to
simple variables and then forgetting to Clear them when you want to use them
again as a symbol is probably the single most common error in using
Mathematica. That is why I put the Clear[x] statement above. If you
evaluated the FindRoot again without clearing you would obtain a strange
result.

FindRoot[Sin[x] == x, {x, 0.5}]
{0.0129087 -> 0.0129087}

If instead you save the solution as a rule you can then use it as in the
following example.

Clear[x];
xsol = FindRoot[Sin[x] == x, {x, 0.5}]
{x -> 0.0129087}

x^2 + Tanh[x]/Sqrt[x]
% /. xsol

x^2 + Tanh[x]/Sqrt[x]
0.113777

and you are free to continue using x as a symbolic variable. And you would
never have had to use a Clear[x] at all.

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/




From: Jonathan Greenberg [mailto:greenberg at ucdavis.edu]
To: mathgroup at smc.vnet.net

I ran a FindRoot where the output was {x->0.543} -- How do I extact that
value (0.543) for use in some other equation (e.g. How do I set a variable
equal to 0.543, instead of that entire rule)?

--j


  • Prev by Date: RE: Confusion between rules and variables
  • Next by Date: Why is it so???
  • Previous by thread: RE: Confusion between rules and variables
  • Next by thread: Speed improvements in Mathematica 5 ??