In this problem, we’ll verify using R that SVD and Eigenvalues are related as worked out in the weekly module. Given a 3 by 2 matrix A A = 1 2 3 ô1 0 4
A<-matrix(c(1,2,3,1,0,4),2,3,byrow=TRUE)
X <- A%*%t(A)
Y <- t(A)%*%A
X
## [,1] [,2]
## [1,] 14 13
## [2,] 13 17
Y
## [,1] [,2] [,3]
## [1,] 2 2 7
## [2,] 2 4 6
## [3,] 7 6 25
e.Y<-eigen(Y)
e.X<-eigen(X)
e.Y$values
## [1] 2.858625e+01 2.413748e+00 -9.583074e-16
e.X$values
## [1] 28.586252 2.413748
e.X
## eigen() decomposition
## $values
## [1] 28.586252 2.413748
##
## $vectors
## [,1] [,2]
## [1,] 0.6653480 -0.7465334
## [2,] 0.7465334 0.6653480
e.Y
## eigen() decomposition
## $values
## [1] 2.858625e+01 2.413748e+00 -9.583074e-16
##
## $vectors
## [,1] [,2] [,3]
## [1,] -0.2640703 -0.05225548 0.9630868
## [2,] -0.2488859 -0.96102189 -0.1203859
## [3,] -0.9318383 0.27148903 -0.2407717
s.A<-svd(A)
#Siginular values of A
s.A$d
## [1] 5.346611 1.553624
#left singular vectors of A
s.A$u
## [,1] [,2]
## [1,] -0.6653480 -0.7465334
## [2,] -0.7465334 0.6653480
e.X$vectors
## [,1] [,2]
## [1,] 0.6653480 -0.7465334
## [2,] 0.7465334 0.6653480
#right singular vectors of A
s.A$v
## [,1] [,2]
## [1,] -0.2640703 -0.05225548
## [2,] -0.2488859 -0.96102189
## [3,] -0.9318383 0.27148903
e.Y$vectors
## [,1] [,2] [,3]
## [1,] -0.2640703 -0.05225548 0.9630868
## [2,] -0.2488859 -0.96102189 -0.1203859
## [3,] -0.9318383 0.27148903 -0.2407717
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 AB = I. The o-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.
ff<- function(A=ls())
{
print("Matrix A is:")
print (A)
#Determinant of A
print("Determanint of A is:")
print(det(A))
#Cofactor of A
print("The Cofactors are:")
c<-solve(A)*det(A)
print(c)
#Diag of det matrix
print("The diagonals of the cofactor matrix are:")
diag<-diag(solve(A)*det(A))
print(diag)
#Inverse cofactor checks
print("Subtracting inverses gives a 0 matrix so they are equal")
print(1/det(A)*solve(A)*det(A) - solve(A))
}
ff(matrix(c(0,0,1,2,-1,3,1,1,4),3,3))
## [1] "Matrix A is:"
## [,1] [,2] [,3]
## [1,] 0 2 1
## [2,] 0 -1 1
## [3,] 1 3 4
## [1] "Determanint of A is:"
## [1] 3
## [1] "The Cofactors are:"
## [,1] [,2] [,3]
## [1,] -7 -5 3
## [2,] 1 -1 0
## [3,] 1 2 0
## [1] "The diagonals of the cofactor matrix are:"
## [1] -7 -1 0
## [1] "Subtracting inverses gives a 0 matrix so they are equal"
## [,1] [,2] [,3]
## [1,] 0 0 0
## [2,] 0 0 0
## [3,] 0 0 0