Page 1 of 1

Eigenvalues, eigenvectors in VBA

Posted: January 13th, 2003, 5:48 pm
by Securitize
Hello everyone,I am trying to create some automation in VBA that will compute eigenvalues and eigenvectors from a given covariance matrix on the spreadsheet. I wish to utilize the power method and solve for each pair by iteration. I have set an initial eigenvector guess as a 3x1 vector X comprised of only 1s. Below is an excerpt from my code:XT = Application.Transpose(X)Y = Application.MMult(A, X)M_0 = Application.Sum(Application.MMult(XT, X), 0)M_1 = Application.Sum(Application.MMult(XT, Y), 0)q = M_1 / M_0largest = Application.Abs(Y(0, 0)) *For i = 1 To matsize If Application.Abs(Y(i, 0)) > largest Then largest = Application.Abs(Y(i, 0)) Next iI am just testing this on a matrix A of dimension 3x3. The value for q will be an approximation for the dominant eigenvalue. The variable largest will be used to standardize the approximating eigenvector Y. After many iterations, q will converge to lamda_1. However, I am getting an error in my code here. The line with the * is causing the problem, as VBA is telling me that I have a problem with subscripts. I am using Option Base 0, and the UBound (Y,1) = 3 and the LBound(Y,1) = 1. In addition, the UBound(Y,2) = 1 and the LBound(Y,2) = 1. Please help me with this error.Thanks in advance!

Eigenvalues, eigenvectors in VBA

Posted: January 14th, 2003, 12:08 pm
by Anthony
If Option Base = 0 then surely your Lbound(y,1) should be 0 not 1.... or am i missing something?

Eigenvalues, eigenvectors in VBA

Posted: January 14th, 2003, 1:31 pm
by Securitize
Anthony,I was wondering about the same thing. I declared the X vector as a dynamic array and redimensioned it to X(2) prior to this excerpt of my code. Does anyone know if VBA always converts to Option Base 1 after performing matrix operations?Thanks in advance!

Eigenvalues, eigenvectors in VBA

Posted: January 14th, 2003, 2:04 pm
by monkeyA
Correct - because the Excel VBA matrix opperations are called from the Application object, they actually take a range as input/output, not an array. VBA 'helpfully' makes these look like a matrix for you. A range cannot have Base 0, hence your problem.solution: write your own matrix functions or rewrite your code for base 1Note:This would have been picked up if you had declared XT correctly!!! (e.g. declaring as an array would have caused an error, declaring as range won't)monkeyA

Eigenvalues, eigenvectors in VBA

Posted: January 14th, 2003, 2:07 pm
by Securitize
monkeyA,Thanks so much for the help. I will just change everything to Base 1. It's actually much easier to work with anyway.Take care,