12
$\begingroup$

This question already has an answer here:

I have this input:

Solve[x^2 + 3 x + 2 == 0, x]

which gives this output:

{{x -> -2}, {x -> -1}}

I want the first x to be named x1 and the second x to be named x2 without having to copy the value and doing x1=-2 manually and x2=-1

$\endgroup$

marked as duplicate by Artes, eldo, Dr. belisarius, Sjoerd C. de Vries, Jens Sep 20 '14 at 0:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

12
$\begingroup$
{x1, x2} = x /. Solve[x^2 + 3 x + 2 == 0, x]
{-2, -1}
$\endgroup$
  • $\begingroup$ loved it! Thanks! $\endgroup$ – Onizuka Sep 19 '14 at 17:43
4
$\begingroup$
{x1, x2} = Last @@@ Solve[x^2 + 3 x + 2 == 0, x]
(* {-2, -1} *)

or

{x1, x2} = Solve[x^2 + 3 x + 2 == 0, x][[All,1,-1]]
(* {-2, -1} *)

or

sol = Solve[x^2 + 3 x + 2 == 0, x];
sol[[All, 0]] = Last;
{x1, x2} = sol 
(* {-2, -1} *)

Note: Since this question has a much simpler structure than the question linked by Artes, these tricks work for the current case (with a single-variable expression to be solved), but not for the more general case in the linked Q/A.

$\endgroup$
  • $\begingroup$ Can't you see it's a duplicate? $\endgroup$ – Artes Sep 19 '14 at 20:18
  • $\begingroup$ @Artes, -- now that i saw your comment -- yes. $\endgroup$ – kglr Sep 19 '14 at 20:35
2
$\begingroup$

A certain generalization using an indexed variable:

r = FindInstance[Sin[x] == Cos[x] && -10 < x < 10, x, Reals, 15] // Values // Flatten // N

{-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, -2.35619}

Map[(x[#] = r[[#]]) &, Range @ Length @ r];

{x[1], x[2], x[3], x[4], x[5]}

{-5.49779, 7.06858, 0.785398, -8.63938, 3.92699, -2.35619}

$\endgroup$

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