function x = u_solve(U,y)
% x=u_solve(U,y)
% U is an upper-triangular matrix
% y is the right hand side
% x is the solution of U*x=y

% your name and the date

% set n for convenience and simplicity
n=length(y);
% initialize y to zero and make sure it is a column vector
x=zeros(n,1); 

% last row is the easy one
Irow=n;
x(Irow)=y(Irow)/U(Irow,Irow);

% the -1 in the middle means it is going up from the bottom
for Irow=n-1:-1:1
  rhs = y(Irow);
  % the following loop could also be written as a single
  % vector statement.
  for Jcol=Irow+1:n
    rhs = rhs - U(Irow,Jcol)*x(Jcol);
  end
  x(Irow) = rhs/U(Irow,Irow);
end
