  function [sigma,u,v] = SigUVn(A,nRepeat)
% A is a nonsingular nxn matrix.
% sigma, u, and v are estimates of A's smallest singular value
% and the associated left and right singular vectors.
% nRepeat steps of the bootstrapping procedure are applied.
[L,U,p] = lu(A,'vector');
[n,n] = size(A);
z = randn(n,1); z = z/norm(z);
for j=1:nRepeat
    % Solve Aw= z.
    w = U\(L\z(p));
    v = w/norm(w);
    z = v;
    % Solve A'w = z.
    w = L'\(U'\z);
    w(p) = w;
    sigma = 1/norm(w);
    u = w/norm(w);
    z = u;
end

% Grading:
% 1 off for doing LU factorization in the loop
% 1 off for doing LU factorization on A and A’ separately
% 2 off for missing z = w

% I didn't take off points for handling of the the permutation matrix unless it made the 
% solution incorrect. Sometimes they misapplied the permutation matrix in the A'w=z step,
% for which I took off 1 point. Many groups explicitly multiplied by P.
