Page 234 - MATLAB an introduction with applications
P. 234
Numerical Methods ——— 219
function [eigs,A] = eig_QR(A,kmax)
%Find eigenvalues by using QR factorization
if nargin<2, kmax = 200; end
for k = 1:kmax
[Q,R] = qr(A); %A = Q R; R=Q’ A=Q^–1 A
*
*
*
A = R Q; %A = Q^–1 A Q
* *
*
end
eigs = diag(A);
function [Q,R] = qr_my(A)
%QR factorization
N = size(A,1); R=A; Q = eye(N);
for k =1:N – 1
H = Householder(R(:,k), k);
R = H R;
*
Q = Q H;
*
end
function H = Householder(x,k)
%Householder transform to zero out tail part starting from k+1
H = eye(N) – 2 w w’; %Householder matrix
* *
N = length(x);
w = zeros(N,1);
w(k) = (x(k) + g)/c; w(k + 1:N) = x(k + 1:N)/c;
tmp = sum(x (k+1: N). ^2);
c =sqrt((x(k) + g)^2 + tmp);
g = sqrt(x(k)^2 + tmp);
Example E4.5: Using Choleski’s method of solution, solve the following linear equations.
x + x + x = 7
1
2
3
3x + 3x + 4x = 23
2
3
1
2x + x + x = 10
3
1
2
Solution:
MATLAB Solution [Using built-in function]:
Choleski’s method:
>> A = [1 1 1;3 3 4;2 1 1];
>> B = [7;23;10];
>> [L,U] = lu(A)