Link to this document's Jupyter Notebook

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

Agenda for today's class (80 minutes)

</p>

  1. (30 minutes) Review pre-class assignment
  2. (30 minutes) Jacobi Method for solving Linear Equations
  3. (20 minutes) Numerical Error

1. Pre-class assignment review

03--Linear_Equations_pre-class-assignment.ipynb


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.

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:

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:

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.

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:

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.

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:


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