In order to successfully complete this assignment, you must follow all the instructions in this notebook and upload your edited ipynb file (including solutions) to D2L with your answers on or before 11:59 PM ET, Friday, February 03, 2023.

BIG HINT: Read the entire homework before starting.

Homework 1: Solving Systems of Linear Equations#

These problems are intended to give students exposure to the style of some of the questions asked on quizzes and help you practice. Please finish the entire Jupyter Notebook and turn in your edited file using the MSU D2L Website.

You may collaborate with other students in this course. However, you may only share ideas with each other, not code or answers.

Also, note that your section’s TA will run your code cells in order (top to bottom) in order to grade your homework submission. So please make sure your code cells work as you intend when you run them in order.

Outline for Homework 1#

  1. Basics

  2. Working at MSU

  3. Balancing Chemical Equations

  4. Traffic Control

  5. Visualizing the Equations

  6. Numerical Error

For convenience, you can run the following cell to perform all the imports you’ll need for this homework. You can also import packages in other cells if you wish.

%matplotlib inline 
import matplotlib.pylab as plt
import numpy as np
import sympy as sym
#sym.init_printing()
import os.path
from urllib.request import urlretrieve
if not os.path.isfile("answercheck.py"):
    urlretrieve('https://raw.githubusercontent.com/colbrydi/jupytercheck/master/answercheck.py', 'answercheck.py');
from answercheck import checkanswer

1. Basics (27 points)#

QUESTION 1a: (18 points) Suppose \(u, v \in\mathbb{R}^5\) and \(x,y\in\mathbb R^3\) are vectors given by

  • \(u=[0,1,2,-1,2], v=[2,1,3,4, 1]\) and \(x=[0,1,-1], y=[3,2, \pi]\).

For each of the following expressions, state whether or not the expression is well-defined in the setting of linear algebra, and briefly explain your reasoning. If an expression is well defined, also calculate its value.

  1. \(u\cdot v\)

  2. \(u\cdot x\)

  3. \(u\cdot v + x\cdot y\)

  4. \(u + x\cdot y\)

  5. \((u+v)\cdot v\)

  6. \((u \cdot v)x\)

Recall that for any two vectors \(a,b \in \mathbb R^n\) (of the same size) given by \(a=[a_1, \dots, a_n]\) and \(b=[b_1, \dots, b_n]\), their dot product (a scalar) is defined as \(a \cdot b = a_1b_1 + a_2b_2 + \cdots + a_nb_n\).

Enter your answers in this markdown cell by double clicking here:

QUESTION 1b: (3 points) Consider the following augmented matrix \([A|b]\), in Reduced Row Echelon Form (RREF), representing a system of linear equations, \(Ax=b\). Find ALL possible solutions, \(x\), of the system, and justify your answer.

\[\begin{split}[A|b] = \left[ \begin{matrix} 1 & 0 & 0 \\ 0 & 1 & 0 \\ 0 & 0 & 1 \end{matrix} \, \middle\vert \, \begin{matrix} 9 \\ -4 \\ 6 \end{matrix} \right]\end{split}\]

Enter your answer in this markdown cell by double clicking here:

QUESTION 1c: (3 points) Consider the following augmented matrix \([B|c]\), in Reduced Row Echelon Form (RREF), representing a system of linear equations, \(Bx=c\). Find ALL possible solutions, \(x\), of the system, and justify your answer.

\[\begin{split}[B|c] = \left[ \begin{matrix} 1 & 0 & 2 & 1\\ 0 & 1 & 3 & 0\\ 0 & 0 & 0 & 1 \\ 0 & 0 & 0 & 0 \end{matrix} \, \middle\vert \, \begin{matrix} 9 \\ -4 \\ 1 \\ 1\end{matrix} \right]\end{split}\]

Enter your answer in this markdown cell by double clicking here:

QUESTION 1d: (3 points) Consider the following augmented matrix \([C|d]\), in Reduced Row Echelon Form (RREF), representing a system of linear equations, \(Cx=d\). Find ALL possible solutions, \(x\), of the system, and justify your answer.

