function ShowUnderDetermined()
% Shows how to us the QR factorization to solve a linear system in which 
% there are more columns than rows.
m = 3;
n = 6;
A = rand(m,n);
b = rand(m,1);
x = UnderDeterminedSolver(A,b);
err = norm(A*x-b)

  function x = UnderDeterminedSolver(A,b)
% x solves Ax = b where A has more columns than rows.
% Idea: A' = QR means Ax = R'Q'x = b. Set y = Q'x and notice
% that in the equation R'y = b, y(m+1:n) can take on any value since
% R(m+1:n,:) = 0. Thus, we set y(m+1:n) = 0 and so R(1:m,1:m)y(1:m) = b.
[m,n] = size(A);
[Q1,R1] = qr(A',0);
x = Q1*(R1'\b);