thx for feedback hagman, much appreciated...
would you have suggestion as to how I may modify/amend the code?
cheers
Main Topics
Browse All TopicsHi
I have below VBA code to determine the eigenvalues/vectors of a symmetric matrix. It seems that for some reason, the code only returns positive eigenvalues (although the absolute #s are correct)
Eg for the following symmetric matrix
1 2
2 1
The code returns the Eigenvalues {3;1}, whereas the 'true' eigenvalues are {-1;3}
The Eigenvectors though seem correct
(0.707; 0.707) for +3
(-0.707; 0.707) for 1
Any idea how I may change the below code such that it gives me the correct sign?
Thanks a lot!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
That would require me do dig deeper into your code.
I wonder a bit about lines 59/60:
I find
newA(i,j)^2 + newA(i,k)^2 = (A(i,j) * Cos_ + A(i,k) * Sin_)^2 + (-A(i,j) * Sin_ + A(i,k) * Cos_)^2
= A(i,j)^2 Cos^2 + 2 Sin Cos A(i,j) A(i,k) + A(i,k)^2 Sin^2 + A(i,j)^2 Sin^2 - 2 Sin Cos A(i,) A(I,k) + A(i,k)^2 Cos^2
= A(i,j)^2 + A(i,k)^2
Hence if Application.SumSq(A) in line 67 does what its name suggests the test in line 68 should practically always evaluate to true, I think
Anyway, something like the following might help when inserted between lines 89 and 90:
Thanks a lot for the follow-up Hageman...is there anything I can do to make you dig deeper into that code ;-)
I inserted your code btwn lines 89/90 (i.e. between the two next statements, pls see below); however, I know get an error msg 'subscript out of range')
Also, I noticed you set m= 2 & i = 3 to p+1....Wouldny p+1 cause the above error? Also, I should have mentioned that the input range maybe of variable size, hence in the smallest case, I may just have a 2x2 matrix....is there a more generic form of your fix which takes this into account?
Thanks a lot for your help - I really vm appreciate it!
Sub EIGEN_JKs()
Dim A() As Variant, Ematrix() As Double
Dim i As Long, j As Long, k As Long, iter As Long, p As Long
Dim den As Double, hold As Double, Sin_ As Double, num As Double
Dim Sin2 As Double, Cos2 As Double, Cos_ As Double, Test As Double
Dim Tan2 As Double, Cot2 As Double, tmp As Double
Const eps As Double = 1E-16
On Error GoTo EndProc
A = Range("L8:P12")
p = UBound(A, 1)
ReDim Ematrix(1 To p, 1 To p + 1)
For iter = 1 To 15
'Orthogonalize pairs of columns in upper off diag
For j = 1 To p - 1
For k = j + 1 To p
den = 0#
num = 0#
'Perform single plane rotation
For i = 1 To p
num = num + 2 * A(i, j) * A(i, k)
den = den + (A(i, j) + A(i, k)) * _
(A(i, j) - A(i, k))
Next i
'Skip rotation if aij is zero and correct ordering
If Abs(num) < eps And den >= 0 Then Exit For
'Perform Rotation
If Abs(num) <= Abs(den) Then
Tan2 = Abs(num) / Abs(den)
Cos2 = 1 / Sqr(1 + Tan2 * Tan2)
Sin2 = Tan2 * Cos2
Else
Cot2 = Abs(den) / Abs(num)
Sin2 = 1 / Sqr(1 + Cot2 * Cot2)
Cos2 = Cot2 * Sin2
End If
Cos_ = Sqr((1 + Cos2) / 2)
Sin_ = Sin2 / (2 * Cos_)
If den < 0 Then
tmp = Cos_
Cos_ = Sin_
Sin_ = tmp
End If
Sin_ = Sgn(num) * Sin_
'Rotate
For i = 1 To p
tmp = A(i, j)
A(i, j) = tmp * Cos_ + A(i, k) * Sin_
A(i, k) = -tmp * Sin_ + A(i, k) * Cos_
Next i
Next k
Next j
'Test for convergence
Test = Application.SumSq(A)
If Abs(Test - hold) < eps And iter > 5 Then Exit For
hold = Test
Next iter
If iter = 16 Then MsgBox "JK Iteration has not converged."
'Compute eigenvalues/eigenvectors
For j = 1 To p
'Compute eigenvalues
For k = 1 To p
Ematrix(j, 1) = Ematrix(j, 1) + A(k, j) ^ 2
Next k
Ematrix(j, 1) = Sqr(Ematrix(j, 1))
'Normalize eigenvectors
For i = 1 To p
If Ematrix(j, 1) <= 0 Then
Ematrix(i, j + 1) = 0
Else
Ematrix(i, j + 1) = A(i, j) / Ematrix(j, 1)
End If
Next i
If Ematrix(j, 1) > 0 Then
' Find biggest component
M = 2
For i = 3 To p + 1
If Abs(A(i, j)) > Abs(A(M, j)) Then
M = i
End If
Next i
' Calculate m'th component of M and jth eigenvector
tmp = 0
For i = 1 To p
tmp = tmp + M(M, i) * A(i, j)
Next i
' recalculate eigenvalue as quotient output/input
Ematrix(j, 1) = tmp / A(M, j)
End If
Next j
For i = 1 To p + 1 '
For j = 1 To p
Range("Z12").Offset(i, j) = Ematrix(j, i)
Next j
Next i
'EIGEN_JK = Ematrix
Exit Sub
EndProc:
MsgBox prompt:="Error in function EIGEN_JK!" & vbCr & vbCr & _
"Error: " & Err.Description & ".", Buttons:=48, _
Title:="Run time error!"
End Sub
Business Accounts
Answer for Membership
by: thehagmanPosted on 2009-10-19 at 12:53:13ID: 25608370
Simply feed your eigenvectors into the matrix and see if the result is more like lambda*v or -lambda*v.
E.g. if the i'th component of v has maximal absolute value, check whether the i'th component of Av has
the same or opposite sign..