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

18 lines
329 B
Mathematica
Raw Normal View History

2021-03-06 18:17:36 +01:00
% Algorithm 3: Forward Substitution (Alg. 3.1.1)
2021-03-13 13:42:56 +01:00
function b = Alg3_forward_substitution(L, b)
2021-03-06 18:17:36 +01:00
[n, m] = size(L);
if n ~= m
error('Matrix is not squared!')
end
if length(b) ~= n
error('Vector b has wrong length!')
end
b(1, :) = b(1, :)/L(1,1);
2021-03-06 18:17:36 +01:00
for i = 2:n
b(i, :) = (b(i, :) - L(i, 1:i-1)*b(1:i-1, :))/L(i, i);
2021-03-06 18:17:36 +01:00
end