Jacobi Rotation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

9/19/11 10:37 PM C:\Documents and Settings\PRAVEEN\My Docu...\Jacobi_rotation.

m 1 of 2
%-----------------------------------------------------------------------% This code performs Similarity Transformation corresponding to Jacobi
% Rotation to compute eigen values and eigen vectors of a symmetric matrix [A].
% [A*] = [R'][A][R]
% [A*] is a diagonal matrix with eigen values as diagonal elements
% Final transformation matrix is stored in [P] and is the accumulation of
% individual rotations [Ri].
% AML702: Advance Computational Method, IIT Delhi
% Praveen Kumar Srivastava, 2011AMZ8095
% Nomesh Kumar 2011AMZ8097
% Suresh Kumar 2011CEZ8273
%-----------------------------------------------------------------------tol = eps; n = size(A,1); max_rotation = 100;
% Initialize rotation matrix [P]
P = eye(n);
% Begin Rotation
for k = 1:max_rotation
% Find maximum off-diagonal element in [A] and its index p & q
Amax = 0.0;
for i = 1:n-1
for j = i+1:n
if abs(A(i,j))>= Amax
Amax = abs(A(i,j));
p = i;
q = j;
end
end
end
% If Amax is < eps, then print results
if Amax < tol
fprintf( '\nEigen values (e_val) and Eigen vectors (e_vect) of [A] at the end of k_th rotation:\n')
e_val = diag(diag(A))
e_vect = P
k
return
end

9/19/11 10:37 PM C:\Documents and Settings\PRAVEEN\My Docu...\Jacobi_rotation.m 2 of 2


% find s2 (sin2t), c2 (cos2t), c (cost), s (sint), t2 (tan2t), t (tant)
if abs(A(p,p)-A(q,q))< eps
s2 = 1; s = 1/sqrt(2); c = s;
else
t2 = 2*A(p,q)/(A(p,p)-A(q,q));
c2 = 1/sqrt(1+t2*t2); s2 = t2*c2;
c = sqrt((1+c2)/2); s = s2/(2*c);
end
% Modify rotation matrix [P]
P(:,[p,q]) = P(:,[p,q])* [c -s; s c];
% Modify A(p,p), A(q,q), A(p,q) & A(q,p) elements of [A]
temp = A(p,p);
A(p,p) = A(p,p)*c*c+A(q,q)*s*s+2*A(p,q)*s*c;
A(q,q) = temp*s*s+A(q,q)*c*c-2*A(p,q)*s*c;
A(p,q) = 0;
A(q,p) = 0;
% Modify A(p,nn) and A(q,nn), (where nn ~=p and nn~=q) elements of [A]
for nn = 1:n
if nn~=p & nn~=q
temp_pn = A(p,nn);
A(p,nn) = A(p,nn)*c + A(q,nn)*s;
A(q,nn) = -temp_pn*s + A(q,nn)*c;
A(nn,p) = A(p,nn);
A(nn,q) = A(q,nn);
end
end
end

You might also like