03 In-Class Assignment: Solving Linear Systems of equations
Contents
03 In-Class Assignment: Solving Linear Systems of equations#
1. Pre-class assignment review#
We will discuss
Row form of a system of linear equations and its graphical interpretation in \(\mathbb R^2\) and \(\mathbb R^3\)
Column form of a system of linear equations and its graphical interpretation
Matrix notation
Matrix form of a system of linear equations
2. Practice with Row/Column/Matrix Forms#
Consider the following system of equations:
✅ Do This: Rewrite the above system in row form below. We’ve included \(\LaTeX\) code to display something resembling the row form of a system of equations. All you need to do is replace the \(?\)’s with the appropriate numbers.
Double click this cell and edit the equations below:
✅ Do This: Rewrite the above system in column form below. We’ve included \(\LaTeX\) code to display something resembling the column form of a system of equations. All you need to do is replace the \(?\)’s with the appropriate numbers.
Double click this cell and edit the equation below:
✅ Do This: Rewrite the above system in augmented matrix form below. We’ve included \(\LaTeX\) code to display something resembling the augmented matrix form of a system of equations. All you need to do is replace the \(?\)’s with the appropriate numbers.
Double click this cell and edit the expression below:
Now consider the following system of equations
✅ Do This: Rewrite the above system in row form, column form, and augmented matrix form below. If you can, try to copy, paste, and modify the previous \(\LaTeX\) code to write your answers. If you can’t, don’t sweat it. Just type your answers as best as you can. Note that learning to typset mathematical expressions using \(\LaTeX\) is not required for this course, but you may find it useful for other courses or later in life.
Row form:
put your answer here
Column form:
put your answer here
Augmented matrix form:
put your answer here
3. Visualizing the Row and Column Pictures#
Consider the following system of equations $\(\begin{align} -x + 3y &= 1\\ 2x - 4y &= -4 \end{align}\)$
✅ Do This: Using Matplotlib, make a plot showing the lines \(-x+3y = 1\) and \(2x-4y = -4\). Determine (using any method you’d like) the solution \((x,y)\) where the two lines intersect, and plot a dot at this point.
Hint: If you aren’t sure where to start, look at PCA-03 Section 2.
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
# Put your code here
The column picture of the system of equations is
This means that some linear combination of the vectors \(\vec{u} = \begin{bmatrix}-1 \\ 2\end{bmatrix}\) and \(\vec{v} = \begin{bmatrix}3 \\ -4\end{bmatrix}\) is equal to the vector \(\vec{w} = \begin{bmatrix}1 \\ -4\end{bmatrix}\).
✅ Do This: Run the following two cells. This will generate a widget with two sliders to select values for \(x\) and \(y\). The widget will plot:
the vector \(x\vec{u}\) (a scalar multiple of \(\vec{u}\)) in blue with the “tail” at the origin
the vector \(y\vec{v}\) (a scalar multiple of \(\vec{v}\)) in red with the “tail” at the “tip” of the vector \(x\vec{u}\),
the target vector \(\vec{w}\) in purple with the “tail” at the origin
By plotting the vectors \(x\vec{u}\) and \(y\vec{v}\) in the manner above (i.e. “tip to tail”), the sum of the two vectors, i.e. \(x\vec{u}+y\vec{v}\) is the vector from the “tail” of the vector \(x\vec{u}\) to the “tip” of the vector \(y\vec{v}\). So if \(x\vec{u}+y\vec{v} = \vec{w}\), the tip of the red vector will coincide with the tip of the purple vector.’’
Set the values for the sliders for \(x\) and \(y\) such that \(x\vec{u}+y\vec{v} = \vec{w}\), i.e. \(x\begin{bmatrix}-1 \\ 2\end{bmatrix} + y\begin{bmatrix}3 \\ -4\end{bmatrix} = \begin{bmatrix}1 \\ -4\end{bmatrix}\).
import ipywidgets as widgets
from ipywidgets import interact
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np
def plot_vector(start, end, color):
tip_width = 0.4
tip_length = 0.5
# Scaling needed so that the tip of the arrow ends at the vector
vec_length = np.linalg.norm(end - start)
if vec_length != 0:
end_tip = end * (vec_length - tip_length) / vec_length
else:
end_tip = start
tip_width = 0
tip_length = 0
plt.arrow(start[0], start[1], end_tip[0], end_tip[1],
head_width=tip_width, head_length=tip_length, fc=color, ec=color)
def column_picture(u, v, target, step=0.5):
min_x, max_x = -5.0, 5.0
min_y, max_y = -5.0, 5.0
def plot_linear_combination(x, y):
plt.figure(figsize=(9,9))
# Set bounds of plot
plt.axis('equal')
plt.axis([abs(u[0]) * min_x + abs(v[0]) * min_y, abs(u[0]) * max_x + abs(v[0]) * max_y,
abs(u[1]) * min_x +abs(v[1]) * min_y, abs(u[1]) * max_x + abs(v[1]) * max_y])
plot_vector(np.zeros(2), x * u, color='blue')
plot_vector(x * u, y * v, color='red')
plot_vector(np.zeros(2), target, color='purple')
plt.title(f'{x}*u + {y}*v = {x * u + y * v}')
interact(plot_linear_combination,
x=widgets.FloatSlider(value=1.0, min=min_x, max=max_x, step=step),
y=widgets.FloatSlider(value=1.0, min=min_y, max=max_y, step=step))
u = np.array([-1, 2])
v = np.array([3, -4])
target = np.array([1, -4])
column_picture(u, v, target)
✅ Do This: What values did you get for \(x\) and \(y\)? Are these the same as what you got when plotting the system of equations in row form?
Put your answers here
One can check that the vectors \(\vec{u} = \begin{bmatrix}-1 \\ 2\end{bmatrix}\) and \(\vec{v} = \begin{bmatrix}3 \\ -4\end{bmatrix}\) span the plane. This means that for any \(2\)-vector \(\vec{w}\), there exists a choice of scalars \(x\) and \(y\) such that \(x\vec{u}+y\vec{v} = \vec{w}\).
✅ Do This: Run the following cell to generate a similar widget as above, but this time, with \(\vec{w} = \begin{bmatrix}3 \\ -2\end{bmatrix}\). Again, select values for \(x\) and \(y\) so that $\(x\begin{bmatrix}-1 \\ 2\end{bmatrix} + y\begin{bmatrix}3 \\ -4\end{bmatrix} = \begin{bmatrix}3 \\ -2\end{bmatrix} \implies \begin{align}-x + 3y &= 3\\2x - 4y &= -2\end{align}\)$
u = np.array([-1, 2])
v = np.array([3, -4])
target = np.array([3, -2])
column_picture(u, v, target)
✅ Do This: Can the vector \(\begin{bmatrix}3 \\ -2\end{bmatrix}\) be expressed as a linear combination of the vectors \(\begin{bmatrix}-1 \\ 2\end{bmatrix}\) and \(\begin{bmatrix}3 \\ -4\end{bmatrix}\)? If so, what values did you get for coefficients \(x\) and \(y\)?
Put your answer here
✅ Do This: Run the following cell to generate a similar widget as above, but this time, for the system of equations $\(\begin{align}-x + 3y &= 5\\2x - 6y &= 5\end{align}\)\( which in column form is \)\(x\begin{bmatrix}-1 \\ 2\end{bmatrix} + y\begin{bmatrix}3 \\ -6\end{bmatrix} = \begin{bmatrix}5 \\ 5\end{bmatrix}\)$
u = np.array([-1, 2])
v = np.array([3, -6])
target = np.array([5, 5])
column_picture(u, v, target)
✅ Do This: After playing with the above widget, answer the following questions:
Can the vector \(\begin{bmatrix}5 \\ 5\end{bmatrix}\) be written as a linear combination of the vectors \(\begin{bmatrix}-1 \\ 2\end{bmatrix}\) and \(\begin{bmatrix}3 \\ -6\end{bmatrix}\)?
Do the vectors \(\begin{bmatrix}-1 \\ 2\end{bmatrix}\) and \(\begin{bmatrix}3 \\ -6\end{bmatrix}\) span the plane?
Are there values of \(x\) and \(y\) that satisfy both of the equations \(-x + 3y = 5\) and \(2x - 6y = 5\)?
Justify all of your answers.
Write your answers here
4. 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, with some modifications by Dr. Santhosh Karnik, Michigan State University. Credit to Craig Gross, Michigan State University for the code for the widget and column picture examples in Section 3.
This work is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
###STARTFOOTER###
Congratulations, we’re done!#
Be sure to turn in this notebook as an .html file on D2L. If your group finished the in-class assignment early, be sure to show it to an instructor/TA/LA before you leave.