# Substituting values of variables in solution of a system of linear equations

After solving a system of 21 linear equations in 21 variables (called Xi in my code) I got the values for all the 21 variables, in the following way:

{X21 -> 2./q^2,
X20 -> (2. - n q X21)/(m q),
X19 -> (2. - m n X20 - n (m X20 + n X21))/m^2,
X18 -> 2./(l q),
X17 -> (2. - l n X18)/(l m),
X16 -> 2./l^2,
X15 -> (2. - g q X18)/(f q),
X14 -> (2. - f n X15 - g (m X17 + n X18))/(f m),
X13 -> (2. - g l X16)/(f l),
X12 -> (2. - f g X13 - g (f X13 + g X16))/f^2,
X11 -> 2./(e q),
X10 -> (2. - e n X11)/(e m),
X9 -> 2./(e l),
X8 -> (2. - e g X9)/(e f),
X7 -> 2./e^2,
X6 -> (2. - b q X11)/(a q),
X5 -> (2. - b (m X10 + n X11) - a n X6)/(a m),
X4 -> (2. - b l X9)/(a l),
X3 -> (2. - a g X4 - b (f X8 + g X9))/(a f),
X2 -> (2. - b e X7)/(a e),
X1 -> (2. - a b X2 - b (a X2 + b X7))/a^2}


The latin letters a,b..q are only to be considered as constant parameters. What I would like to do is to express EACH and EVERY value of the variables as a function of only letters. This is possible, because each Xi variable depends on ONLY the variables from X21 to Xi, therefore in principle the problem boils down to a simple substitution.

Nevertheless, I find it difficult to operate this massive substitution in a concise and elegant way, which eventually should produce an array where each entry is given by the value of one variable Xi, expressed in terms of a,b,...q.

• "solving a system of 21 linear equations in 21 variables" - I'm either going blind, or you only have 13 solutions. – J. M. will be back soon May 12 '16 at 13:35
• @J.M.they are indeed 21. I edited my post to make it clearer – johnhenry May 12 '16 at 13:52
• Solve[Equal @@@ list, First /@ list] should do the trick. – J. M. will be back soon May 12 '16 at 13:58
• As a minor change to @J.M. answer: "Solve[Equal @@@ (list // Rationalize), First /@ list] // Simplify" – Bob Hanlon May 12 '16 at 14:05
• @J.M. and Bob Hanlon. I know I am not supposed to do this but +1, these are really great answers! – Jack LaVigne May 12 '16 at 14:45

list = {X21 -> 2./q^2, X20 -> (2. - n q X21)/(m q),
X19 -> (2. - m n X20 - n (m X20 + n X21))/m^2, X18 -> 2./(l q),
X17 -> (2. - l n X18)/(l m), X16 -> 2./l^2, X15 -> (2. - g q X18)/(f q),
X14 -> (2. - f n X15 - g (m X17 + n X18))/(f m),
X13 -> (2. - g l X16)/(f l), X12 -> (2. - f g X13 - g (f X13 + g X16))/f^2,
X11 -> 2./(e q), X10 -> (2. - e n X11)/(e m), X9 -> 2./(e l),
X8 -> (2. - e g X9)/(e f), X7 -> 2./e^2, X6 -> (2. - b q X11)/(a q),
X5 -> (2. - b (m X10 + n X11) - a n X6)/(a m), X4 -> (2. - b l X9)/(a l),
X3 -> (2. - a g X4 - b (f X8 + g X9))/(a f), X2 -> (2. - b e X7)/(a e),
X1 -> (2. - a b X2 - b (a X2 + b X7))/a^2};


Starting with J.M.'s comment

Solve[Equal @@@ list, First /@ list] // Simplify


The result is cleaner if the numeric values are rationalized

Solve[Equal @@@ (list // Rationalize), First /@ list] // Simplify