1
$\begingroup$

My code simply can't solve this. $ x_1^2 + x_2 = 2^2 + 3 = 7 $

assign[eqn_] := 
 Thread[Subscript[x, #] & /@ Range[Length[Solve[eqn, x]]] ==
   Values@Flatten@Solve[eqn, x]];
assign[x^2 - 5 x == -6]

Output

{Subscript[x, 1] == 2, Subscript[x, 2] == 3}

But then when I input

Subscript[x, 1 ]^2 + Subscript[x, 2]

It just simply output what I've input.

$\endgroup$
  • 3
    $\begingroup$ Maybe give Mathematica a bit more guidance: Simplify[ Subscript[x, 1]^2 + Subscript[x, 2], Assumptions -> assign[ x^2 - 5 x == -6 ] ] $\endgroup$ – LouisB Feb 16 at 10:11
4
$\begingroup$
Clear["Global`*"]

Note that Equal (==) does not assign values. You need to use Set (=)

assign[eqn_] := 
  Thread[Subscript[x, #] & /@ Range[Length[Solve[eqn, x]]] == 
     Values@Flatten@Solve[eqn, x]] /. Equal -> Set;

assign[x^2 - 5 x == -6]

(* {2, 3} *)

Subscript[x, 1]^2 + Subscript[x, 2]

(* 7 *)
$\endgroup$
  • $\begingroup$ This is a great trick! Somehow in my 16 years of mathematica use I've never seen the ./ Equal -> Set rule. $\endgroup$ – evanb Feb 16 at 21:04

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