\[\begin{split}[C|d] = \left[ \begin{matrix} 1 & 0 & 2 \\ 0 & 1 & 3 \\ 0 & 0 & 0 \end{matrix} \, \middle\vert \, \begin{matrix} 9 \\ -4 \\ 0\end{matrix} \right]\end{split}\]

Enter your answer in this markdown cell by double clicking here:


2. Working at MSU (12 points)#

Yoshi works at MSU as both a clerical assistant and a learning assistant. Yoshi gets paid \(\$13.7\) an hour for working as a clerical assistant and \(\$14.50\) an hour for working as a learning assistant. Last week, Yoshi worked a total of \(23\) hours and earned a total of \(\$323.9\). (Ignore taxes for this problem.) If we let \(c\) to be the number of hours Yoshi worked as a clerical assistant and \(l\) to be the number of hours Yoshi worked as a learning assistant then we get two equations:

\[ c + l = 23\]
\[ 13.7c + 14.50l = 323.9\]

QUESTION 2a: (6 points) Represent the above system of equations as a matrix equation of the form \(Ax = b\) and then use numpy to solve the system and display your answer. Store your answer in the variable x for answer check.

Notes: Use numpy.matrix (not numpy.array) to ensure that the answercheck returns answer correctly.

##Edit this cell to answer the question.
from answercheck import checkanswer

checkanswer.vector(x,"f89aacde9530f92290a813b17aac0ae6");

QUESTION 2b: (6 points) Use complete sentences to describe what the three values \(A_{2,1}\), \(x_{2}\), and \(b_{1}\) represent in the context of Yoshi’s situation.

COMPLETE THE FOLLOWING SENTENCES..

The value \(A_{2,1}\) (the entry in the second row and first column of \(A\)) represents

The value \(x_{2}\) (the second entry of \(x\)) represents…..

The value \(b_{1}\) (the first entry of \(b\)) represents…..


3. Balancing Chemical Equations (20 points)#

Image from Wikipedia article on Stoichiometry.

A chemical formula is a symbolic representation which shows how many atoms of each element make up a molecule. For example, water (denoted by \(H_2O\)) is a molecule with two \(H\) (hydrogen) atoms and one \(O\) (oxygen) atom. A chemical equation is the symbolic representation of a chemical reaction, wherein the reactant molecules are given on the left-hand side and the product molecules on the right-hand side. The coefficients of the symbols represent the number of molecules of each chemical required to preserve matter in the chemical reaction.

For example, the process of burning methane is represented by the chemical equation

\[ 1 CH_4 + 2 O_2 \rightarrow 1CO_2 + 2 H_2O\]

The above equation shows that one molecule of \(CH_4\) (methane) and two molecules of \(O_2\) (oxygen) turn into one molecule of \(CO_2\) (carbon dioxide) and two molecules of \(H_2O\) (water). We see there are a total of one \(C\) (carbon) atom, four \(H\) (hydrogen) atoms, and four \(O\) (oxygen) atoms on the left side, and the same on the right side. So matter is neither created nor destroyed by the reaction.

Now, suppose that we know that burning methane requires \(CH_4\) (methane) and \(O_2\) (oxygen) molecules and produces \(CO_2\) (carbon dioxide) and \(H_2O\) (water) molecules, but we don’t know how many of each molecule are required for the reaction to take place or how many of each molecule are will be produced by the reaction.

We can assign coefficients that represent the number of each molecule involved in the reaction like this:

\[x_1 CH_4 + x_2 O_2 \rightarrow x_3 CO_2 + x_4 H_2O\]

Here we see that each molecule in the reaction gives us an unknown variable. For each element, we can obtain an equation in these variables by using the fact that the total number of atoms of that element on the left side and right side must be equal:

\[ C: x_1 = x_3 \]
\[ H: 4x_1 = 2x_4\]
\[ O: 2x_2 = 2x_3 + x_4\]

We can convert represent this system of equations by the augmented matrix $\( \left[ \begin{matrix} 1 & 0 & -1 & 0 \\ 4 & 0 & 0 &-2 \\ 0 & 2 & -2 & -1 \end{matrix} \, \middle\vert \, \begin{matrix} 0 \\ 0 \\ 0 \end{matrix} \right] \stackrel{RREF}{\longrightarrow} \left[ \begin{matrix} 1 & 0 & 0 & -0.5 \\ 0 & 1 & 0 &-1 \\ 0 & 0 & 1 & -0.5 \end{matrix} \, \middle\vert \, \begin{matrix} 0 \\ 0 \\ 0 \end{matrix} \right]\)$

