function x = bsub2(A,b)
% Finds the solution vector of a system of linear equations
% where the coefficient matrix  is upper triangular 
% A x = b
% backward substitution
% Input Arguments
% A the coefficient matrix
% b the right hand side vector 
% Output Arguments
% x the solution vector

[mA,nA] = size(A) ;
[mb,nb] = size(b) ;

if mA == nA 
    if (nA==mb) && (nb==1)
        % dimensions agree
        x = b ;
        for i = n:-1:1
 %             for j = i+1:n
 %                  x(i) = x(i) - A(i,j)*x(j) ;
 %             end
 %              x(i) = x(i)/A(i,i) ;
 % use A(i,i+1:n) and x(i+1:n) instead of the for loop
            x(i) = (x(i) - A(i,i+1:n)*x(i+1:n))/A(i,i) ;
        end

    else
        error('wrong dimensions of b') ;
    end    
else
    error('A is not square');
end    
    

end

