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

Discuss the difference between @ vs * for matrix multiply. Discuss pre-class assignment. Review Gauss-Jordan elimination.


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.

\[\begin{split} \left[ \begin{matrix} a & b & c \\ e & f & g \\ i & j & k \end{matrix} \, \middle\vert \, \begin{matrix} d \\ h \\ l \end{matrix} \right] \end{split}\]

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.

Pseudocode#

QUESTION: Write down the steps you would complete to implement the Gauss-Jordan elimination algorithm as a computer programmer. 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 an \(m \times n\) Matrix \(A\) (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:

# Put your answer here. 

Let’s 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:

\[\begin{split} 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] \end{split}\]

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

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.


Congratulations, we’re done!#

###STARTFOOTER###