WUT_Computer_Science/ENUME/projectC/backSubstitution.m

17 lines
541 B
Mathematica
Raw Normal View History

2022-01-07 19:37:20 +01:00
% solves system with triangular matrix
function result = backSubstitution(eqsys)
for col = size(eqsys, 1):-1:1
% normalize diagonal coefficients to 1
eqsys(col, :) = eqsys(col, :) / eqsys(col, col);
% eliminate factor from other rows
for row = (col - 1):-1:1
reductor = eqsys(row, col) / eqsys(col, col);
eqsys(row, :) = eqsys(row, :) - eqsys(col, :) * reductor;
end
end
% rightmost column is now the result
result = eqsys(:, size(eqsys, 2));
end