AAE-NA-Labs/01_Direct-Methods-for-Solving-Linear-Systems/Report/algorithms/Alg13.m

19 lines
436 B
Mathematica
Raw Normal View History

2023-03-11 20:08:05 +01:00
function [Q, R] = Alg13(A)
% Algorithm 13: The QR algorithm by the Gram-Schmidt ortogonalization
% [Q, R] = Alg13(A)
% Based on [GVL96]: Section 5.2.8
[m,n] = size(A);
if ~ (m >= n)
error("m has to be greater or equal n!")
end
R = zeros(n, n);
Q = zeros(m, n);
for k = 1:n
R(k, k) = norm(A(:, k));
Q(:, k) = A(:, k)/R(k, k);
R(k, k+1:n) = Q(:, k)'*A(:, k+1:n);
A(:, k+1:n) = A(:, k+1:n) - Q(:, k)*R(k, k+1:n);
end