Homework 04

Week04 Problem Set -1
Problem Set 1

\(\begin{aligned}\mathbf{A}=\left[\begin{matrix} 1 & 2 & 3 \\ -1 & 0 & 4 \end{matrix}\right]\end{aligned}\)

# Create a 3 X 2 matrix A
A <- matrix(c(1, 2, 3, -1, 0, 4), nrow = 2, ncol = 3, byrow = TRUE)
Matrix - A
A
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]   -1    0    4
Matrix - X & Y
# Compute X and Y
X <- A %*% t(A)
Y <- t(A) %*% A
X
##      [,1] [,2]
## [1,]   14   11
## [2,]   11   17
Y
##      [,1] [,2] [,3]
## [1,]    2    2   -1
## [2,]    2    4    6
## [3,]   -1    6   25
Eigenvalues/Eigenvectors of X
# Compute Eigenvectors and Eigenvvalues of X and save it in variable evX
evX <- eigen(X)
valuesX <- evX$values
vectorsX <- evX$vectors
evX
## $values
## [1] 26.601802  4.398198
## 
## $vectors
##           [,1]       [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635  0.6576043
Eigenvalues/Eigenvectors of Y
# Compute Eigenvectors and Eigenvvalues of Y and save it in variable evY
evY <- eigen(Y)
valuesY <- evY$values
vectorsY <- evY$vectors
evY
## $values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
## 
## $vectors
##             [,1]       [,2]       [,3]
## [1,] -0.01856629 -0.6727903  0.7396003
## [2,]  0.25499937 -0.7184510 -0.6471502
## [3,]  0.96676296  0.1765824  0.1849001
Left-singular, Singular values, Right-singular vectors of A
# Compute svd of matrix A using svd command
svdA <- svd(A)
svdA
## $d
## [1] 5.157693 2.097188
## 
## $u
##            [,1]       [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635  0.6576043
## 
## $v
##             [,1]       [,2]
## [1,]  0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296  0.1765824
Compare Values
SVD-u1 SVD-u2 EigenvectorX-v1 EigenvectorX-v2
-0.6576043 -0.7533635 0.6576043 -0.7533635
-0.7533635 0.6576043 0.7533635 0.6576043


SVD-v1 SVD-v2 EigenvectorY-v1 EigenvectorY-v2
0.0185663 -0.6727903 -0.0185663 -0.6727903
-0.2549994 -0.718451 0.2549994 -0.718451
-0.966763 0.1765824 0.966763 0.1765824


We notice that the singular vectors using the svd command are the same as eigenvectors of X and Y. There is a difference of only sign (either positive or negative). The ratio are otherwise the same.

SVD-d Square of (SVD-d) EigenvaluesX EigenvaluesY
5.1576934 26.6018017 26.6018017 26.6018017
2.0971882 4.3981983 4.3981983 4.3981983
1.05898210^{-16} (Equal to 0)


Two non-zero eigenvalues of both X and Y are the same and are squares of non-zero singular values of A as show in the above table.

Week04 Problem Set -2
Problem Set 2
myinverse <- function(A) {
  m <- nrow(A)
  n <- ncol(A)
  # Construct a cofactor matrix by initializing it to a diagonal matrix
  C <- diag(m)
  
  # Loop thru each element of the matrix A and assign cofactor matrix elements using determinants
  for (i in 1:m) {
    for (j in 1:n) {
      C[i, j] = ((-1)^(i+j)) * det(A[-i, -j])
    }
  }
  
  # Print the co-factor matrix C
  print ('Cofactor matrix')
  print (C)
  
  # Determine the transpose of the co-factor matrix
  CT = t(C)
  print ('Transpose of cofactor matrix')
  print (CT)
  
  # Determinant of matrix A
  detA = det(A)
  print ('Determinant of matrix A')
  print (detA)
  
  # Determine the inverse of the matrix by dividing the transpose of the co-factor matrix elements by the determinant
  AInv = 1/detA * CT
  
  # Return the inverse of the matrix
  return (AInv)
}

A <- matrix(c(7, 2, 1, 0, 3, -1, -3, 4, -2), nrow = 3, ncol = 3, byrow = TRUE)

#Matrix Inverse using co-factors
AInv1 <- myinverse(A)
## [1] "Cofactor matrix"
##      [,1] [,2] [,3]
## [1,]   -2    3    9
## [2,]    8  -11  -34
## [3,]   -5    7   21
## [1] "Transpose of cofactor matrix"
##      [,1] [,2] [,3]
## [1,]   -2    8   -5
## [2,]    3  -11    7
## [3,]    9  -34   21
## [1] "Determinant of matrix A"
## [1] 1
print(AInv1)
##      [,1] [,2] [,3]
## [1,]   -2    8   -5
## [2,]    3  -11    7
## [3,]    9  -34   21
#Matrix Inverse using solve() function of R
AInv2 <- solve(A)
print(AInv2)
##      [,1] [,2] [,3]
## [1,]   -2    8   -5
## [2,]    3  -11    7
## [3,]    9  -34   21
# Confirm matrix inverse AInv1 by multiplying it with Matrix A to get Identify Matrix
print (round(A %*% AInv1, 2))
##      [,1] [,2] [,3]
## [1,]    1    0    0
## [2,]    0    1    0
## [3,]    0    0    1

We observe that the inverse of matrix A is the same in both cases (co-factor method and using solve() function). Also, we can confirm that by multiplying matrix A with it’s inverse to get the identiy matrix I.