Exercise: Solving linear equation systems by hand
Goal
After this exercise you will be able to:
- Translate a small biology mixture problem into a system of linear equations and write it as \(A\mathbf{x} = \mathbf{b}\).
- Solve \(2\times 2\) and \(3\times 3\) systems by hand using substitution and Gaussian elimination with an augmented matrix.
- Compute the inverse of a \(2\times 2\) matrix by row-reducing \([\,A \mid I\,]\) to \([\,I \mid A^{-1}\,]\), and use it to solve for several measurement vectors.
- Compute the determinant \(ad - bc\) of a \(2\times 2\) matrix and use it to tell whether the system has exactly one solution.
- Check a solution by substituting it back into the original equations.
Everything here is done with pen and paper. You will check these answers in R in Linear systems in R.
Background
A bulk sample is a mixture of cell types. If we know, for each cell type, how much signal it contributes to each measured gene (its signature), then the total measured signal is a weighted sum of the unknown cell-type amounts. With as many genes measured as cell types present, recovering the amounts is exactly solving a square linear system.
In standard form, \[ A\mathbf{x} = \mathbf{b}, \] where the column \(j\) of \(A\) is the signature of cell type \(j\), the entry \(x_j\) is the unknown amount of cell type \(j\), and \(\mathbf{b}\) holds the bulk measurements. Row \(i\) of the system reads \[ A_{i1}x_1 + A_{i2}x_2 + \dots + A_{in}x_n = b_i, \] the total signal for gene \(i\).
Two solution methods:
- Substitution — solve one equation for one unknown and substitute into the others. Good for \(2\times 2\).
- Gaussian elimination — use row operations (multiply a row by a non-zero number; add a multiple of one row to another) to reach upper-triangular form, then back-substitute from the bottom row up. Scales to any size.
By hand
Do these on paper. Show the row operations, not just the final numbers.
Problem 1 — a 2×2 mixture (substitution)
A blood sample is a mix of two cell types. Per unit, their signatures for genes G1 and G2 are:
| Type A | Type B | |
|---|---|---|
| G1 | 3 | 1 |
| G2 | 1 | 2 |
The bulk measurements are G1 \(= 11\) and G2 \(= 7\).
- Write the system as two equations and as \(A\mathbf{x} = \mathbf{b}\).
- Solve for the amounts \(x\) (Type A) and \(y\) (Type B) by substitution.
- Check your answer in both original equations.
Problem 2 — the same 2×2 by Gaussian elimination
Re-solve Problem 1 using Gaussian elimination on the augmented matrix \([\,A \mid \mathbf{b}\,]\). Reach upper-triangular form, then back-substitute. You should of course get the same answer as in Problem 1.
Problem 3 — a 3×3 mixture (Gaussian elimination)
Three cell types (amounts \(x\), \(y\), \(z\)), three marker genes give the system \[ \begin{aligned} 1x + 2y + 1z &= 8\\ 2x + 1y + 1z &= 7\\ 1x + 1y + 3z &= 10 \end{aligned} \]
- Write the augmented matrix \([\,A \mid \mathbf{b}\,]\).
- Eliminate column 1, then column 2, to reach upper-triangular form.
- Back-substitute to find \(x\), \(y\), \(z\), and check all three equations.
Problem 4 — the inverse with an augmented matrix
Use the signature matrix \(A\) from Problem 1.
- Write the augmented matrix \([\,A \mid I\,]\) and row-reduce it until the left side is \(I\). Read off \(A^{-1}\).
- Check that \(A^{-1}\mathbf{b}\) with \(\mathbf{b} = (11, 7)\) gives your answer from Problem 1.
- A second blood sample, with the same two cell types, measures G1 \(= 8\) and G2 \(= 6\). Find its amounts with one matrix–vector multiplication, and check them.
Problem 5 — the determinant
- Compute \(\det A = ad - bc\) for the signature matrix \(A\) from Problem 1. Use the \(2\times2\) formula \(A^{-1} = \frac{1}{ad - bc}\begin{pmatrix} d & -b \\ -c & a \end{pmatrix}\) and compare with your answer to Problem 4.
- Two other cell types have signatures that give \(B = \begin{pmatrix} 2 & 4 \\ 1 & 2 \end{pmatrix}\). Compute \(\det B\). What does it say about the columns of \(B\), and about how many solutions \(B\mathbf{x} = \mathbf{b}\) can have?
Explore
Write the two equations as \(ax + by = c\) and \(dx + ey = f\) (here \(x\) and \(y\) are the amounts from Problem 1). Each equation is a line in the \((x, y)\) plane, and a solution is a point on both lines. The picture below starts with the system from Problem 1 (R1 blue, R2 orange). Use the sliders, or drag the open circle on a line to shift it and the filled circle to turn it. The grey box shows the elimination step \(\text{R2} - m\,\text{R1}\) with the current numbers.
E1. Compare the crossing point and the grey box with your answers to Problems 1 and 2.
E2. Keep R1 and set \(d = 1.5\), \(e = 0.5\). Now move only \(f\). For which value of \(f\) are there infinitely many solutions? What does elimination leave for any other value, for example \(f = 4\)?
E3. Click reset. Explain why changing only \(f\) can never give a system with no solution. How far does the crossing point move when \(f\) changes by \(0.5\)?
E4. Click nearly parallel (\(x + y = 2\), \(x + 1.1y = 2.5\)). Where do the lines cross? Move \(f\) from \(2.5\) to \(3\). Where is the crossing now, and how does the jump compare with E3? What would this mean for a mixture of two cell types with almost the same signature?
E1. The lines cross at \((x, y) = (3, 2)\), the answer to Problem 1. The box shows \(\text{R2} - (1/3)\,\text{R1}\), which leaves \(0x + 1.667y = 3.333\), that is \(\tfrac53 y = \tfrac{10}{3}\) as in Problem 2, so \(y = 2\) and \(x = 3\).
E2. \(f = 5.5\). Then R2 \(= 1.5x + 0.5y = 5.5\) is exactly \(\tfrac12\times\)R1, the same line drawn twice. \(\text{R2} - \tfrac12\text{R1}\) leaves \(0 = 0\), so \(y\) is free and every point on the line is a solution. For any other \(f\) the lines are parallel and distinct: with \(f = 4\) the step leaves \(0 = 4 - 5.5 = -1.5\), which is impossible, so there is no solution.
E3. \(f\) only shifts line R2 sideways; its slope stays the same. Two lines with different slopes always cross exactly once, wherever they are. A change of \(0.5\) in \(f\) moves the crossing from \((3, 2)\) to \((2.9, 2.3)\), a distance of about \(0.32\).
E4. They cross at \((-3, 5)\). With \(f = 3\) the crossing jumps to \((-8, 10)\), off the plot: a move of about \(7.07\) for the same change of \(0.5\), more than 20 times as far as in E3. The lines are only \(2.7^\circ\) apart, so a small shift of one line slides the crossing a long way along the other. For a mixture, two almost identical signatures mean a little measurement noise in \(\mathbf{b}\) gives very different estimated amounts.
Elimination step by step
The demo below does Gaussian elimination on \([\,A \mid \mathbf{b}\,]\) one row operation at a time, starting on the system of Problem 3. Each step names the operation, shows the matrix before and after (pivot circled, changed row in yellow, the new 0 boxed in green) and writes out the row arithmetic. Then it back-substitutes and checks. You can type in any system up to \(4\times 4\) (fractions like 1/3 work) or ask for a random one.
E5. Do Problem 3 on paper first, then step through it with next ▶. Compare every row operation and every number with yours. Which step brings in the first fraction, and why?
E6. Click needs a swap, no solution and infinitely many (any size). In each case, find the step where elimination shows you which case you are in, and say what you see in the matrix.
E5. The steps are \(\text{R2} \leftarrow \text{R2} - 2\,\text{R1}\), \(\text{R3} \leftarrow \text{R3} - \text{R1}\) and \(\text{R3} \leftarrow \text{R3} - \tfrac13\,\text{R2}\), then back-substitution gives \(z = \tfrac{15}{7}\), \(y = \tfrac{16}{7}\), \(x = \tfrac{9}{7}\), as in the solution to Problem 3. The first fraction comes with the third step: the number to remove is \(-1\) and the pivot is \(-3\), so the multiplier is \(-1 \div (-3) = \tfrac13\).
E6. Needs a swap: a pivot spot holds 0, and since we cannot divide by 0 the demo swaps in a row below it that has a non-zero number there. No solution: a row becomes \(0 \;\ldots\; 0 \mid c\) with \(c \neq 0\), which reads \(0 = c\) and can never be true. Infinitely many: a row becomes all zeros, \(0 = 0\), so there are fewer real equations than unknowns; an unknown without a pivot is free, and every value of it gives a solution.
The inverse step by step
The same kind of stepper for Problem 4: it row-reduces \([\,A \mid I\,]\) all the way to \([\,I \mid A^{-1}\,]\), including dividing each row by its pivot and clearing the entries above the pivots, and ends with the check \(A\,A^{-1} = I\).
E7. Step through Problem 4 and compare with your \(A^{-1}\). Then change the bottom-right entry of \(A\) from \(2\) to \(\tfrac13\). What happens, and why does this matrix have no inverse?
E7. The steps are \(\text{R2} \leftarrow \text{R2} - \tfrac13\text{R1}\), \(\text{R2} \leftarrow \text{R2} \div \tfrac53\), \(\text{R1} \leftarrow \text{R1} - \text{R2}\) and \(\text{R1} \leftarrow \text{R1} \div 3\), giving \(A^{-1} = \begin{pmatrix} \tfrac25 & -\tfrac15 \\ -\tfrac15 & \tfrac35 \end{pmatrix}\) as in the solution. With \(\tfrac13\) in the corner, the second row \((1, \tfrac13)\) is \(\tfrac13\) times the first row \((3, 1)\), so \(\text{R2} - \tfrac13\text{R1}\) leaves a row of zeros on the left. A zero row can never become a row of \(I\), so there is no inverse.
Hints
Each equation is one row. The numbers multiplying the unknowns (\(x\), \(y\), …) go into \(A\) in order; the right-hand number goes into \(\mathbf{b}\). For Problem 1, the two equations are \(3x + 1y = 11\) and \(1x + 2y = 7\).
From the second equation of Problem 1, \(x = 7 - 2y\). Substitute that into the first equation to get a single equation in \(y\) alone, solve it, then back-substitute to get \(x\).
To kill the entry in row \(i\), column \(j\) using a pivot row \(p\) (which has a non-zero entry in column \(j\)), use \[ \text{R}_i \;\leftarrow\; \text{R}_i - \frac{A_{ij}}{A_{pj}}\,\text{R}_p . \] Apply the operation to the whole augmented row, including the \(\mathbf{b}\) entry.
After reaching upper-triangular form, the last row has only one unknown — solve it. Carry that value into the row above, which now also has one remaining unknown, and continue upward.
Work in three stages: (1) clear the entry below the diagonal, exactly as in Problem 2; (2) clear the entry above the diagonal, using row 2 to change row 1; (3) divide each row by its diagonal number so the left side becomes \(I\). Every operation acts on all four numbers in the row. Check with \(A^{-1}A = I\).
Solutions
Problem 1 — substitution
Equations: \(3x + y = 11\) and \(x + 2y = 7\). In standard form, \[ \begin{bmatrix} 3 & 1 \\ 1 & 2 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 11 \\ 7 \end{bmatrix}. \]
From equation 2, \(x = 7 - 2y\). Substitute into equation 1: \[ 3(7 - 2y) + y = 11 \;\Rightarrow\; 21 - 6y + y = 11 \;\Rightarrow\; -5y = -10 \;\Rightarrow\; y = 2. \] Then \(x = 7 - 2(2) = 3\). So \(x = 3\) (Type A), \(y = 2\) (Type B).
Check: \(3(3) + 1(2) = 11\ \checkmark\) and \(1(3) + 2(2) = 7\ \checkmark\).
Problem 2 — Gaussian elimination
\[ \left[\begin{array}{cc|c} 3 & 1 & 11 \\ 1 & 2 & 7 \end{array}\right] \xrightarrow{\;\text{R2}-\frac13\text{R1}\;} \left[\begin{array}{cc|c} 3 & 1 & 11 \\ 0 & \tfrac{5}{3} & \tfrac{10}{3} \end{array}\right] \] Bottom row: \(\tfrac{5}{3}y = \tfrac{10}{3} \Rightarrow y = 2\). Back-substitute into R1: \(3x + 2 = 11 \Rightarrow x = 3\). Same answer.
Problem 3 — 3×3 Gaussian elimination
Augmented matrix: \[ \left[\begin{array}{ccc|c} 1 & 2 & 1 & 8\\ 2 & 1 & 1 & 7\\ 1 & 1 & 3 & 10 \end{array}\right] \]
Clear column 1 with \(\text{R2}-2\text{R1}\) and \(\text{R3}-\text{R1}\): \[ \left[\begin{array}{ccc|c} 1 & 2 & 1 & 8\\ 0 & -3 & -1 & -9\\ 0 & -1 & 2 & 2 \end{array}\right] \] Clear column 2 with \(\text{R3} - \tfrac{1}{3}\text{R2}\) (since \(-1 = \tfrac{1}{3}\cdot(-3)\)): \[ \left[\begin{array}{ccc|c} 1 & 2 & 1 & 8\\ 0 & -3 & -1 & -9\\ 0 & 0 & \tfrac{7}{3} & 5 \end{array}\right] \] (Row 3: \(2 - \tfrac13(-1) = \tfrac{7}{3}\) and \(2 - \tfrac13(-9) = 5\).)
Back-substitute: \[ \begin{aligned} \tfrac{7}{3}z &= 5 &&\Rightarrow\; z = \tfrac{15}{7}\approx 2.143\\ -3y - z &= -9 &&\Rightarrow\; -3y = -9 + \tfrac{15}{7} = -\tfrac{48}{7} \;\Rightarrow\; y = \tfrac{16}{7}\approx 2.286\\ x + 2y + z &= 8 &&\Rightarrow\; x = 8 - \tfrac{32}{7} - \tfrac{15}{7} = 8 - \tfrac{47}{7} = \tfrac{9}{7}\approx 1.286 \end{aligned} \] So \(x = \tfrac{9}{7},\ y = \tfrac{16}{7},\ z = \tfrac{15}{7}\).
Check equation 1: \(\tfrac{9}{7} + 2\cdot\tfrac{16}{7} + \tfrac{15}{7} = \tfrac{9 + 32 + 15}{7} = \tfrac{56}{7} = 8\ \checkmark\). (Equations 2 and 3 check out the same way.)
Problem 4 — the inverse
Clear below the diagonal (as in Problem 2), make the second pivot 1, clear above it, then scale row 1: \[ \left[\begin{array}{cc|cc} 3 & 1 & 1 & 0 \\ 1 & 2 & 0 & 1 \end{array}\right] \xrightarrow{\;\text{R2}-\frac13\text{R1}\;} \left[\begin{array}{cc|cc} 3 & 1 & 1 & 0 \\ 0 & \tfrac53 & -\tfrac13 & 1 \end{array}\right] \xrightarrow{\;\text{R2}\,\times\,\frac35\;} \left[\begin{array}{cc|cc} 3 & 1 & 1 & 0 \\ 0 & 1 & -\tfrac15 & \tfrac35 \end{array}\right] \] \[ \xrightarrow{\;\text{R1}-\text{R2}\;} \left[\begin{array}{cc|cc} 3 & 0 & \tfrac65 & -\tfrac35 \\ 0 & 1 & -\tfrac15 & \tfrac35 \end{array}\right] \xrightarrow{\;\text{R1}\,\div\,3\;} \left[\begin{array}{cc|cc} 1 & 0 & \tfrac25 & -\tfrac15 \\ 0 & 1 & -\tfrac15 & \tfrac35 \end{array}\right] \] So \(A^{-1} = \begin{pmatrix} 0.4 & -0.2 \\ -0.2 & 0.6 \end{pmatrix}\). Check: \(A^{-1}A = \begin{pmatrix} 0.4\cdot3 - 0.2\cdot1 & 0.4\cdot1 - 0.2\cdot2 \\ -0.2\cdot3 + 0.6\cdot1 & -0.2\cdot1 + 0.6\cdot2 \end{pmatrix} = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\). \(\checkmark\)
\(A^{-1}\mathbf{b} = \begin{pmatrix} 0.4\cdot 11 - 0.2\cdot 7 \\ -0.2\cdot 11 + 0.6\cdot 7 \end{pmatrix} = \begin{pmatrix} 3 \\ 2 \end{pmatrix}\), as in Problem 1.
\(A^{-1}\begin{pmatrix} 8 \\ 6 \end{pmatrix} = \begin{pmatrix} 3.2 - 1.2 \\ -1.6 + 3.6 \end{pmatrix} = \begin{pmatrix} 2 \\ 2 \end{pmatrix}\): 2 units of Type A and 2 of Type B. Check: G1 \(= 3\cdot2 + 1\cdot2 = 8\) and G2 \(= 1\cdot2 + 2\cdot2 = 6\). \(\checkmark\) The inverse was computed once and reused; with many samples that saves redoing the elimination each time.
Problem 5 — the determinant
\(\det A = 3\cdot2 - 1\cdot1 = 5\), so \(A^{-1} = \frac15\begin{pmatrix} 2 & -1 \\ -1 & 3 \end{pmatrix} = \begin{pmatrix} 0.4 & -0.2 \\ -0.2 & 0.6 \end{pmatrix}\), the same as in Problem 4.
\(\det B = 2\cdot2 - 4\cdot1 = 0\). The second column \((4, 2)\) is twice the first \((2, 1)\): the columns are parallel, so the two cell types cannot be told apart. \(B\) has no inverse, and \(B\mathbf{x} = \mathbf{b}\) has either no solution or infinitely many, never exactly one.
Where this leads
Every system in the by-hand problems had exactly one solution. As the Explore section shows, that is not guaranteed: Linear systems in R builds these systems and asks what happens when \(A\) is singular or non-square — the cases of no solution and infinitely many solutions, which correspond to a deconvolution problem being over- or under-determined.