function [x]=backsub2(U,rhs)
% performs back substitution 
% given an upper triangular matrix U and a rhs vector
% outputs the solution of U*x=rhs
% (this version uses row*column vector multiplication to eliminate one loop)

N=length(rhs);

% initialize x as a column vector of the correct length
x=zeros(N,1);

% last row gives last component of x
x(N)=rhs(N)/U(N,N);

for k=N-1:-1:1   
    x(k)=(rhs(k) - U(k,k+1:N)*x(k+1:N))/U(k,k); 
end

end
    