Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I understand Mathematica can't assign the results of a Solve to the unknowns because there may be more than 1 solution. How can I assign the 4 values of following result to variables?

enter image description here

share|improve this question
Could you elaborate on what you are trying to achieve? In principle you can use Replace (or Part) to assign the values to variables. –  sebhofer Jun 11 '12 at 13:36
@sebhofer - I want to assign the first x value to a variable X1, then the y value to Y1, the other two to X2 and Y2. I just don't seem to get the hang of references yet. –  stevenvh Jun 11 '12 at 13:40

3 Answers 3

up vote 10 down vote accepted

You can do this :

s = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}];
xx = s[[All, 1, 2]];
yy = s[[All, 2, 2]];

Now you can access solutions, this way xx[[1]], yy[[2]].

If you prefer to collect solutions in Array, there is another way :

X = Array[ x, {Length@s}];
Y = Array[ y, {Length@s}];
x[k_] /; MemberQ[ Range[ Length @ s], k] := s[[k, 1, 2]]
y[k_] /; MemberQ[ Range[ Length @ s], k] := s[[k, 2, 2]]

now X is equivalent to s[[All, 1, 2]], while Y to s[[All, 2, 2]], e.g. :

X[[1]] == x[1]
Y == s[[All, 2, 2]]

You do not have to use or even to define X and Y arrays, e.g.

{x[1], y[1]}
{(-11181 - Sqrt[2242057])/74498, 1/386 (13 - Sqrt[2242057])}

We've used Condition i.e. /; to assure definitions of x[i], y[i] only for i in an appropriate range determined by Length @ s, i.e. number of solutions.

share|improve this answer
I think the OP is a complete beginner, and all he's looking for is ReplaceAll. This might be a bit too advanced for someone new to Mma. –  Szabolcs Jun 11 '12 at 13:57
@Szabolcs Literally there is the assignment tag, so he is rather looking for Set or SetDelayed applications. –  Artes Jun 11 '12 at 16:07
I think I'll use your first solution for now, until I get the hang of ReplaceAll. @Szabolcs: Yes, Mma virgin. Thanks, all. –  stevenvh Jun 12 '12 at 7:09
@stevenvh I think this answer can be also interesting for you : mathematica.stackexchange.com/questions/1819/… –  Artes Jul 12 '12 at 11:17

Usually you don't want to actually assign values to x and y, and you would use replacement rules instead:

sols = Solve[y^2 == 13 x + 17 && y == 193 x + 29, {x, y}];

{x, y} /. sols[[1]]

or for the second solution:

{x, y} /. sols[[2]]

If you really want to assign values to x and y globally, you could use:

Set @@@ sols[[1]]

but you must clear x and y before using another set:

Clear[x, y]
Set @@@ sols[[2]]

If you want to assign values to x and y within a Block you could do something like this:

Hold @@ {sols[[2]]} /. Rule -> Set /. _[vars_] :>
   Sin[x] + Sqrt[y] // N

This uses what I am calling the injector pattern to get the values into Block in the right syntax without it prematurely evaluating.

Related questions:

Getting rid of the “x ->” in FindInstance results

Using the output of Solve

share|improve this answer
+1.Something related to your last comment and the injector pattern. –  Leonid Shifrin Jun 11 '12 at 14:05

If you really wish to assign solutions to variables, you can do something like this:

In[1]:= ClearAll[Subscript]


Subscript[y,2]=1/386 (13+Sqrt[2242057])

Then you can use the solutions for demonstration purposes:

enter image description here

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.