xaktly | Matrix operations

Cramer's rule


An interesting way of using matrices to solve linear systems


Cramer's rule is used to solve systems of equations for which we have $n$ linearly-independent equations and $n$ unknowns. We'll show the rule here, use it in a couple of examples, then show how the rule is derived so you'll know where it comes from.

The general idea of Cramer's rule is that if we have an $n \times n$ system represented by the matrix-vector equation

$$A \vec x = \vec b$$

– remember that $det(A) \ne 0$ is required for the system to have a solution – The components $(x_i)$ of vector $\vec x$ are

$$x_i = \frac{\text{det}(A_i)}{\text{det}(A)}, \phantom{00} i = 1, \dots , n$$

where the matrix $A_i$ is formed by replacing the $i^{th}$ column of $A$ by the column vector $\vec b$.

Example 1

The simplest example is a $2 \times 2$ system, so let's solve one of those. Solve for the intersection of the lines

$$ \begin{align} 2x - y &= 3 \\[5pt] -3x + 2y &= -5 \end{align}$$


Solution: The matrix-vector representation of this system is

$$\begin{bmatrix} 2 & -1 \\ -3 & 2 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 3 \\ -5 \end{bmatrix}$$

Using Cramer's rule, the x-component of our solution is

$$x = \frac{\text{det}(A_1)}{\text{det}(A)} = \frac{\text{det} \begin{bmatrix} \color{red}{3} & -1 \\ \color{red}{-5} & 2 \end{bmatrix}}{\text{det}\begin{bmatrix} 2 & -1 \\ -3 & 2 \end{bmatrix}}$$

The red is the $\vec b$ vector substituted for the first column of matrix $A$. Then we can calculate $x$.

$$x = \frac{3(2)-(-5)(-1)}{2(2) - (-3)(-1)} = \frac{6-5}{4-3} = 1$$

Likewise, the y-component of the solution is

$$y = \frac{\text{det}(A_2)}{\text{det}(A)} = \frac{\text{det} \begin{bmatrix} 2 & \color{red}{3} \\ -3 & \color{red}{-5} \end{bmatrix}}{\text{det}\begin{bmatrix} 2 & -1 \\ -3 & 2 \end{bmatrix}}$$

$$y = \frac{2(-5)-3(-3)}{2(2) - (-3(-1))} = \frac{-10+9}{4-3} = -1$$

Now let's check that the solution $(x, y) = (1, -1)$ is valid:

$$ \begin{align} 2(1) - (-1) &= 2 + 1 = 3 \phantom{000} \checkmark \\[5pt] -3(1) + 2(-1) &= -3 - 2 = -5 \phantom{000} \checkmark \end{align}$$

That's it!


Example 2

Use Cramer's rule to solve this $3 \times 3$ system:

$$ \begin{align} x_1 - 2x_2 - x_3 &= -4 \\[5pt] -x_1 + x_2 + 4x_3 &= -1 \\[5pt] 2x_1 + 2x_2 - x_3 &= 6 \end{align}$$


Solution: The matrix-vector representation of this system is

$$\begin{bmatrix} 1 & -2 & -1 \\ -1 & 1 & 4 \\ 2 & 2 & -1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} -4 \\ -1 \\ 6 \end{bmatrix}$$

Using Cramer's rule, the x1-component of our solution is

$$x_1 = \frac{\text{det}(A_1)}{\text{det}(A)} = \frac{\text{det} \begin{bmatrix} \color{red}{-4} & -2 & -1 \\ \color{red}{-1} & 1 & 4 \\ \color{red}{6} & 2 & -1 \end{bmatrix}}{\text{det}\begin{bmatrix} 1 & -2 & -1 \\ -1 & 1 & 4 \\ 2 & 2 & -1 \end{bmatrix}}$$

The red is the $\vec b$ vector substituted for the first column of matrix $A$. Then we can calculate $x_1$.

$$ \begin{align} x_1 &= \frac{-4(1)(-1) -2(4)(6) + (-1)(-1)(2) - \\ 6(1)(-1) - 2(4)(-4) -(-1)(-1)(-2)}{1(1)(-1) -2(4)(2) + (-1)(-1)(2) - \\ 2(1)(-1) - 2(4)(1) - (-1)(-1)(-2)} \\[5pt] &= \frac{4-48+2+6+32+2}{-1-16+2+2-8+2} \\[5pt] &= \frac{-2}{-19} = \frac{2}{19} \end{align}$$

The $x_2$ component is

