AAE-NA-Labs/Direct Methods for Solving Linear Systems/back_substitution.m

22 lines
375 B
Mathematica
Raw Normal View History

% Argorithm 4: Back Substitution (Alg. 3.1.2)
2021-03-06 18:17:36 +01:00
function b = back_substitution(U,b)
[n, m] = size(U);
if n ~= m
error('Matrix is not squared!')
end
if length(b) ~= n
error('Vector b has wrong length!')
end
if det(U) == 0
error('Matrix is not nonsingular!')
end
2021-03-06 18:17:36 +01:00
b(n) = b(n)/U(n, n);
for i = n-1:-1:1
2021-03-06 18:17:36 +01:00
b(i) = (b(i) - U(i, i+1 : n)*b(i+1 : n))/U(i, i);
end
end