In order to successfully complete this assignment you need to participate both individually and in groups during class. If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending asyncronously, turn in your assignment using D2L no later than 11:59pm on the day of class. See links at the end of this document for access to the class timeline for your section.
#Load Useful Python Libraries
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
sym.init_printing(use_unicode=True)
We are going to think about Gauss-Jordan as an algorithm. First I want you to think about how you would Gneralize the procedure to work on any matrix. Do the following beofre moving on to the next section.
✅ **DO THIS:** Use the following matrix to think about how you would solve any system of equations using the Gauss-Jordan elimination algorithm. Focus on the steps.
$$ \left[ \begin{matrix} a & b & c \\ e & f & g \\ i & j & k \end{matrix} \, \middle\vert \, \begin{matrix} d \\ h \\ l \end{matrix} \right] $$✅ **QUESTION:** Describe the first three mathematical steps you would do to put the above equation into a reduced row echelon form using Gauss-Jordan method?
✅ **DO THIS:** Write down the steps you would complete to implement the Gauss-Jordan elimination algorithm as a computer programer. Some questions to answer:
Once you have thought this though the instructor will work with you to build the algorithm.
The following is implementation of the Basic Gauss-Jordan Elimination Algorithm for Matrix $A^{m\times n}$ (Pseudocode):
for i from 1 to m:
for j from 1 to n
if i ≠ j:
Ratio = A[j,i]/A[i,i]
#Elementary Row Operation 3
for k from 1 to m:
A[j,k] = A[j,k] - Ratio * A[i,k]
next k
endif
next j
#Elementary Row Operation 2
Const = A[i,i]
for k from 1 to m:
A[i,k] = A[i,k]/Const
next i
✅ **DO THIS**: using the Pseudocode provided above, write a basic_gauss_jordan
function which takes a list of lists $A$ as input and returns the modified list of lists:
Put your answer here.
Lets check your function by applying the basic_gauss_jordan
function and check to see if it matches the answer from matrix $A$ in the pre-class video:
A = [[1, 1, 1, 2], [2, 3, 1, 3], [0, -2, -3, -8]]
answer = basic_gauss_jordan(A)
sym.Matrix(answer)
answer_from_video = [[1, 0, 0, -1], [0, 1, 0, 1], [0, 0, 1, 2]]
np.allclose(answer, answer_from_video)
The above psuedocode does not quite work properly for all matrices. For example, consider the following augmented matrix:
$$ B = \left[ \begin{matrix} 0 & 1 & 33\\ 5 & 3 & 7 \\ 6 & 69 & 4 \end{matrix} \, \middle\vert \, \begin{matrix} 30 \\ 90 \\ 420 \end{matrix} \right] $$✅ **QUESTION** Explain why doesn't the provided basic_gauss_jordan
function work on the matrix $B$?
Put your answer to the above question here.
✅ **QUESTION** Describe how you could modify matrix $B$ so that it would work with basic_gauss_jordan
AND still give the correct solution?
Put your answer to the above question here.
# Put your code here
The Python sympy
library has a "reduced row echelon form" (rref) function which runs a much more efficent version of the Gauss-Jordan function. To use the rref
funciton you must first convert your matrix into a sympy.Matrix
and then run the function. For example, lets do this for the above matrix $B$ which I will show again here:
sym.Matrix(B).rref()
This function outputs two values (a matrix an a tuple). For the purposes of this class we only the matrix. I generally use the following syntac when using rref()
sym.Matrix(B).rref()[0]
✅**QUESTION**: Although we do not use it often in this course, what does the second output of the rref
mean (i.e. what does (0,1,2)
mean? hint: read the documentation for rref
.
Put your answer to the above question here
How lets consider the multi-week example from the pre class assignment, where:
Week 1: $$ c + b = 30 $$ $$ 20c + 25b = 690 $$
Week 2: $$ c + b = 35 $$ $$ 20c + 25b = 750 $$
Week 3: $$ c + b = 30 $$ $$ 20c + 25b = 650 $$
✅**DO THIS**: Write a $2 \times 5$ augmented matrix representing the 6 equations above. (you can just copy and paste this from the pre-class if you got it right there), Name your Matrix $G$ to verify your answer using the checkanswer
function below.
#Put your answer to the above question here.
The following function will apply the rref function to the matrix $G$ and store it in a variable called, wait for it, rref
:
rref,_ = sym.Matrix(G).rref()
rref
✅**QUESTION**: Given the above, How many hours did Giselle work as a capenter for the three weeks and how many hours did she work as a blacksmith. Fill in your answers below to check if you are correct:
#Replace the zeros with your answers
carpenter_week1 = 0
carpenter_week2 = 0
carpenter_week3 = 0
blacksmith_week1 = 0
blacksmith_week2 = 0
blacksmith_week3 = 0
from answercheck import checkanswer
hours = [[carpenter_week1, carpenter_week2, carpenter_week3],
[blacksmith_week1, blacksmith_week2, blacksmith_week3]]
hours = np.matrix(hours).astype('float')
checkanswer.matrix(hours,'b2d4a73cac3c95204f5ed743b507093a');
If you attend class in-person then have one of the instructors check your notebook and sign you out before leaving class. If you are attending remote, turn in your assignment using D2L.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.