# How to evaluate {Subscript[x, 1] == 2, Subscript[x, 2] == 3} , $x_1^2+x_2=2^2+3=7$ [closed]

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.

• Maybe give Mathematica a bit more guidance: Simplify[ Subscript[x, 1]^2 + Subscript[x, 2], Assumptions -> assign[ x^2 - 5 x == -6 ] ]  – LouisB Feb 16 at 10:11

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 *)

• This is a great trick! Somehow in my 16 years of mathematica use I've never seen the ./ Equal -> Set` rule. – evanb Feb 16 at 21:04