Page 254 - MATLAB an introduction with applications
P. 254
Numerical Methods ——— 239
It is possible to construct the sequence {R } so that
j
lim = D = diag( ,λ ,.....,λ )
λ
j→ D j 1 2 n
In practice, we will stop when the off-diagonal elements are close to zero. Then we will have D ≈ D.
m
The complete program is shown below:
A = [11 2 8;2 2 –10;9 –10 5];
%Output – V is the nxn matrix of eigenvectors
% – D is the diagonal nxn matrix of eigenvalues
D = A;
[n,n] = size(A);
V = eye(n);
% Calculate row p and column q of the off-diagonal element
% of greatest magnitude in A
[m1 p] = max(abs(D–diag(diag(D))));
[m2 q] = max(m1);
p = p(q);
i = 1;
while(i<10)
% Zero out Dpq and Dqp
t = D(p,q)/(D(q,q)–D(p,p));
c = 1/sqrt(t^2+1);
s = c*t;
R = [c s;–s c];
D([p q],:) = R’*D([p q],:);
D(:,[p q]) = D(:,[p q])*R;
V(:,[p q]) = V(:,[p q])*R;
[m1 p] = max(abs(D–diag(diag(D))));
[m2 q] = max(m1);
p = p(q);
i = i+1;
end
D = diag(diag(D))
fprintf(‘final eigenvalues are %f\t%f\t%f\n’,D(1,1),D(2,2),D(3,3));
The output of the program is as follows:
D =
18.4278 0 0
0 –9.2213 0
0 0 8.7934
final eigenvalues are 18.427839 –9.221279 8.793440