This leads us to equations $\(x_1 - \frac 1 2 x_4 = 0\)$

\[ x_2 - x_4 = 0\]
\[ x_3 - \frac 1 2 x_4 = 0\]

To get integer values for our solution we can take \(x_4 =2\) and see that \(x_1 = 1, x_2 = 2,\) and \(x_3 = 1\), which is consistent with the chemical equation for burning methane.


For “simple” chemical reactions, balancing the formula by solving a system of linear equations might seem like overkill, as the coefficients are usually small and be obtained by guessing and checking. However, for more complicated chemical reactions, this method has value.

The (unbalanced) chemical reaction $\(KNO_3 + C_{12}H_{22}O_{11} \rightarrow N_2 + CO_2 + H_2O + K_2CO_3\)\( represents combining \)KNO_3\( (potassium nitrate) and \)C_{12}H_{22}O_{11}\( (sucrose) to produce \)N_2\( (nitrogen), \)CO_2\( (carbon dioxide), \)H_2O\( (water), and \)K_2CO_3$ (potassium carbonate). To balance this reaction, we will once again assign coefficients for each molecule in the reaction

\[x_1KNO_3 + x_2C_{12}H_{22}O_{11} \rightarrow x_3N_2 + x_4CO_2 + x_5H_2O + x_6K_2CO_3.\]

Again, for each element, we will have one equation which arises from the fact that the number of atoms of that element on the left and right sides must be equal:

\[C: 12x_2 = x_4 + x_6\]
\[H: 22x_2 = 2x_5\]
\[K: x_1 = 2x_6\]
\[N: x_1 = 2x_3\]
\[O: 3x_1 + 11x_2 = 2x_4 + x_5 + 3x_6\]

QUESTION 3a: (4 points) 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:

\[?x_1 + ?x_2 + ?x_3 + ?x_4 + ?x_5 + ?x_6 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 + ?x_5 + ?x_6 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 + ?x_5 + ?x_6 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 + ?x_5 + ?x_6 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 + ?x_5 + ?x_6 = ?\]

QUESTION 3b: (4 points) 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:

\[\begin{split}x_1\begin{bmatrix}? \\ ? \\ ? \\ ? \\ ?\end{bmatrix} + x_2\begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix} + x_3\begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix} + x_4\begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix} + x_5\begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix} + x_6\begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix} = \begin{bmatrix}? \\ ?\\ ? \\ ? \\ ?\end{bmatrix}\end{split}\]

QUESTION 3c: (6 points)

  • (3 pts) Represent the above system of linear equations as an augmented matrix \(M\), and store the augmented matrix as a numpy matrix called M for answer check.

  • (3 pts) (Using sympy) Find the reduced echelon form of M and store it as a sympy matrix called M_RREF for answer check.

#Edit this cell to answer the question of finding M
M = np.matrix("")
from answercheck import checkanswer
# check if the augmented matrix M is correct
checkanswer.matrix(M,"62674079c98c89caa39e62be575359f2")
#Edit this cell to answer the question of finding M_RREF as a sympy matrix
M_RREF = sym.Matrix()
from answercheck import checkanswer
# check if the M_RREF is correct
checkanswer.matrix(sym.Matrix(M_RREF),"da1d7eb7e0e258460654b03f18d3d522")

QUESTION 3d: (6 points) Find the solution to this system of equations for which \(x_1,x_2,x_3,x_4,x_5,x_6\) are all positive integers and \(x_6\) is as small as possible. Save your answer as solution which is the list \([x_1,x_2,x_3,x_4,x_5,x_6]\)

Hint: If you run the cell below, the non-integer entries of M_RREF will be displayed as fractions instead of as a decimal.

M_RREF
#Edit this cell to save your answer.
solution=[1,2,3,4,5,6]
from answercheck import checkanswer

checkanswer.vector(np.matrix(solution),"7946d0c288fc32c4a6342e0cb54b793c");

