How do I extract a solution from the results of Solve or FindRoot?
 Solve, FindRoot, and other similar functions typically return something like the following. These functions do not return a number, such as 2.5, because sometimes you want to solve for more than one variable. If only numbers were returned, you would never know which number belonged to which variable - especially if each variable had more than one solution. For example: In[1]:= |  |
Out[1]= |
 |
Without showing the attachment between the solution and the variable names, there is no way that you would know for sure which solutions were for x and which ones were for y, or that -0.411438 is a solution for x if and only if 0.911438 is the solution for y. Mathematica is returning 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 evaluate the following. In[2]:= |  |
Out[2]= |
 |
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 variable. In[3]:= |  |
Out[3]= |
 |
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. View an Online Tutorial (Flash Format)
Download this FAQ as a Mathematica 5.2 Notebook
Questions or comments? Send email to support@wolfram.com.
|