Problem Set 1:
1. In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a \(3x2\) matrix A
# First create matrix A
A <- matrix(c(1,2,3,-1,0,4), nrow = 2)
# Then transpose matrix A using t()
AT <- t(A)
AT
## [,1] [,2]
## [1,] 1 2
## [2,] 3 -1
## [3,] 0 4
# Multiply the transposed matrix A by matrix A
Y<- AT %*% A
Y
## [,1] [,2] [,3]
## [1,] 5 1 8
## [2,] 1 10 -4
## [3,] 8 -4 16
# Multiply A by transposed matrix A (AA^T)
X<- A %*% AT
X
## [,1] [,2]
## [1,] 10 -1
## [2,] -1 21
# compute eigenvalues and eigenvectors
EX = eigen(X)
EY = eigen(Y)
SA <- svd(A)
#As you can see the first two columns are the Evectors of X and the right are the left singular for A
cbind(EX$vectors, SA$u)
## [,1] [,2] [,3] [,4]
## [1,] -0.0898056 -0.9959593 -0.0898056 0.9959593
## [2,] 0.9959593 -0.0898056 0.9959593 0.0898056
#As you can see the first two columns are the Evectors of X and the right are the right singular for A except the Evectors for Y has the third column that is not present in svd for the right singular.
cbind(EY$vectors, SA$v)
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.4141868 -0.3734355 0.8300574 0.4141868 0.3734355
## [2,] 0.2755368 -0.9206109 -0.2766858 -0.2755368 0.9206109
## [3,] -0.8674842 -0.1141117 -0.4842001 0.8674842 0.1141117
#to compute the non zero values, you need to take the sqrt of the eigenvalues for X and compare it to the singulard values of svd for A.
cbind(sqrt(EX$values), SA$d)
## [,1] [,2]
## [1,] 4.592404 4.592404
## [2,] 3.147988 3.147988
2. Using the procedure outlined in section 1 of the weekly handout, write a function tocompute the inverse of a well-conditioned full-rank square matrix using co-factors. Your function should be myinverse(A).
A <- matrix(c(2,3,6,6,3,5,5,8,9),nrow=3)
myinverse <- function(A){
#generate empty I matrix first
I <- diag(1,nrow(A),ncol(A))
#iterate over the rows
for (i in 1:nrow(A)) {
#iterate over the columns
for (j in 1:ncol(A)){
#Calucate the value for each cell in the matrix
Mmini <- A[-i,-j]
#-1 raised to a power provides the appropriate signs as the mini det is calculated for the 2x2s
I[i,j] <- ((-1)^(i+j))*det(Mmini)
}
}
#finish the calculation by dividing by the det of A
return(t(I)/det(A))
}
#Original Matrix cbind to Inverse matrix B
B=myinverse(A)
A%*%B
## [,1] [,2] [,3]
## [1,] 1.000000e+00 0 1.110223e-16
## [2,] 2.220446e-16 1 2.220446e-16
## [3,] 5.551115e-16 0 1.000000e+00
#althought the values are in scinotation you can see the 1's along the diagonal and zero/near zero in the other cells.