2021-03-13 13:42:56 +01:00
|
|
|
function b = Alg4_back_substitution(U,b)
|
2021-03-13 17:46:26 +01:00
|
|
|
% ADDME Argorithm 4: Back Substitution (Golub, Loan, Alg. 3.1.2)
|
|
|
|
% Returns vetor b with solution to he Ux = b.
|
2021-03-06 14:15:22 +01:00
|
|
|
|
2021-03-13 17:46:26 +01:00
|
|
|
[m, n] = size(U);
|
|
|
|
|
|
|
|
if U ~= triu(U)
|
|
|
|
error('Matrix is not upper triangular!')
|
|
|
|
end
|
|
|
|
|
|
|
|
if m ~= n
|
2021-03-06 14:15:22 +01:00
|
|
|
error('Matrix is not squared!')
|
|
|
|
end
|
|
|
|
|
2021-03-13 17:46:26 +01:00
|
|
|
if length(b) ~= m
|
2021-03-06 14:15:22 +01:00
|
|
|
error('Vector b has wrong length!')
|
|
|
|
end
|
|
|
|
|
2021-03-13 17:46:26 +01:00
|
|
|
% if det(U) < 0.001
|
|
|
|
% error('Matrix is not nonsingular!')
|
|
|
|
% end
|
2021-03-06 14:15:22 +01:00
|
|
|
|
2021-03-13 17:46:26 +01:00
|
|
|
% b(m, :) so that matrices are also accepted
|
2021-03-07 22:56:18 +01:00
|
|
|
|
2021-03-13 17:46:26 +01:00
|
|
|
b(m, :) = b(m, :)/U(m, m);
|
|
|
|
for i = m-1:-1:1
|
|
|
|
b(i, :) = (b(i, :) - U(i, i+1 : m)*b(i+1 : m, :))/U(i, i);
|
2021-03-06 14:15:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|