MathGroup Archive 1996

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

Search the Archive

Re: ???? How to assign result from Solve to variable ????

  • To: mathgroup at
  • Subject: [mg3846] Re: ???? How to assign result from Solve to variable ????
  • From: ianc (Ian Collier)
  • Date: Mon, 29 Apr 1996 00:33:02 -0400
  • Organization: Wolfram Research, Inc.
  • Sender: owner-wri-mathgroup at

In article <4let71$e4m at>, rpiyaket at (Ram
Piyaket) wrote:

> Hi,
> I was wondering if anyone could tell me how to deal with this problem.
> I want to assign the numerical result that I obtain from the Solve or etc. 
> command, which typically enclosed in the braces ({...}) to a variable
> so that I can reuse it. I just want the numerical number, not the braces.
> Can you tell me how? I appreciate all the help from you. Thank you very much.
> Ram

The following is taken from the Technical Support FAQ area 
of Wolfram Research's Web pages.  The exact URL is:

How do I extract a solution from the results of Solve or FindRoot?

Solve and FindRoot typically return something like the following: 

 {{x -> 2.5}}

At first, it may seem a little confusing, but there is a very 
good reason for it. The reason Solve or FindRoot doesn't simply 
return a number, such as 2.5, is because sometimes you want to 
solve for more than one variable. If Mathematica just returned 
numbers, you would never know which number belonged to which
variable -- especially, if each variable had more than one 
solution. For example: 

In[1]:= Solve[{x^2 + y^2 == 1, x + y==.5},{x,y}]

Out[1]= {{x -> -0.411438, y -> 0.911438},
          {x -> 0.911438, y -> -0.411438}}

Without showing the attachment between the solution and the 
variable names, there is no way that we would know for sure 
which solutions were for x and which ones were for y, or that 
-.411438 is a solution for x if and only if .911438 is the
solution for y. 

What Mathematica is returning is a collection of rules. Rules 
are a well-defined and well-documented concept in Mathematica, 
and they are easy to extract. If you want to pull all of the 
rules for x out of the result of the above Solve command, you
can do the following: 

In[2]:= x /. %

Out[2]= {-0.411438, 0.911438}

The /. is shorthand for ReplaceAll, and causes Mathematica to 
replace all instances of x on the left-hand side with the rule(s) 
for x on the right-hand side. In fact, the left-hand side can be 
any expression of x, y, or any other variables: 

In[3]:= x^2 - y/2 + x y z /. %%

Out[3]= {-0.286438 - 0.375 z, 1.03644 - 0.375 z}

This returns the expression evaluated at each of the two 
solutions; note that z remains unevaluated in the solution because 
z was not a rule. 

This and many other frequently asked questions about Mathematica
is answered in the Technical Support FAQ area of the Wolfram 
Research Web pages <>.

I hope this helps.


Ian Collier
Wolfram Research, Inc.
tel:(217) 398-0700   fax:(217) 398-0747    ianc at
Wolfram Research Home Page:


  • Prev by Date: implicit equations
  • Next by Date: Re: Help on Function
  • Previous by thread: ???? How to assign result from Solve to variable ????
  • Next by thread: Re: ???? How to assign result from Solve to variable ????