-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAlgorithm2.m
More file actions
35 lines (25 loc) · 770 Bytes
/
Copy pathAlgorithm2.m
File metadata and controls
35 lines (25 loc) · 770 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function [ Q, R ] = Algorithm2( A, iter )
% Implemented in NREL
% QR decomposition using Ruhe notation mod Gram-Schmidt
% Sample use: A = rand(100); Algorithm2(A);
% (c) Julien Langou (CU Denver), K. Swirydowicz (NREL), S. J. Thomas (NREL)
n=size(A, 1);
m=size(A, 2);
Q = zeros(n,m);
R = eye(m,m);
R(1,1) = norm(A(:,1));
Q(:,1) = A(:,1)/norm(A(:,1));
for j=2: m
a = A(:, j);
R(1:j-1, j) = zeros(j-1,1);
for i=1:j-1
s = Q(:, i)'*a;
a = a - s*Q(:,i);
R(i, j) = R(i, j) +s;
end
R( j, j) = norm(a);
Q(:, j) = a/R(j,j);
end
fprintf('||Q(:,1:%d)^TQ(:, 1:%d) - I|| = %16.16e|| \n', m, m, norm(Q(:, 1:m )'*Q(:, 1:m)-eye(m,m)));
fprintf('||A - QR||/||A|| = %16.16e \n\n', norm(A-Q*R, 'fro')/norm(A, 'fro'));
end