# Matrix Algebra for Electrical Engineers
## Introduction - Linear Equations

Let us illustrate through examples what linear equations are. We will also be introducing new notation wherever appropriate.
For example:
    $3x-y=14$
    $2x+y=11$
You could solve for $x$ or $y$ in one equation and substitute it into the other. But what if you had three variables, and three equations, such as:
    $12x+2y+15z=25$
    $23x+12y+45z=46$
    $32x+68y+10z=8$
Substitution is still viable, but it would take a while, and you have many opportunities to make a mistake. In this page, we will talk about turning linear equations into a matrix, and then using properties of matrices to solve them. Note that we're skipping most of the theory involved, and are solely focusing on their practical usage.
Now let's examine those first two problems. The first thing we need to do is turn the systems of equations into matrices. To do that, we make each row correspond to an equation, and each column (vertical) correspond to a single variable. So for the first equation:
    $3x-y=14$
    $2x+y=11$
So we have:
    $\left[{\begin{array}{ccc}3&-1&14\\2&1&11\end{array}}\right]$
Where the first row is the first equation, and the second row is the second. The first column corresponds to $x$, the second to $y$, and the third to $z$.
Let's try doing that to the second one now too:
    $\left[{\begin{array}{cccc}12&2&15&25\\23&12&45&46\\32&68&10&8\end{array}}\right]$

## Matrices

Suppose that you have a linear system of equations
    ${\begin{aligned}a_{11}x_{1}+a_{12}x_{2}+a_{13}x_{3}+a_{14}x_{4}&=b_{1}\\a_{21}x_{1}+a_{22}x_{2}+a_{23}x_{3}+a_{24}x_{4}&=b_{2}\\a_{31}x_{1}+a_{32}x_{2}+a_{33}x_{3}+a_{34}x_{4}&=b_{3}\\a_{41}x_{1}+a_{42}x_{2}+a_{43}x_{3}+a_{44}x_{4}&=b_{4}\end{aligned}}~.$
Matrices provide a simple way of expressing these equations. Thus, we can instead write
    ${\begin{bmatrix}a_{11}&a_{12}&a_{13}&a_{14}\\a_{21}&a_{22}&a_{23}&a_{24}\\a_{31}&a_{32}&a_{33}&a_{34}\\a_{41}&a_{42}&a_{43}&a_{44}\end{bmatrix}}{\begin{bmatrix}x_{1}\\x_{2}\\x_{3}\\x_{4}\end{bmatrix}}={\begin{bmatrix}b_{1}\\b_{2}\\b_{3}\\b_{4}\end{bmatrix}}~.$
Here $\mathbf {A}$ is a $4\times 4$ matrix while $\mathbf {x}$ and $\mathbf {b}$ are $4\times 1$ matrices. In general, an $m\times n$ matrix $\mathbf {A}$ is a set of numbers arranged in $m$ rows and $n$ columns.
    $\mathbf {A} ={\begin{bmatrix}a_{11}&a_{12}&a_{13}&\dots &a_{1n}\\a_{21}&a_{22}&a_{23}&\dots &a_{2n}\\\vdots &\vdots &\vdots &\ddots &\vdots \\a_{m1}&a_{m2}&a_{m3}&\dots &a_{mn}\end{bmatrix}}~.$

## Determinant of a matrix

The next thing we will discuss is the meaning of the determinant of a matrix. Note that a determinant is only defined for square matrices.
For a $2\times 2$ matrix $\mathbf {A}$, we have
    $\mathbf {A} ={\begin{bmatrix}a_{11}&a_{12}\\a_{21}&a_{22}\end{bmatrix}}\implies \det(\mathbf {A} )={\begin{vmatrix}a_{11}&a_{12}\\a_{21}&a_{22}\end{vmatrix}}=a_{11}a_{22}-a_{12}a_{21}~.$
For a $n\times n$ matrix, the determinant is calculated by expanding into minors as
    ${\begin{aligned}&\det(\mathbf {A} )={\begin{vmatrix}a_{11}&a_{12}&a_{13}&\dots &a_{1n}\\a_{21}&a_{22}&a_{23}&\dots &a_{2n}\\a_{31}&a_{32}&a_{33}&\dots &a_{3n}\\\vdots &\vdots &\vdots &\ddots &\vdots \\a_{n1}&a_{n2}&a_{n3}&\dots &a_{nn}\end{vmatrix}}\\&=a_{11}{\begin{vmatrix}a_{22}&a_{23}&\dots &a_{2n}\\a_{32}&a_{33}&\dots &a_{3n}\\\vdots &\vdots &\ddots &\vdots \\a_{n2}&a_{n3}&\dots &a_{nn}\end{vmatrix}}-a_{12}{\begin{vmatrix}a_{21}&a_{23}&\dots &a_{2n}\\a_{31}&a_{33}&\dots &a_{3n}\\\vdots &\vdots &\ddots &\vdots \\a_{n1}&a_{n3}&\dots &a_{nn}\end{vmatrix}}+\dots \pm a_{1n}{\begin{vmatrix}a_{21}&a_{23}&\dots &a_{2(n-1)}\\a_{31}&a_{33}&\dots &a_{3(n-1)}\\\vdots &\vdots &\ddots &\vdots \\a_{n1}&a_{n3}&\dots &a_{n(n-1)}\end{vmatrix}}\end{aligned}}$
