1

( 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?

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Narasimham
  • 3,160
  • 13
  • 26
  • 2
    Thread[{a, b, c} -> (x /. sol)] or Thread[{a, b, c} -> Last @@@ sol]? – kglr Apr 14 '15 at 14:52
  • 1
  • @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. – kglr Apr 14 '15 at 20:22
  • 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... – Narasimham Apr 14 '15 at 21:43

1 Answers1

4
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.}} *)
kglr
  • 394,356
  • 18
  • 477
  • 896