# Solving Sets of Linear Equations¶

## Learning Objectives¶

By the end of this section you should be able to:

1. Solve a linear system using the substitution method.
2. Solve a linear system using the row reduction method.
3. Use Python to solve a linear system.

## Introduction¶

Being able to solve sets of linear equations is very important to a chemical engineer. There will be times where you have over 10 unknown variables and need to solve for them. This can be done by hand and can also be solved using a computer, more specifically, a programming language, such as python.

## By Hand¶

### Substitution Method for Solving Linear Systems¶

One of the simplest methods to solve a system of linear equations is the substitution method. The substitution method functions by substituting one of the variables for another. Here is an example:

$y = 2x + 4$
$3x + y = 9$

We can now substitute the y in the first equation into the second equation and solve.

$3x + (2x + 4) = 9$
$5x = 5$
$x = 1$

Finally, we can solve for y using x.

$y = 2(1) + 4$
$y = 6$

### Row Reduction Method for Solving Linear Systems¶

A more complicated but more practical method, for large systems, to solve large matrices is row reduction. Row reduction is done through matrix manipulation. There are four main rules to matrix manipulation:

1. Multiply a row by a non-zero constant.
2. Add one row to another.
3. Interchange between rows.
4. Add a multiple of one row to another.

We will be using these rules to help us solve a system of linear equations.

In this example, we must first convert the system of linear equations into a matrix. Then, we must row reduce the matrix until we get ones along the diagonal of the matrix and a lower triangle of zeros. Here is the system of equations:

$x + y - z = 5$
$2x + y + 3z = 2$
$4x - y + 2z = -1$

We must convert this system into an augmented matrix.

$\begin{split}\begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 2 & 1 & 3 & | & 2 \\ 4 & -1 & 2 & | & -1 \end{vmatrix}\end{split}$

Now we shall row reduce the augmented matrix using the previous rules stated. First, we will $:raw-latex:\mathbf{R}_2 - 2:raw-latex:mathbf{R}_1$

$\begin{split}\begin{equation*} \begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 0 & -1 & 5 & | & -8 \\ 4 & -1 & 2 & | & -1 \end{vmatrix} \end{equation*}\end{split}$

Second, we will $:raw-latex:\mathbf{R}_3 - 4:raw-latex:mathbf{R}_1$

$\begin{split}\begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 0 & -1 & 5 & | & -8 \\ 0 & -5 & 6 & | & -21 \end{vmatrix}\end{split}$

Next, we will $-1 :raw-latex:\times :raw-latex:mathbf{R}_2$

$\begin{split}\begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 0 & 1 & -5 & | & 8 \\ 0 & -5 & 6 & | & -21 \end{vmatrix}\end{split}$

After this, we will $:raw-latex:\mathbf{R}_3 + 5:raw-latex:mathbf{R}_2$

$\begin{split}\begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 0 & 1 & -5 & | & 8 \\ 0 & 0 & -19 & | & 19 \end{vmatrix}\end{split}$

Finally we will $:raw-latex:\frac{\mathbf{R}_3}{-19}$

$\begin{split}\begin{vmatrix} 1 & 1 & -1 & | & 5 \\ 0 & 1 & -5 & | & 8 \\ 0 & 0 & 1 & | & -1 \end{vmatrix}\end{split}$

Since now we know

$z = -1$

we can solve for y which is

\begin{align}\begin{aligned}y = 3\\and finally solve for x which is\end{aligned}\end{align}
$x = 1$

## Using Python¶

As stated before, Python is a very powerful high-level programming language that can be used to compute tedious and complex arithmetic questions. Here is a simple example of how you can use Python to solve a system of linear equations.

Let’s say we have the same system of equations as shown above. Let’s solve it using Python!

$x + y - z = 5$
$2x + y + 3z = 2$
$4x - y + 2z = -1$
In :

import numpy as py
from scipy.linalg import solve

A = [[1, 1, -1], [2, 1, 3], [4, -1, 2]]
b = [, , [-1]]

x = solve(A,b)
x

Out:

array([[ 1.],
[ 3.],
[-1.]])


Here you can see what took multiple steps of matrix manipulation is easily solved in a few lines of code in Python.

Now you try using Python to solve this system of linear equations!

$x + 3y - z = 2$
$2x + 5 + 3z = 1$
$x - 3y + 2z = -4$
In [ ]: