Exploring Gaussian Elimination

Introduction

One of the main applications of matrices is solving simultaneous equations of the form:
Here are the variables (unknowns) and the coefficients and are given real numbers. This set of equations can be written using matrix multiplication as:
where is a column vector of unknowns, is a given matrix (column vector) and is the matrix of coefficients. An efficient way of solving a system is by Gaussian Elimination which involves the use of the augmented matrix .
In this activity we will explore the basic commands used to reduce an augmented matrix to row echelon form and solve a system equations.

Before starting

Use the MATLAB Live Editor to edit and run this Live Script in your browser or your desktop.
  1. Read each section carefully.
  2. Run the code within a section before starting to read the next.
  3. To run the code from each section, click into the code section and then click on the Run Section button (from the toolstrip) or click on the blue stripe on left side of that section as shown below:
runsect.png
Remark: Run the code of each section from top to bottom, otherwise you may get an error.
- The end of a section is indicated with a thin line, like the next one -

1. How to solve a system of equations

Matrices are entered in MATLAB by typing the elements within square brackets [], much like we did with vectors. These can be written using commas or spaces to separate the elements in a row and using a semicolon to separate rows. For example, the system
(1)
can be described using the matrices:
So, for instance, in order to define A, we need to use the following code:
A = [1,2,-3;-3,-4,8;2,5,-8]

1.1 Example 1 - Finding the row reduced matrix with rref()

The row reduced form of a matrix can tell us a lot about the system, particularly how many solutions the system has. We can use the command rref to find this reduced form very quickly - without all the hassle of many lines of algebra using elementary row operations!
Using the system defined in (1), we can use the rref command with matrix A as below:
rref(A)
This function gives the Gauss-Jordan reduction for this matrix. What does the reduced row echelon form above tell us about the system? How many solutions does this system have?
Using the augmented matrix instead, we can see solutions for , and right away:
b = [-1;3;-5]; % the right hand side of the system above
Ag = [A,b]; % the augmented matrix for the system
rref(Ag)
What are solutions for , and ?
Try to find the reduced row echelon form by hand yourself and compare your results to those above. You can also compare your findings to the example on page 130 of the workbook.

2. Creating and solving a system of equations

To create and use a system of equations, we can use the commands syms and equationsToMatrix. These commands allow us to enter the equations exactly as they might appear in a problem, for example:
The following examples demonstrate how to turn this system into a matrix of coefficients and vector that represents the right-hand side.

2.1 Example 2 - Defining equations

Let us use the system of equations above:
syms x1 x2 x3
eqn1 = x1 + 2*x2 - 3*x3 == -1
eqn2 = -3*x1 - 4*x2 + 8*x3 == 3
eqn3 = 2*x1 + 5*x2 - 8*x3 == -5
Note 1: The use of symbolic variables allows us to setup equations with a left- and right-hand side. We need the "==" symbol to assign the right-hand side of an equation, while we use the single "=" to assign the entire equation to a variable in MATLAB (eqn1, eqn2 or eqn3 in this case).

2.2 Example 3 - Building matrices

We can equationsToMatrix to convert the equations into the form . The first input defines the equations (as above) and the second defines the variables we are using in this problem.
[A,b] = equationsToMatrix([eqn1, eqn2, eqn3], [x1, x2, x3])

2.3 Example 4 - Solving the system using linsolve()

Now that we have A and we can solve the system using the command linsolve.
In the following example we will use this command to get a particular solution to our system:
x = linsolve(A,b)
See how your results from example 1 compare here!

2.4 Example 5 - Checking for more solutions using null()

The nullspace (denoted ) of a matrix can give us lots of useful information about a system and it is found by solving: . The nullspace is related to the solutions of the system through, where is a particular solution.
The solution to the system above is, in fact, the only solution. We can check this using the command null, which gives us elements in the nullspace of the matrix A.
For example, run the following code:
NSA = null(A)
Here we have no elements in our nullspace. This tells us that we have only one solution to the system above - the particular solution.
Note 2: Observe that the command null produces a result that is empty in MATLAB for this example. However, the nullspace is never empty - since it always contains at least the 0 vector.
Run the following code instead and check the nullspace of this new matrix:
C = [1,-2,4;2,-3,9;5,-9,21];
NSC = null(C)
What do you notice about the elements in the nullspace? Are they "nice looking" numbers?
NSCr = null(C,'r') % the optional input r gives us rational answers
The optional input parameter 'r' gives us nicer answers. Remember that any scalar multiple of a nullspace element can be used in the solution to a system of equations.

3. Hands on Practice

Let's practice what we just learned.

3.1 Solving systems of equations

Activity 1:

At the start of the workshop, you solved the following system of linear equations by hand:
Use MATLAB and rref to solve this system of equations and use null to verify the number of solutions.
Write your code here:
 
 
 
 
 

Activity 2:

There has been a natural disaster on an island off the coast of Australia and you have been hired by the United Nations to help with the distribution of canned food. The only available rations are the canned goods A, B and C.
To avoid permanent health issues, the survivors must receive 12 mg of nutrient P, 14 mg of nutrient Q and 6 mg of nutrient R. Each can provides nutrients as below:
Resources are in short supply, so you must ensure that you distribute the food as efficiently as possible. Use MATLAB and rref to determine the quantity of cans that every person should be given to avoid permanent health issues.
Write your code here:
%{---put nutrient vector below---}%
 
 
 
%{---put nutrient vectors for each can below---}%
 
 
 
%{---solve the system---}%
 
 

3.2 Solutions and the nullspace

Consider the system:
with

Activity 3:

Add the matrix A to MATLAB in any way you like, then use the command rref to find the row reduced matrix.
Write your code here:
 
 
 
 
Next, determine if there are any free variables using null:
 
Confirm your results by hand and see how many solutions you get!