4. Traffic Control (15 points)#

Image of traffic coming in and out of 4 intersections

The city of Lansing is trying to resolve traffic congestion at the intersections of Saginaw, Larch, Michigan, and Cedar, as shown in the figure above. The streets are one-way, as indicated by the arrows.#

The city wishes to improve the signals at these corners so as to speed the trafic flow. The data the traffic engineers have gathered shows the following:

  • 300 cars/hour enter intersection A on Saginaw.

  • 500 cars/hour enter intersection A on Cedar.

  • 200 cars/hour leave intersection B on Larch.

  • 100 cars/hour leave intersection B on Saginaw.

  • 300 cars/hour enter intersection C on Michigan.

  • 200 cars/hour enter intersection C on Larch.

  • 600 cars/hour leave intersection D on Cedar.

  • 400 cars/hour leave intersection D on Michigan.

We can then use the variables:

  • \(x_1\) to denote the number of cars/hour leaving intersection A and entering intersection B on Saginaw

  • \(x_2\) to denote the number of cars/hour leaving intersection A and entering intersection D on Cedar

  • \(x_3\) to denote the number of cars/hour leaving intersection C and entering intersection D on Michigan

  • \(x_4\) to denote the number of cars/hour leaving intersection C and entering intersection B on Larch

The number of cars entering intersection B must equal the number leaving intersection B, so $\(x_1+x_4 = 100+200\)\( or equivalently \)\(x_1+x_4 = 300\)$

Question 4a: (3 points) Write the three equations represeting the traffic entering and leaving intersections A, C, and D.

Put your answer to the above question here.

Intersection A:

Intersection C:

Intersection D:

Question 4b: (9 points, 3+3+3) Use the four equations to write 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.

#Put your answer to the above question here (you can double click this cell and edit the \(\LaTeX\) code for these matrices).

Row form:

\[?x_1 + ?x_2 + ?x_3 + ?x_4 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 = ?\]
\[?x_1 + ?x_2 + ?x_3 + ?x_4 = ?\]

Column form:

\[\begin{split}x_1\begin{bmatrix}? \\ ? \\ ?\\ ?\end{bmatrix} + x_2\begin{bmatrix}? \\ ? \\ ?\\ ?\end{bmatrix} + x_3\begin{bmatrix}? \\ ? \\ ?\\ ?\end{bmatrix} + x_4\begin{bmatrix}? \\ ? \\ ?\\ ?\end{bmatrix} = \begin{bmatrix}? \\ ? \\? \\? \end{bmatrix}\end{split}\]

Augmented matrix form:

\[\begin{split}\left[ \begin{array}{rrrr|r} a & b & c & d &f \\ a & b & c & d &f \\ a & b & c & d &f \\ a & b & c & d &f \end{array} \right]\end{split}\]

Question 4c: (1 points) Find the reduced echelon form of the agumented matrix above, and store it as a sympy matrix named A_RREF for answercheck.

#Edit this cell to answer the question of finding M_RREF as a sympy matrix
A_RREF = sym.Matrix()
checkanswer.matrix(A_RREF,"2443e397c8b20ffdfc91c204864f6eac")

Question 4d: (2 points) The solution to this system of equations (with \(x_4\) as a parameter) is: $\(x_1 = ?\)\( \)\(x_2 = ?\)\( \)\(x_3 = ?\)\( \)\(x_4 = ?\)$

Question 4e: (3 points) Based on your solution in 4c, what are the largest and smallest possible values for the number of cars/hour leaving intersection C and entering intersection B on Larch? Justify your answer.

#Put your answer to the above question here


5. Visualizing the Equations (18 points)#

In this problem, we need to solve and visualize the Row Pictures and the Column Pictures of the following system of equations.

\[\begin{split}\begin{align} 2x - 2y &= x-3\\ 3x + 3y +2 &= 19 + 4x -y \end{align}\end{split}\]

Note: You can try simplify the system.

Question 5a: (4 points, 2+2) Use the equations to write the above system in augmented matrix form below and give the answer of \(x\) and \(y\) .

All you need to do is replace the \(?\)’s with the appropriate numbers.

Augmented matrix form:

