

clear all hidden; close all hidden; clc;

A=round(rand(5,3)*10-5);

A=[-1 13; 2 4; -2 -7];
A=[1 17; 8 -9; -4 -16];

A=[2 3; 4 -9; -4 -12];


Aold=A;
m=size(A,1);
n=size(A,2); 
Q=eye(m,m);

for k=1:n
  k
  alpha=-sign(A(k,k))*norm(A(k:m,k),2)
  vv=A(k:m,k)-alpha*eye(m-k+1,1)
  normvv=sqrt(2*alpha*(alpha-A(k,k)))
  v=vv/normvv;
  Qnew=eye(m,m);
  Qnew(k:m,k:m)=eye(m-k+1,m-k+1)-2*v*v'
  A=Qnew*A
  Q=Q*Qnew;  
end
R=A

norm(Q*R-Aold)


