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 asynchronously, 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.

In-Class Assignment: Gauss-Jordan

Simple xy graph with a high order polynomial representing a curve. In this assignment we will be covering curve fitting an the figure is just intended as a viaual motivation

In [1]:
#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)

1. Pre-class assignment review


2. Generalize the procedure

We are going to think about Gauss-Jordan as an algorithm. First I want you to think about how you would generalize the procedure to work on any matrix. Do the following before 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: What are the first three mathematical steps you would do to put the above equation into a reduced row echelon form using Gauss-Jordan method?

Put your answer here.

Psudocode

QUESTION: Write down the steps you would complete to implement the Gauss-Jordan elimination algorithm as a computer programer. Some questions to answer:

  1. What are the inputs?
  2. What are the outputs?
  3. How many and what types of loops would you have to guarantee success of your program?

Once you have thought this though the instructor will work with you to build the algorithm.


3. Basic Gauss Jordan

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 m   
        if i ≠ j:
            Ratio = A[j,i]/A[i,i]
            #Elementary Row Operation 3
            for k from 1 to n:
                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 n:
        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:

In [2]:
# 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:

In [3]:
A = [[1, 1, 1, 2], [2, 3, 1, 3], [0, -2, -3, -8]]
answer = basic_gauss_jordan(A)
sym.Matrix(answer)
In [4]:
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.

In [5]:
# Put your code here

Congratulations, we're done!

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.

Course Resources:

Written by Dr. Dirk Colbry, Michigan State University Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.