In short, the determinant of a matrix $\mathbf {A}$ has the value
    ${\det(\mathbf {A} )=\sum _{i=1}^{n}(-1)^{i+j}a_{ij}M_{ij}}$
where $M_{ij}$ is the determinant of the submatrix of $\mathbf {A}$ formed by eliminating row $i$ and column $j$ from $\mathbf {A}$.

## Cramers Rule

Now we're going to unify our (basic) knowledge of turning a set of linear equations into a matrix, and calculating their determinant, as a method for solving linear equations.
Cramer's rule is an elegant formula for the solutions of a system of linear equations. A typical linear system (also known as a set of "simultaneous linear equations") is a set of N linear equations in N variables (or "unknowns".)
For N=3, it might look like this:
       $Ax+By+Cz=P\,$
       $Dx+Ey+Fz=Q\,$
       $Gx+Hy+Iz=R\,$
The numbers A...I (the "coefficient matrix") are given, as are the numbers P, Q, R (the "right-hand-sides".) The values of x, y, z (the "unknowns") are to be found. A system of N linear equations in N unknowns has a uniquely determined solution except in unusual circumstances (the coefficient matrix having determinant zero, as will be shown below.)
If the number of equations were greater than the number of unknowns, there would be no solutions except in unusual circumstances. If the number of equations were less than the number of unknowns, there would be infinitely many solutions. Neither of those cases is covered by Cramer's rule.
Cramer's rule gives the solution in terms of the determinants of the coefficient matrix and the coefficient matrix with individual columns replaced. It says that the value of the nth unknown is the quotient of the determinant of the coefficient matrix with its nth column replaced by the right-hand-side numbers, divided by the determinant of the unmodified coefficient matrix.
For the 3-equation example given above:
       $x={\frac {\begin{vmatrix}\color {Red}P&B&C\\\color {Red}Q&E&F\\\color {Red}R&H&I\end{vmatrix}}{\begin{vmatrix}A&B&C\\D&E&F\\G&H&I\end{vmatrix}}}\ \ \ \ y={\frac {\begin{vmatrix}A&\color {Red}P&C\\D&\color {Red}Q&F\\G&\color {Red}R&I\end{vmatrix}}{\begin{vmatrix}A&B&C\\D&E&F\\G&H&I\end{vmatrix}}}\ \ \ \ z={\frac {\begin{vmatrix}A&B&\color {Red}P\\D&E&\color {Red}Q\\G&H&\color {Red}R\end{vmatrix}}{\begin{vmatrix}A&B&C\\D&E&F\\G&H&I\end{vmatrix}}}$
This works for any number of equations.

## Basic examples

Now, let's try that with our previous two examples:
    Let's do the simpler one first:
    $\left[{\begin{array}{ccc}3&-1&14\\2&1&11\end{array}}\right]$
    The Coefficient matrix is given as:
    $\left[{\begin{array}{ccc}3&-1\\2&1\end{array}}\right]$
As per Cramer's Rule, the variable of the nth column can be solved by taking the determinant of the matrix with the nth column replaced with the solution, and dividing it by the coefficient matrix. Let's do that. Lets first write out the matrix for calculating the first columns variable, $x$. Substituting the solutions (14 and 11) into the first column gives us the matrix:
    $\left[{\begin{array}{ccc}14&-1\\11&1\end{array}}\right]$
Now, we need to divide the matrix with the substitution by the original matrix:
    ${\frac {\left[{\begin{array}{cc}14&-1\\11&1\end{array}}\right]}{\left[{\begin{array}{cc}3&-1\\2&1\end{array}}\right]}}={\frac {14*1-(-1*11)}{3*1-(-1*2)}}={\frac {25}{5}}=5$
    From this we can see that the value of the variable in the first column, $x$ is equal to 5.
Now let's do the same for the second column. Replacing the -1 and 1 with the 14 and 11, and dividing the determinants:
    ${\frac {\left[{\begin{array}{cc}3&14\\2&11\end{array}}\right]}{\left[{\begin{array}{cc}3&-1\\2&1\end{array}}\right]}}={\frac {3*11-(14*2)}{3*1-(-1*2)}}={\frac {5}{5}}=1$
    From this we can see that the value of the variable in the second column, $y$ is equal to 1.