$$x_2 = \frac{\text{det}(A_2)}{\text{det}(A)} = \frac{\text{det} \begin{bmatrix} 1 & \color{red}{-4} & -1 \\ -1 & \color{red}{-1} & 4 \\ 2 & \color{red}{6} & -1 \end{bmatrix}}{\text{det}\begin{bmatrix} 1 & -2 & -1 \\ -1 & 1 & 4 \\ 2 & 2 & -1 \end{bmatrix}}$$

The red is the $\vec b$ vector substituted for the second column of matrix $A$. Then we can calculate $x_2$.

$$ \begin{align} x_2 &= \frac{1(-1)(-1) -4(4)(2) + (-1)(-1)(6) - \\ 2(-1)(-1) - 6(4)(1) -(-1)(-1)(-4)}{1(1)(-1) -2(4)(2) + (-1)(-1)(2) - \\ 2(1)(-1) - 2(4)(1) - (-1)(-1)(-2)} \\[5pt] &= \frac{1-32+6-2-24+4}{-1-16+2+2-8+2} \\[5pt] &= \frac{-47}{-19} = \frac{47}{19} \end{align}$$

Finally, the $x_3$ component is

$$x_3 = \frac{\text{det}(A_3)}{\text{det}(A)} = \frac{\text{det} \begin{bmatrix} 1 & -2 & \color{red}{-4} \\ -1 & 1 & \color{red}{-1} \\ 2 & 2 & \color{red}{6} \end{bmatrix}}{\text{det}\begin{bmatrix} 1 & -2 & -1 \\ -1 & 1 & 4 \\ 2 & 2 & -1 \end{bmatrix}}$$

The red is the $\vec b$ vector substituted for the second column of matrix $A$. Then we can calculate $x_3$.

$$ \begin{align} x_3 &= \frac{1(1)(6) + (-2)(-1)(2) + (-4)(-1)(2) - \\ 2(1)(-4) - 2(-1)(1) - 6(-1)(-2)}{1(1)(-1) -2(4)(2) + (-1)(-1)(2) - \\ 2(1)(-1) - 2(4)(1) - (-1)(-1)(-2)} \\[5pt] &= \frac{6+4+8+8+2-12}{-1-16+2+2-8+2} \\[5pt] &= \frac{16}{-19} = -\frac{16}{19} \end{align}$$

So our solution is

$$(x_1, x_2, x_3) = \left( \frac{2}{19}, \frac{47}{19}, \frac{-16}{19} \right)$$

... but we should check our solution:


$$ \begin{align} \frac{4}{19} - 2 \left( \frac{47}{19} \right) - \frac{-16}{19} &= -4 \\[5pt] 2 - 94 + 16 &= -4(19) \\[5pt] -76 &= -76 \phantom{000} \checkmark \end{align}$$


$$ \begin{align} -\frac{4}{19} + \frac{47}{19} + 4 \left( \frac{-16}{19} \right) &= -1 \\[5pt] -2 + 47 -64 &= -1(19) \\[5pt] -19 = -19 \phantom{000} \checkmark \end{align}$$


$$ \begin{align} 2 \left( \frac{4}{19} \right) - 2 \left( \frac{47}{19} \right) - \frac{-16}{19} &= 6 \\[5pt] 4 + 94 + 16 &= 6(19) \\[5pt] 114 &= 114 \phantom{000} \checkmark \end{align}$$


Yep. That's it!


A derivation (sort of)


A full derivation or proof of Cramer's rule is a little beyond the scope of what we can do here, but we can make a hand-waving demonstration of how Cramer's rule is connected to a $2 \times 2$ system. Take the system

$$ \begin{align} ax + by &= u \\[5pt] cx + dy &= v \end{align}$$

Now let's solve that system by multiplying the top equation by $c$ and the bottom equation by $a$ to get

$$ \begin{align} cax + cby &= cu \\[5pt] acx + ady &= av \end{align}$$

Now let's subtract the first equation from the second:

$$\require{cancel} \cancel{acx} - \cancel{acx} + ady - bcy = av - cu$$

Now solve for y in two steps:

$$ \begin{align} y(ad - bc) &= av - cu \\[5pt] y &= \frac{av - cu}{ad - bc} \end{align}$$

Now this is just the Cramer's rule equation for $y$,

$$y = \frac{\text{det}\begin{bmatrix} a & \color{red}{u} \\ c & \color{red}{v} \end{bmatrix}}{\text{det}\begin{bmatrix} a & b \\ c & d \end{bmatrix}},$$

where the red column was substituted for the second column of the coefficient matrix, just as Cramer's rule prescribes. You can convince yourself that solvind for $x$ yields the correct Cramer's rule expression in that case.

Practice problems

Solve for the intersection of these pairs of lines using Cramer's rule.


$$1. \phantom{00000} \begin{align} 7x - y &= 15 \\[5pt] x + 4y &= 10 \end{align}$$

Solution

In matrix form, this system is

$$\begin{bmatrix} 7 & -1 \\ 1 & 4 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 15 \\ 10 \end{bmatrix}$$

$$ \begin{align} x &= \frac{\text{det} \begin{bmatrix} 15 & -1 \\ 10 & 4 \end{bmatrix}}{\text{det} \begin{bmatrix} 7 & -1 \\ 1 & 4 \end{bmatrix}} \\[5pt] &= \frac{60 - (-10)}{28 - (-1)} \\[5pt] &= \frac{70}{29} \end{align}$$

$$ \begin{align} y &= \frac{\text{det} \begin{bmatrix} 7 & 15 \\ 1 & 10 \end{bmatrix}}{29} \\[5pt] &= \frac{70-15}{29} \\[5pt] &= \frac{55}{29} \end{align}$$

$$(x, y) = \left( \frac{70}{29}, \frac{55}{29} \right)$$


$$2. \phantom{00000} \begin{align} 3x - 3y &= 11 \\[5pt] 2x - 3y &= 4 \end{align}$$

Solution

In matrix form, this system is

$$\begin{bmatrix} 3 & -3 \\ 2 & -3 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 11 \\ 4 \end{bmatrix}$$

$$ \begin{align} x &= \frac{\text{det} \begin{bmatrix} 11 & -3 \\ 4 & -3 \end{bmatrix}}{\text{det} \begin{bmatrix} 3 & -3 \\ 2 & -3 \end{bmatrix}} \\[5pt] &= \frac{-33+12}{-9+6} \\[5pt] &= \frac{-21}{-3} = 7 \end{align}$$

$$ \begin{align} y &= \frac{\text{det} \begin{bmatrix} 3 & 11 \\ 2 & 4 \end{bmatrix}}{-3} \\[5pt] &= \frac{12-22}{-3} \\[5pt] &= \frac{-10}{-3} = \frac{10}{3} \end{align}$$

$$(x, y) = \left( 7, \frac{10}{3} \right)$$

$$3. \phantom{00000} \begin{align} 6x + 7y &= 13 \\[5pt] -x + 4y &= -2 \end{align}$$

Solution

In matrix form, this system is

$$\begin{bmatrix} 6 & 7 \\ -1 & 4 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} 13 \\ -2 \end{bmatrix}$$

$$ \begin{align} x &= \frac{\text{det} \begin{bmatrix} 13 & 7 \\ -2 & 4 \end{bmatrix}}{\text{det} \begin{bmatrix} 6 & 7 \\ -1 & 4 \end{bmatrix}} \\[5pt] &= \frac{52+14}{24+7} \\[5pt] &= \frac{66}{31} \end{align}$$

$$ \begin{align} y &= \frac{\text{det} \begin{bmatrix} 6 & 13 \\ -1 & -2 \end{bmatrix}}{31} \\[5pt] &= \frac{-12+13}{31} \\[5pt] &= \frac{1}{31} \end{align}$$

$$(x, y) = \left( \frac{66}{31}, \frac{1}{31} \right)$$


$$4. \phantom{00000} \begin{align} 4x - 5y &= -5 \\[5pt] 5x + 2y &= 17 \end{align}$$

Solution

In matrix form, this system is

$$\begin{bmatrix} 4 & -5 \\ 5 & 2 \end{bmatrix} \begin{bmatrix} x \\ y \end{bmatrix} = \begin{bmatrix} -5 \\ 17 \end{bmatrix}$$

$$ \begin{align} x &= \frac{\text{det} \begin{bmatrix} -5 & -5 \\ 17 & 2 \end{bmatrix}}{\text{det} \begin{bmatrix} 4 & -5 \\ 5 & 2 \end{bmatrix}} \\[5pt] &= \frac{-10+85}{8+25} \\[5pt] &= \frac{75}{33} \end{align}$$

$$ \begin{align} y &= \frac{\text{det} \begin{bmatrix} 4 & -5 \\ 5 & 17 \end{bmatrix}}{33} \\[5pt] &= \frac{68+25}{33} \\[5pt] &= \frac{93}{33} \end{align}$$

$$(x, y) = \left( \frac{75}{33}, \frac{93}{33} \right)$$



Creative Commons License   optimized for firefox
xaktly.com by Dr. Jeff Cruzan is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. © 2016, Jeff Cruzan. All text and images on this website not specifically attributed to another source were created by me and I reserve all rights as to their use. Any opinions expressed on this website are entirely mine, and do not necessarily reflect the views of any of my employers. Please feel free to send any questions or comments to jeff.cruzan@verizon.net.