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 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.
</p>
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:
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);
<matplotlib.legend.Legend at 0x7f43a0d6cdd8>
✅ 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.
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.
✅ 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)
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:
import numpy as np
J = np.array([20])
L = [20]
print(pow(20,8))
print(pow(L[0],8))
print(pow(J[0],8))
25600000000 25600000000 25600000000
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)
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
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.
Written by Dr. Dirk Colbry, Michigan State University
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.