# Solving Sets of Linear Equations¶

## Learning Objectives¶

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

- Solve a linear system using the
**substitution method**. - Solve a linear system using the
**row reduction method**. - 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:

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

Finally, we can solve for y using x.

### 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:

- Multiply a row by a non-zero constant.
- Add one row to another.
- Interchange between rows.
- 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:

We must convert this system into an augmented matrix.

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 $

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

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

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

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

Since now we know

we can solve for y which is

## 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!

```
In [8]:
```

```
import numpy as py
from scipy.linalg import solve
A = [[1, 1, -1], [2, 1, 3], [4, -1, 2]]
b = [[5], [2], [-1]]
x = solve(A,b)
x
```

```
Out[8]:
```

```
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!

```
In [ ]:
```

```
```