It looks like you're not reducing the size of the blocks on each iteration. Everything seems to be a function of the same m
and n
(which you didn't define in your code). See the line on the Wikipedia page where they define A′ and use it to build Q2 (just the lower two thirds). Below is some code of mine adapted to perform QR-decomposition of 3-by-3 matrices that might help. Note particularly that the second block only works on A(:,2)
and q(2:3,:)
:
function [q,r]=qr3(A)
u = A(:,1);
u(1) = u(1)-(1-2*(u(1)<0))*norm(u); % Flip < to > to match sign convention of qr
u = u/norm(u);
u(~isfinite(u)) = sqrt(3)/3;
q = -2*(u*u');
q([1 5 9]) = q([1 5 9])+1;
u = q(2:3,:)*A(:,2);
u(1) = u(1)-(1-2*(u(1)<0))*norm(u); % Flip < to > to match sign convention of qr
u = u/norm(u);
u(~isfinite(u)) = sqrt(2)/2;
q(:,2:3) = q(:,2:3)*[1-2*u(1)^2 -2*u(1)*u(2);
-2*u(1)*u(2) 1-2*u(2)^2];
r = triu(q'*A);
The above code and the method detailed on Wikipedia use a different sign convention from Matlab's qr
function. See my comments in the code for how to flip the sign.