1
$\begingroup$

( Did not readily find from Documentation even if used before).

NSolve[ 2 x^3 + x ^2 - 5 x + 2 == 0, x]

How to collect roots with new labels a,b and c?

$\endgroup$
  • 2
    $\begingroup$ Thread[{a, b, c} -> (x /. sol)] or Thread[{a, b, c} -> Last @@@ sol]? $\endgroup$ – kglr Apr 14 '15 at 14:52
  • 1
    $\begingroup$ Have a look Assign output of solve to variables. $\endgroup$ – user9660 Apr 14 '15 at 15:16
  • $\begingroup$ @BobHanlon, @Community, inferring from the accepted answer, the OP wants to turn {{x -> -2.}, {x -> 0.5}, {x -> 1.}} into {{a -> -2.}, {b -> 0.5}, {c -> 1.}} . None of the answers in the linked q/a does that. So I suggest this is not a duplicate. $\endgroup$ – kglr Apr 14 '15 at 20:22
  • $\begingroup$ Trying to put into proper perspective . The output of NSolve is to be collected under new preferred labels. Even the x's can be renamed once isolated. Need not read more into my accepted answer. As OP I requested merely for plain assignment, forgotten stuff reminder as stated.However kguler supplies much more useful content ( yet to run it fully) serving more to herd data under a new label.When duplicated info serves my purpose, I cannot be as OP be indifferent to new detail and I can get back here anytime under old questions reference anyway... $\endgroup$ – Narasimham Apr 14 '15 at 21:43
4
$\begingroup$
sol = NSolve[2 x^3 + x^2 - 5 x + 2 == 0, x]
(* {{x -> -2.}, {x -> 0.5}, {x -> 1.}} *)

Judging from OP's acceptance of this answer, the task is turn sol into

{{a -> -2.}, {b -> 0.5}, {c -> 1.}} 

Here are a few methods, sorted by StringCount, to relabel x

new = {a, b, c};
List /@ Thread[new -> (x /. sol)]
List /@ Thread[new -> Last @@@ sol]
Block[{i = 1}, sol /. x :> new[[i++]]]
List /@ Thread[new -> sol[[All, 1, 2]]]
List /@ MapThread[Rule, {new, x /. sol}]
Block[{i = 1}, sol /. Thread[x :> new[[i++]]]]
Block[{sol2 = sol}, sol2[[All, 1, 1]] = new; sol2]
Block[{i = 1}, sol /. Rule[x, y_] :> Rule[new[[i++]], y]]
Block[{nl = Reverse@new}, sol /. x :> First[nl = RotateRight[nl]]]
Block[{nl = Reverse@new}, Replace[sol, x :> First[nl = RotateRight[nl]], 3]]
Block[{nl = Reverse@new}, MapAt[First[nl = RotateRight[nl]] &, sol, {{All, 1, 1}}]]
Block[{nl = Reverse@new},ReplacePart[sol, {_, 1, 1} :> First[nl = RotateRight[nl]]]]

all give

(* {{a -> -2.}, {b -> 0.5}, {c -> 1.}} *)
$\endgroup$

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