function X = backsub(A,B)
% X = backsub(A,B)
% A is an nxn upper-triangular matrix, input.
% B is a nx1 vector, input.
% X is an nx1 vector, output.
n = length(B);
det1 = A(n,n);
X = zeros(n,1);
X(n) = B(n)/A(n,n);
for r = n-1:-1:1,
det1 = det1*A(r,r);
if det1 == 0, break, end
X(r) = (B(r) - A(r,r+1:n)*X(r+1:n))/A(r,r);
end