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