Added Householder algorithm and introduced Householder QR (got R, gotta find Q)
This commit is contained in:
parent
121ada24f9
commit
7e55d72cfe
21
Direct Methods for Solving Linear Systems/Alg11.m
Normal file
21
Direct Methods for Solving Linear Systems/Alg11.m
Normal file
@ -0,0 +1,21 @@
|
||||
function [Q R] = Alg11(A)
|
||||
% Algorithm 11: QR factorization via Housholder algorithm.
|
||||
|
||||
[m, n] = size(A);
|
||||
|
||||
if ~ (m>=n)
|
||||
error("m has to be greater or equal n!")
|
||||
end
|
||||
|
||||
for j = 1:n
|
||||
[v, beta] = householder(A(j:m, j))
|
||||
A(j:m, j:n) = (eye(m-j+1)-beta*(v*v.'))*A(j:m, j:n)
|
||||
if j < m
|
||||
A(j+1:m, j) = v(2:m-j+1)
|
||||
end
|
||||
end
|
||||
|
||||
R = triu(A)
|
||||
|
||||
end
|
||||
|
6
Direct Methods for Solving Linear Systems/Alg13.m
Normal file
6
Direct Methods for Solving Linear Systems/Alg13.m
Normal file
@ -0,0 +1,6 @@
|
||||
function [Q R] = Alg13(A)
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
27
Direct Methods for Solving Linear Systems/householder.m
Normal file
27
Direct Methods for Solving Linear Systems/householder.m
Normal file
@ -0,0 +1,27 @@
|
||||
function [v,beta] = householder(x)
|
||||
% householder takes vertical vector x
|
||||
|
||||
if ~iscolumn(x)
|
||||
x = x.';
|
||||
end
|
||||
|
||||
n = length(x);
|
||||
sig = x(2:n).' * x(2:n);
|
||||
v = [1;
|
||||
x(2:n)];
|
||||
|
||||
if isempty(sig) || (sig == 0)
|
||||
beta = 0;
|
||||
else
|
||||
mu = sqrt(x(1)^2+sig);
|
||||
if x(1) <= 0
|
||||
v(1) = x(1) - mu;
|
||||
else
|
||||
v(1) = -sig/(x(1) + mu);
|
||||
end
|
||||
beta = 2*v(1)^2/(sig + v(1)^2);
|
||||
v = v/v(1);
|
||||
end
|
||||
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user