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.

In-Class Assignment: Solving Linear Systems of equations

Picture of groundhog day movie poster.

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.


1. Pre-class assignment review

03--Linear_Equations-pre-class-assignment


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);

**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 calculation and when answering the questions below:

0.1 + 0.2 == 0.3
False

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

Put your answer to the above question here.

**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)
False

**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');
Testing 0.3
Answer seems to be correct

checkanswer.float(0.1+0.2,'e85b79abfd76b7c13b1334d8d8c194a5')
Testing 0.3
Answer seems to be correct


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.