function x=BackSub(U,b)
% This function computes the vector x by backward substitution.
% We solve Ux=b, where U is an nxn, non-singular upper triangular matrix 
% and b is a known vector of the length n, we find the vector x.

%% Compute x by backward substitution.
s=size(U);
n=s(1);
x=zeros(n,1); 
% U(i,i)*x(i) = b(i)-sum_{j=i+1}^{n}
for k=1:n
    i=n-k+1; % Compute row => i=n,n-1,n-2,...,3,2,1
    x(i)=b(i);
    for j=(i+1):n
        x(i)=x(i)-U(i,j)*x(j);
    end
    x(i)=x(i)/U(i,i);
end
end
