# Jacobi iterative method

I'm using Jacobi iterative method for finding eigenvalue and eigenvector for hermitian or symmetric matrix. Eigenvectors corresponding to eigenvalues are not exact. The third eigenvector is totally off. That I've implemented and read about Jacobi. What are the possible reasons for that?

This is my function file in scilab:

function [eig,eigv]=Jacobimethod(A, I)
r=0
while [1]

B=A-diag(diag(A))
[m, index]=max(abs(B))
if m==0
break
end

p=min(index)
q=max(index)
R=eye(n,n)
if A(p,p)==A(q,q)
theta=%pi/4
else
theta=(atan(2*A(p,q)/(A(p,p)-A(q,q))))/2
end
c=cos(theta)
s=sin(theta)
R(p,p)=c
R(q,q)=c
R(p,q)=-s
R(q,p)=s
Anew=clean((R')*A*R,10^-4)
//disp(Anew)
eig=diag(Anew)
A=Anew
X=I*R
eigv=X
I=X
r=r+1

end
disp(r,"No of iterations")
endfunction


Probably you accumulated rotations wrong. At each step, you have a working matrix $$D$$ that starts from $$D=A$$ and should converge to a diagonal matrix, and a matrix $$Q$$ that accumulates all rotations performed on $$D$$. Print after each step $$\|QDQ^T - A\|$$, and see where it stops being small.
• Anyway, if you can verify that you have found a diagonal (up to machine precision) matrix $D$ and an orthogonal matrix $Q$ such that $\|QDQ^T-A\| / \|A\|$ is of the order of machine precision, then you have successfully solved your problem; there is no possibility that the eigenvectors are wrong, because that is the definition of a (backward stable) solution. Maybe your $D$ is not "diagonal enough" for you to stop? – Federico Poloni Sep 27 '20 at 20:41