\[\begin{split}\left[ \begin{array}{rr|r} ? & ? & ? \\ ? & ? & ? \end{array} \right]\end{split}\]
\[x=?\]
\[y=?\]

Question 5b: (6 points) Using Matplotlib, make a plot showing the Row Picture of the system. Determine (based on the answer of 5a) the solution \((x,y)\) where the two lines intersect, and plot a dot at this point.

%matplotlib inline
import matplotlib.pylab as plt
import numpy as np

# Put your code here

Question 5c: (8 points, 2+6) Writing the Column form of the equitions. And using Matplotlib, make a plot showing the Column Picture of the system. Using the solution \((x,y)\) from 5a, draw the picture as follows:

  • the vector \(x\vec{u}\) (a scalar multiple of \(\vec{u}\)) in blue with the starting point at the origin

  • the vector \(y\vec{v}\) (a scalar multiple of \(\vec{v}\)) in red with the starting point at the end of the vector \(x\vec{u}\),

  • the target vector \(\vec{w}\) in purple with the starting point at the origin and the ending point at the end of the vector \(y\vec{v}\)

It is not required to use the above mentioned colors, but it is necessary to draw the above arrows with three different colors.

All you need to do is replace the \(?\)’s with the appropriate numbers.

The column form of the system of equations is

\[\begin{split}x\begin{bmatrix}? \\ ?\end{bmatrix} + y\begin{bmatrix}? \\ ?\end{bmatrix} = \begin{bmatrix}? \\ ?\end{bmatrix}\end{split}\]
%matplotlib inline
import matplotlib.pylab as plt
import numpy as np

# Put your code here

6. Numerical Error (8 points)#

Computers system mainly uses the binary (base 2) number system. Therefore, inputting the decimal system we use in life to a computer requires the use of machine language to convert it to a binary system, and then some rounding errors occur in this process (we have seen this in ICA-03).

Depending on the problem we may need exact precisions or a bound of this round-off error. For a given value \(x\) (\(x\neq 0\)) and its approximation \(\tilde{x}\), we define the following terms:

  • relative error: $\( \delta_{rel} = \frac{|x-\tilde{x}|}{|x|}\)$

  • absolute error $\( \delta_{abs} = |x-\tilde{x}|\)$

# use `np.e` to see e up to 15 digits
np.e

Question 6a: (3 points) Let $\( x = 2.71828 18284 59045 \)\( is the value of the Euler number \)e$ correct to 15 decimal places (you can call np.e to retrieve it) and

  • \(x_1\) = 2.718281;

  • \(x_2\) = 2.7182;

  • \(x_3\) = 2.71829;

be approximations of \(x\). For each of the three approximations, compute the relative error and the absolute error to \(15\) decimal places.

The relative errors (\(\delta_{rel}\)) and the absolute errors (\(\delta_{abs}\)) are:

\[\begin{split} \begin{align*} \delta_{1,rel}&=? &\qquad \delta_{1,abs}&=? \\ \delta_{2,rel}&=? &\qquad \delta_{2,abs}&=? \\ \delta_{3,rel}&=? &\qquad \delta_{3,abs}&=? \\ \end{align*} \end{split}\]

Note: You can use print(f'{x:.15f}') to display 15 decimal digits of x

Question 6b: (2 points) Consider two numbers \(a = 1.42892301\) and \(b = 1.42892201973\).

(i) Is \(a\) a sufficiently close approximation of \(b\) if we require a relative error of at most \(0.001\)?

(ii) Is \(a\) a sufficiently close approximation of \(b\) if we require a relative error of at most \(0.0000001\)?

Enter your answers:

Question 6c: (3 points) Modify the cell below to define a function that determines whether a is a sufficiently close approximation of b if we require an absolute error of at most tol and returns True if a is sufficiently close to b, and False otherwise. The cell below that one tests this function with two numbers \(a=-12801.092382039847\) and \(b=-12801.092372039847\), and \(tol = 0.00001\).

# Modify the following function to return **True** if the number a == b at the precision tol, otherwise return **False**
def CheckEquivalence():
    return
# Test your function 
a = -12801.092382039847
b = -12801.092372039847
tol = 0.00001

print(CheckEquivalence(a, b, tol))

Congratulations, we’re done!#