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

19 lines
388 B
Mathematica
Raw Normal View History

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