ASSIGNMENT 4 IS 605 FUNDAMENTALS OF COMPUTATIONAL MATHEMATICS - 2014 1. Problem Set 1 In this problem, weâll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 Ã 2 matrix A
# Make the matrix A
A <- matrix(c(1,2,3,
-1,0,4),
byrow = TRUE,
2,3)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] -1 0 4
# Calculate X
X <- A %*% t(A)
X
## [,1] [,2]
## [1,] 14 11
## [2,] 11 17
# Calcualte Y
Y <- t(A) %*% A
Y
## [,1] [,2] [,3]
## [1,] 2 2 -1
## [2,] 2 4 6
## [3,] -1 6 25
Eigenvalues and eigen vectors
paste("Eigen Valeus and Vectors of X")
## [1] "Eigen Valeus and Vectors of X"
# Find eigen value and vector of X
evx <- eigen(X)
# Print eigen value of X
evx$values
## [1] 26.601802 4.398198
# Print eigen vector of X
evx$vectors
## [,1] [,2]
## [1,] 0.6576043 -0.7533635
## [2,] 0.7533635 0.6576043
paste("Eigen values and vectors of y")
## [1] "Eigen values and vectors of y"
# Calculate eigen value and vecotr of Y
evy <- eigen(Y)
# Print eigen value of Y
evy$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
# Print eigen vector of Y
evy$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
Then, compute the left-singular, singular values, and right-singular vectors of A using the svd command.
# Do svd of A
svd_a <- svd(A)
left-singular
# Print left-singular value
svd_a$u
## [,1] [,2]
## [1,] -0.6576043 -0.7533635
## [2,] -0.7533635 0.6576043
singular values
# Print singular value
svd_a$d
## [1] 5.157693 2.097188
right-singular
# Print right singular value
svd_a$v
## [,1] [,2]
## [1,] 0.01856629 -0.6727903
## [2,] -0.25499937 -0.7184510
## [3,] -0.96676296 0.1765824
Examine the two sets of singular vectors and show that they are indeed eigenvectors of X and Y. Answer: From the results we can see that svd_a\(u is the eigenvectors of X and svd_a\)v is eigen vectors of Y
In addition, the two non-zero eigenvalues (the 3rd value will be very close to zero, if not zero) of both X and Y are the same and are squares of the non-zero singular values of A.
print("Two non-zero eigenvalues of X and Y")
## [1] "Two non-zero eigenvalues of X and Y"
evx$values
## [1] 26.601802 4.398198
evy$values
## [1] 2.660180e+01 4.398198e+00 1.058982e-16
print("Square of non-zero singular values of A")
## [1] "Square of non-zero singular values of A"
svd(A)$d^2
## [1] 26.601802 4.398198
From the data we can see that thy are same. 2. Problem Set 2 Using the procedure outlined in section 1 of the weekly handout, write a function to compute the inverse of a well-conditioned full-rank square matrix using co-factors. In order to compute the co-factors, you may use built-in commands to compute the determinant. Your function should have the following signature: B = myinverse(A) where A is a matrix and B is its inverse and AÃB = I. The off-diagonal elements of I should be close to zero, if not zero. Likewise, the diagonal elements should be close to 1, if not 1. Small numerical precision errors are acceptable but the function myinverse should be correct and must use co-factors and determinant of A to compute the inverse. Please submit PS1 and PS2 in an R-markdown document with your first initial and last name.
myinverse <- function(A) {
# check if matrix is full-rank and square
if (nrow(A) != ncol(A) ) {
return("Input is not a square matrix")
}
if(det(A)==0){
return("Matrix is singular")
}
if(Matrix::rankMatrix(A)[1] != nrow(A)){
return("Matrix is not full rank")
}
cofactor <- A*0
size <- nrow(A)
for (i in 1:size) {
for (j in 1:size) {
# M is A with row i & column j excluded
M <- A[-i, -j]
cofactor[i, j] <- (-1)^(i + j) * det(M)
}
}
print("Cofactor of A")
print(cofactor)
# Calcualte B
B <- t(cofactor) / det(A)
# Return result
return(B)
}
Testing
A <- matrix(sample(1:100,25),5,5)
A
## [,1] [,2] [,3] [,4] [,5]
## [1,] 50 61 95 18 30
## [2,] 97 64 78 14 46
## [3,] 94 13 17 2 91
## [4,] 6 22 96 70 74
## [5,] 60 40 12 100 24
print("Using built in function")
## [1] "Using built in function"
solve(A)
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.09794122 0.09973243 -0.03398867 0.02341248 -0.012042085
## [2,] 0.19483829 -0.18297766 0.06900948 -0.06028002 0.031361808
## [3,] -0.08567988 0.08999263 -0.03769092 0.03102719 -0.018141805
## [4,] -0.03049195 0.02517145 -0.01314442 0.01153068 0.004155959
## [5,] 0.09001229 -0.09424568 0.04356974 -0.02162260 0.011256607
print("Soluiton using my function")
## [1] "Soluiton using my function"
myinverse(A)
## [1] "Cofactor of A"
## [,1] [,2] [,3] [,4] [,5]
## [1,] 45195136 -89908448 39537120 14070560 -41536320
## [2,] -46021696 84435344 -41527248 -11615408 43489824
## [3,] 15684128 -31844536 17392536 6065512 -20105328
## [4,] -10803728 27816316 -14317548 -5320852 9977784
## [5,] 5556840 -14471958 8371566 -1917774 -5194380
## [,1] [,2] [,3] [,4] [,5]
## [1,] -0.09794122 0.09973243 -0.03398867 0.02341248 -0.012042085
## [2,] 0.19483829 -0.18297766 0.06900948 -0.06028002 0.031361808
## [3,] -0.08567988 0.08999263 -0.03769092 0.03102719 -0.018141805
## [4,] -0.03049195 0.02517145 -0.01314442 0.01153068 0.004155959
## [5,] 0.09001229 -0.09424568 0.04356974 -0.02162260 0.011256607