Quantcast

Inverse matrix using eigendecomposition

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Inverse matrix using eigendecomposition

wwreith
General goal: Write R code to find the inverse matrix of an nxn positive definite symmetric matrix. Use solve() to verify your code works.

Started with a 3x3 matrix example to build the code, but something dosen't seem to be working. I just don't know where I am going wrong.

##Example matrix I found online
A<-c(4,1,-1,1,2,1,-1,1,2)
m<-matrix(A,nrow=3,ncol=3)

##Caculate the eigen vectors and eigenvalues
E<-eigen(m, sym=TRUE)
Q<-E$vectors
V<-E$values
n<-nrow(m)

##normalize the eigenvectors
for(i in 1:n){
  Q[,i]<-Q[,i]/sqrt(sum(Q[,i]^2))
}

##verify dot product of vectors are orthogonal
sum(Q[,1]*Q[,2])
sum(Q[,1]*Q[,3])
sum(Q[,2]*Q[,3])

##Begin creating QDQ^T matrix. Where Q are orthonormal eigenvectors, and D is a diagonal matrix with 1/eigenvalues on the diagonal. and Q^T is the transpose of Q.

R<-t(Q)
D<-mat.or.vec(n,n)
for(i in 1:n) {
  D[i,i]<-1/V[i]
  }
P<-Q*D*R

## P should be the inverse of the matrix m. Check using

solve(m)

## solve(m) does not equal P? Any ideas of what I am missing/not understanding?
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Inverse matrix using eigendecomposition

Berend Hasselman
wwreith wrote
General goal: Write R code to find the inverse matrix of an nxn positive definite symmetric matrix. Use solve() to verify your code works.

Started with a 3x3 matrix example to build the code, but something dosen't seem to be working. I just don't know where I am going wrong.

##Example matrix I found online
A<-c(4,1,-1,1,2,1,-1,1,2)
m<-matrix(A,nrow=3,ncol=3)

##Caculate the eigen vectors and eigenvalues
E<-eigen(m, sym=TRUE)
Q<-E$vectors
V<-E$values
n<-nrow(m)

##normalize the eigenvectors
for(i in 1:n){
  Q[,i]<-Q[,i]/sqrt(sum(Q[,i]^2))
}

##verify dot product of vectors are orthogonal
sum(Q[,1]*Q[,2])
sum(Q[,1]*Q[,3])
sum(Q[,2]*Q[,3])

##Begin creating QDQ^T matrix. Where Q are orthonormal eigenvectors, and D is a diagonal matrix with 1/eigenvalues on the diagonal. and Q^T is the transpose of Q.

R<-t(Q)
D<-mat.or.vec(n,n)
for(i in 1:n) {
  D[i,i]<-1/V[i]
  }
P<-Q*D*R

## P should be the inverse of the matrix m. Check using

solve(m)

## solve(m) does not equal P? Any ideas of what I am missing/not understanding?
Homework questions are not answered.

But to give you a hint: look in the "An Introduction to R" manual Chapter 5 "Array and Matrices" especially section 5.7 "Matrix facilities". (http://cran.r-project.org/doc/manuals/R-intro.html)

You should be able to work out what's wrong in your script (a single statement).

Berend
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Inverse matrix using eigendecomposition

wwreith
This post was updated on .
Sorry but I am not a student, at least not since 2007. However I am performing grunt work for a someone with a Ph.D. so it does remind me of the student days. I just have a pay check instead of student loans.    


I assume Q*D*R instead of Q %*% D %*% R is the mistake. At least you didn't say I blundered the theory.
Loading...