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

Today’s Agenda (80 minutes)

  1. (20 minutes) Pre-class assignment review

  2. (20 minutes) Generalize the procedure

  3. (20 minutes) Basic Gauss Jordan

#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)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-edd51b6ed677> in <module>
      1 #Load Useful Python Libraries
----> 2 get_ipython().run_line_magic('matplotlib', 'inline')
      3 import matplotlib.pylab as plt
      4 import numpy as np
      5 import sympy as sym

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2342                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2343             with self.builtin_trap:
-> 2344                 result = fn(*args, **kwargs)
   2345             return result
   2346 

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   3511         """
   3512         from IPython.core import pylabtools as pt
-> 3513         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3514 
   3515         if gui != 'inline':

~/REPOS/MTH314_Textbook/MakeTextbook/envs/lib/python3.9/site-packages/IPython/core/pylabtools.py in find_gui_and_backend(gui, gui_select)
    278     """
    279 
--> 280     import matplotlib
    281 
    282     if gui and gui != 'auto':

ModuleNotFoundError: No module named 'matplotlib'

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.

\[\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.

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:

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

\[\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.