Continuing from Homework 1, complete the following swirl modules: Lesson 5: Missing Values
, Lesson 6: Subsetting Vectors
, Lesson 7: Matrices and Data Frames
, Lesson 9: Functions
. We will not need the material in Lesson 8: Logic
, but you can do this too if you like. You will have a chance to ask swirl questions in lab if difficulties arise. In this class, we will emphasize using existing functions rather than writing our own, but it will help you to understand how new functions are made.
Q1. Write a brief comment on what you did with swirl, which tutorials you successfully completed, and the technical or conceptual obstacles (if any) that you encountered and (hopefully) overcame. This could be as little as a single sentence.
Q1 answer.
Credit was given as long as a sentence or more was written demonstrating engagement with the assignment.
In Q2-Q8, you are asked to carry out and check examples of each matrix operation we have covered in class, both by hand and using a computer. The code provided can be used to make more examples by changing the value of set.seed
used to generate the random numbers. Computers use a random number generator that is actually a deterministic algorithm which produces a long sequence of numbers that behave much like random numbers. The seed tells the algorithm where to start reading off numbers from this sequence.
Particularly if you are in the 25% of the class that that has no previous experience working with matrices, you are recommended to do extra examples until you are comfortable working them by hand.
We will generates exercises using the following function:
# randomMatrix() generates a p x q matrix with elements chosen randomly from the given values
randomMatrix <- function(p,q,values=-4:4){
matrix(sample(values,size=p*q,replace=TRUE),p,q)
}
This produces a \(p\times q\) matrix with random entries which are, by default, drawn with replacement from the sequence \(-4,-3,\dots,0,\dots,3,4\). Let’s use randomMatrix
to produce two \(2\times 2\) matrices \({\mathbb{A}}\) and \({\mathbb{B}}\), setting the seed of the random number generator so that the function returns the same matrix each time
set.seed(5)
A <- randomMatrix(2,2)
B <- randomMatrix(2,2)
A
## [,1] [,2]
## [1,] -3 4
## [2,] 2 -2
B
## [,1] [,2]
## [1,] -4 0
## [2,] 2 3
Q2. Matrix addition. Compute by hand the matrix sum \[ {\mathbb{A}}+{\mathbb{B}} = \begin{bmatrix} -3 & 4 \\ 2 & -2 \\ \end{bmatrix} + \begin{bmatrix} -4 & 0 \\ 2 & 3 \\ \end{bmatrix} \]
Then check your calculation using R. Report your answer and your R code.
Q2 answer.
\[ \mathbb{A}+\mathbb{B}= \begin{bmatrix} -3+ (-4) & 4+0\\ 2+2 & -2+3 \end{bmatrix} = \begin{bmatrix} -7 & 4\\ 4 & 1 \end{bmatrix} \]
A+B
## [,1] [,2]
## [1,] -7 4
## [2,] 4 1
Q3. Scalar times matrix multiplication. Compute by hand the following product of a scalar and a matrix \[ 2 {\mathbb{A}} = 2 \begin{bmatrix} -3 & 4 \\ 2 & -2 \\ \end{bmatrix} \]
Then check your calculation using R. Report your answer and your R code.
Q3 answer. \[ 2\mathbb{A}= \begin{bmatrix} 2(-3)& 2(-4)\\ 2(2) & 2(-2) \end{bmatrix} = \begin{bmatrix} -6 & -8\\ 4 & -4 \end{bmatrix} \]
Confirming with R
2*A
## [,1] [,2]
## [1,] -6 8
## [2,] 4 -4
Q4. Multiplying two square matrices. Compute by hand the matrix product \[ {\mathbb{A}}\,{\mathbb{B}} = \begin{bmatrix} -3 & 4 \\ 2 & -2 \\ \end{bmatrix} \begin{bmatrix} -4 & 0 \\ 2 & 3 \\ \end{bmatrix} \]
Then check your calculation using R. Report your answer and your R code. Also report whether you successfully found the answer calculating my hand.
Q4 answer. \[ \mathbb{A}\mathbb{B}= \begin{bmatrix} 12+8& 10+12\\ -8 + (-4) & 0+(-6) \end{bmatrix} = \begin{bmatrix} 20 & 12\\ -12 & 6 \end{bmatrix} \]
Confirming with R
A%*%B
## [,1] [,2]
## [1,] 20 12
## [2,] -12 -6
Thus, we successfully found the answer calculating by hand.
Q5. Multiplying two rectangular matrices. Compute by hand the product \({\mathbb{C}}{\mathbb{D}}\) where \({\mathbb{C}}\) and \({\mathbb{D}}\) are given by
set.seed(7)
C <- randomMatrix(2,3)
D <- randomMatrix(3,2)
Then check your calculation using R. Report your answer and your R code. Also report whether you successfully found the answer calculating my hand.
Q5 answer.
\[ \mathbb{C}\mathbb{D}= \begin{bmatrix} -4 + (-12)+6 & 0+9+4\\ 1+(-16) + (-9) & 0+12+(-6) \end{bmatrix} = \begin{bmatrix} -10 & 13\\ -24 & 6 \end{bmatrix} \]
Confirming with R
set.seed(7)
C <- randomMatrix(2,3)
D <- randomMatrix(3,2)
C%*%D
## [,1] [,2]
## [1,] -10 13
## [2,] -24 6
Thus, we successfully found the answer calculating by hand.
Q6. Multiplying a matrix and a column vector. Compute by hand the matrix product \[ {\mathbb{E}}\,{\mathbb{F}} = \begin{bmatrix} 2 & 0 & 1 & 3 \\ -3 & -3 & -1 & -3 \\ 0 & 3 & 3 & 1 \\ -3 & -1 & 3 & 0 \\ \end{bmatrix} \begin{bmatrix} 3 \\ -1 \\ 1 \\ -2 \\ \end{bmatrix} \]
Then check your calculation using R. Report your answer and your R code. Also report whether you successfully found the answer calculating my hand.
Q6 answer.
\[ \mathbb{E}\mathbb{F}= \begin{bmatrix} 6 + 0 +1+(-6)\\ -9+3+(-1)+6\\ 0+(-3)+3+(-2)\\ -9+1+3+0 \end{bmatrix} = \begin{bmatrix} 1\\ -1\\ -2\\ -5 \end{bmatrix} \]
Confirming with R
# Defining E
E <- matrix(c(2,-3,0,-3,0,-3,3,-1,1,-1,3,3,3,-3,1,0),4)
F_vec <- matrix(c(3,-1,1,-2),ncol=1)
E%*%F_vec
## [,1]
## [1,] 1
## [2,] -1
## [3,] -2
## [4,] -5
Thus, we successfully found the answer calculating by hand.
Q7. Inverting a \(2\times 2\) matrix. Compute by hand the matrix inverse \[ {\mathbb{A}}^{-1} = \begin{bmatrix} -3 & 4 \\ 2 & -2 \\ \end{bmatrix} ^{-1} \]
Then check your calculation using R. Report your answer and your R code. Also report whether you successfully found the answer calculating my hand.
Generate more examples using randomMatrix(2,2)
until you are confident about inverting \(2\times 2\) matrices by hand.
Q7 answer.
\[ \text{Det}(\mathbb{A})=(-3)(-2)-(4)(2) = -2\\ \implies \mathbb{A}^{-1} = -\frac{1}{2} \begin{bmatrix} -2 & -4\\ -2 & -3 \end{bmatrix} = \begin{bmatrix} 1 & 2\\ 1 & 1.5 \end{bmatrix} \]
Confirming with R
solve(A)
## [,1] [,2]
## [1,] 1 2.0
## [2,] 1 1.5
Thus, we successfully found the answer calculating by hand.
Q8. Transposing a matrix. Compute by hand the transpose of \({\mathbb{C}}\), \[ {\mathbb{C}}^{{{\scriptscriptstyle \mathrm{T}}}} = \begin{bmatrix} 4 & -3 & -2 \\ -1 & -4 & 3 \\ \end{bmatrix} ^{T} \]
Then check your calculation using R. Report your answer and your R code. Also report whether you successfully found the answer calculating my hand.
Q8 answer.
Transposing a matrix means interchanging the columns and the rows. \[ \mathbb{C}^T = \begin{bmatrix} 4 & -1\\ -3 & -4\\ -2 & 3 \end{bmatrix} \]
Confirming with R
t(C)
## [,1] [,2]
## [1,] 4 -1
## [2,] -3 -4
## [3,] -2 3
Thus, we successfully found the answer calculating by hand.
Q9. Checking dimensions for a matrix product. Let \(\underset{2\times 2}{{\mathbb{A}}}\), \(\underset{2\times 2}{{\mathbb{B}}}\), \(\underset{2\times 3}{{\mathbb{C}}}\), \(\underset{3\times 2}{{\mathbb{D}}}\), \(\underset{4\times 4}{{\mathbb{E}}}\) and \(\underset{4\times 1}{{\mathbb{F}}}\) be six matrices with the specified dimensions. Which of the following matrix products are possible:
Q9 answer.
a, c, e, g and h.
The others have incompatible dimensions, that is, the number of columns of the first matrix in the product (matrix on the left) is not equal to the number of rows of the second matrix (matrix on the right).
Q10. Solve the following system of linear equations using R. \[\begin{array}{ccccccccc} &-& 2 x &+& 2 y &- & z &=& -4 \\ -3 w &+& 2 x &+& y & & &=& -2\\ 3 w &-& 2 x && &- & 3 z &=& -2 \\ 1 w &+& 4 x &-& 4 y &+ & 4 z &=& 0 \end{array}\] Report the solution (to 3 significant figures) and your R code. You are not required to solve this by hand.
Q10 answer.
We will express this problem in the matrix form and solve that.
The matrix form of the problem is \[
\begin{bmatrix}
0 & -2 & 2 & -1\\
-3 & 2 & 1 & 0\\
3 & -2 & 0 & 3\\
1 & 4 & 4 & 4
\end{bmatrix}
\begin{bmatrix}
w\\
x\\
y\\
z
\end{bmatrix} =
\begin{bmatrix}
4\\
-2\\
-2\\
0
\end{bmatrix}
\]
Let us label the matrices as \(\mathbb{M_1},\mathbb{X},\mathbb{M_2}\) respectively. Hence, the system of equations is \[ \mathbb{M_1}\mathbb{X}=\mathbb{M_2} \] The solution to which can be obtained by \[ \mathbb{X} = \mathbb{M_1}^{-1} \mathbb{M_2} \]
Let us now compute this using R. We will first define the matrices and then solve for \(\mathbb{X}\) to get the values of the variables.
# Defining matrices
M1 <- matrix(c(0,-2,2,-1,-3,2,1,0,3,-2,0,-3,1,4,-4,4), byrow=TRUE, ncol=4)
M2 <- matrix(c(-4,-2,-2,0),ncol=1)
solve(M1)%*%M2
## [,1]
## [1,] -5.428571
## [2,] -5.214286
## [3,] -7.857143
## [4,] -1.285714
Thus we have that \(w=-5.428571\), \(x=-5.214286\), \(y=-7.857143\), \(z=-1.285714\).
As usual, you also need to make a Sources statement, which will let the grader know whether you completed the homework based on class and lab materials alone or whether you made use of additional resources.
License: This material is provided under an MIT license