%
% To solve an upper triangular system Ax = b by back-substitution
% Version where the loop index goes down
%
%

function x = backsubst2(A, b)
    n = size(A,1);
    x = zeros(n,1); 
    
    %Work from bottom up to fill in x
    x(n) = b(n)/A(n,n);
    for i = n - 1:-1:1
        x(i) = (b(i)-A(i,i+1:end)*x(i+1:end))/A(i,i);    
    end
end