03 In-Class Assignment: Solving Linear Systems of equations

In the movie Groundhog day the main character “Phil” repeats the same day over and over again. This is an iterative process where Phil learns from past mistakes until he has a “perfect” day. The Groundhog movie is a fun analogy of iterative methods for solving linear equations. In this class we will write our own iterative method.

2. Jacobi Method for solving Linear Equations

During class today we will write an iterative method (named after Carl Gustav Jacob Jacobi) to solve the following system of equations:

\[ 6x + 2y - ~z = 4~\]
\[~ x + 5y + ~z = 3~\]
\[ 2x +~ y + 4z = 27\]

Here is a basic outline of the Jacobi method algorithm:

  1. Initialize each of the variables as zero (\(x_0 = 0, y_0 = 0, z_0 = 0\))

  2. Calculate the next iteration using the above equations and the values from the previous iterations. For example here is the formula for calculating \(x_i\) from \(y_{(i-1)}\) and \(z_{(i-1)}\) based on the first equation: $\(x_i = \frac{4 - 2y_{(i-1)} + z_{(i-1)}}{6} \)\( Similarly, we can obtain the update for \)y_i\( and \)z_i$ from the second and third equations, respectively.

  3. Increment the iteration counter \((i = i + 1)\) and repeat Step 2.

  4. Stop when the answer “converges” or a maximum number of iterations has been reached. (ex. \(i\) = 100)

IMPORTANT NOTE:

A sufficient (but not necessary) condition for the method to converge is that the matrix A is strictly or irreducibly diagonally dominant. Strict row diagonal dominance means that for each row, the absolute value of the diagonal term is greater than the sum of absolute values of other terms. - From Wikipedia

In other words, the Jacobi Methid will not work an all problems.

DO THIS: Write out the equations for \(x_i\), \(y_i\), and \(z_i\) based on \(x_{(i-1)}\), \(y_{(i-1)}\), and \(z_{(i-1)}\).

Put your answer to the above question here.

DO THIS: Complete the following code by adding formulas for \(y_i\) and \(z_i\) to solve the above equations using the Jacobi method.

%matplotlib inline
import matplotlib.pylab as plt

x = []
y = []
z = []

#step 1: inicialize to zero
x.append(0)
y.append(0)
z.append(0)

for i in range(1,100):
    xi = (4 - 2*y[i-1]+ z[i-1])/6
#####Start your code here #####
    yi = 0 #Change this line
    zi = 0 #Change this line
#####End of your code here#####        
    #Add latest value to history
    x.append(xi)
    y.append(yi)
    z.append(zi)

#Plot History of values
plt.plot(x, label='x')
plt.plot(y, label='y')
plt.plot(z, label='z')
plt.xlabel('Iteration')
plt.ylabel('Value')
plt.legend(loc=1);
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-e58954ae8d36> in <module>
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pylab as plt
      3 
      4 x = []
      5 y = []

~/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'

QUESTION: What are the final values for \(x\), \(y\), and \(z\)?

\[x = \]
\[y = \]
\[z = \]

DO THIS: Write out each of the above equations and show that your final result is a solution to the system of equations:

# Put your code here

QUESTION: By inspecting the graph, how long did it take for the algorithum to converge to a solution?

Put your answer to the above question here.

QUESTION: How could you rewrite the above program to stop earlier.

Put your answer to the above question here.


3. Numerical Error

Consider the following python statement when answering the questions below:

0.1 + 0.2 == 0.3

QUESTION: Why does Python return False even though the above statement is clearly true?

Put your answer to the above question here.

DO THIS: Let’s consider another example. Run the following code which should return true.

import numpy as np
J = np.array([20])
L = [20]

pow(L[0],8) == pow(J[0],8)

If you have an older version of numpy installed (like 1.18.5) then the results of running the above cell may be false (did anyone get this result?). This is because numpy changed how it handles something called “roundoff error”. here is another cell that may help you see better what is going on:

import numpy as np
J = np.array([20])
L = [20]
print(pow(20,8))
print(pow(L[0],8))
print(pow(J[0],8))

The older version of numpy would return the following:

25600000000
25600000000
-169803776

We could say to always upgrade to the latest stable version (generally a good idea). But some other libraries that depend on numpy may not be up to date so sometimes python will install an older version to maintain compatibility. For example, one really popular program is tensorflow, which often requires an older version of numpy.

QUESTION: If Python is sometimes wrong, why do we use it?

Put your answer to the above question here.

QUESTION: What are ways you can do to watch out for these types of errors?

Put your answer to the above question here.

QUESTION: Modify the following program to return True if the values are within some small number (e) of each other.

def checktrue(a,b,e=0.001):
    return a == b

#Test function
checktrue(0.1+0.2, 0.3)

QUESTION: What is a good value to set for e and why?

Put your answer to the above question here.

QUESTION: The errors seen in this example seem like they would be fairly common in Python. See if you can find a function in Numpy that has the same purpose as checktrue:

Put your answer to the above question here.

The class answercheck program will take into consideration round off error. For example, the checkanswer.float command would consider both of the above correct:

from answercheck import checkanswer

checkanswer.float(0.300,'e85b79abfd76b7c13b1334d8d8c194a5');
checkanswer.float(0.1+0.2,'e85b79abfd76b7c13b1334d8d8c194a5')

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.