For your homework report this week, you do not need to submit solutions to the swirl lessons and matrix exercises 1-6. For exercises 1-6, you should say which parts you were able to compute by hand and check successfully using R. You do need to submit a solution to exercise 7. As usual, you also need to make Sources and Please explain statements.

More swirl lessons

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 (on 1/11 or 1/12) if difficulties arise.


Matrix exercises

It is good to do at least one example of each of the fundamental matrix operations by hand, even though in practice we almost always use a computer. For the homework, you are required to carry out and check exactly one example of each operation, but the code provided can be used to make more examples until you are comfortable with carrying out all of them.

We will generates exercises using the following function:

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(1)
A <- randomMatrix(2,2)
B <- randomMatrix(2,2)
A
##      [,1] [,2]
## [1,]   -2    1
## [2,]   -1    4
B
##      [,1] [,2]
## [1,]   -3    4
## [2,]    4    1
  1. Addition. Compute by hand the matrix sum \[ {\mathbb{A}}+{\mathbb{B}} = \begin{bmatrix} -2 & 1 \\ -1 & 4 \\ \end{bmatrix} + \begin{bmatrix} -3 & 4 \\ 4 & 1 \\ \end{bmatrix} \]

Then check your calculation using R.

  1. Scalar multiplication. Compute by hand the following product of a scalar and a matrix \[ 2 {\mathbb{A}} = 2 \begin{bmatrix} -2 & 1 \\ -1 & 4 \\ \end{bmatrix} \]

Then check your calculation using R.

  1. Multiplying two square matrices. Compute by hand the matrix product \[ {\mathbb{A}}{\mathbb{B}} = \begin{bmatrix} -2 & 1 \\ -1 & 4 \\ \end{bmatrix} \begin{bmatrix} -3 & 4 \\ 4 & 1 \\ \end{bmatrix} \]

Then check your calculation using R.

  1. Multiplying two rectangular matrices. Compute by hand the product \({\mathbb{C}}{\mathbb{D}}\) where \({\mathbb{C}}\) and \({\mathbb{D}}\) are given by
set.seed(2)
C <- randomMatrix(2,3)
D <- randomMatrix(3,2)

Then check your calculation using R.

  1. Inverting a \(2\times 2\) matrix. Compute by hand the matrix inverse \[ {\mathbb{A}}^{-1} = \begin{bmatrix} -2 & 1 \\ -1 & 4 \\ \end{bmatrix} ^{-1} \]

Then check your calculation using R. Generate more examples using randomMatrix() until you are confident about inverting \(2\times 2\) matrices by hand.

  1. Transposing a matrix. Compute by hand the transpose of \({\mathbb{C}}\), \[ {\mathbb{C}}^{{{\scriptscriptstyle \mathrm{T}}}} = \begin{bmatrix} -3 & 1 & 4 \\ 2 & -3 & 4 \\ \end{bmatrix} ^{T} \]

Then check your calculation using R.

  1. Solve the following system of linear equations using R. \[\begin{array}{ccccccccc} -3w &+& x &+& y && &=& 2 \\ 3w &+& x &+& y &+& z &=& 1 \\ -w &-& 3x && &+& 3z &=& -1 \\ -2w &-& 2x &&&+& 3z &=& 4 \end{array}\]

License: This material is provided under an MIT license