function qrdemo(A) % Visualization of the Householder QR transformation (no pivoting) % adapted by M.E. from Cleve Moler's beautiful "lugui.m" % from Numerical Computing with MATLAB % (c) 2004 Cleve Moler. if nargin<1, A = randn(8,5); end [m,n] = size(A); shg clf dx = 100; dy = 30; warns = warning('off','MATLAB:divideByZero'); set(gcf,'double','on','name','QR Demo', ... 'menu','none','numbertitle','off','color','white', ... 'pos',[480-(dx/2)*min(9,n) 320 (n+1)*dx (m+3)*dy], ... 'windowbuttonupfcn','set(gcf,''tag'',''pivot'')') stop = uicontrol('style','toggle','string','X','fontweight','bold', ... 'back','w','pos',[(n+1)*dx-25 (m+3)*dy-25 25 25]); axes('pos',[0 0 1 1]) axis off Qcolor = [.90 0 0]; Rcolor = [0 0 .90]; Acolor = [0 0 0]; PartialPivotColor = [1 0 1]; PivotColor = [1 0 0]; TempColor = [1 1 1]; paws = 0.02; % Each element has its own handle for j = 1:n for i = 1:m t(i,j) = text('units','pixels','string',spf(A(i,j)), ... 'fontname','courier','fontweight','bold','fontsize',14, ... 'horiz','right','color',Acolor, ... 'pos',[20+j*dx 20+(m+2-i)*dy],'userdata',[i j], ... 'buttondownfcn','set(gcf,''userdata'',get(gco,''userdata''))'); end end ttl = text('units','pixels','string','Matrix A', ... 'fontname','courier','fontweight','bold','fontsize',14, ... 'horiz','left','color',Acolor, ... 'pos',[(20+1*dx) 25],'userdata',[1 1], ... 'buttondownfcn','set(gcf,''userdata'',get(gco,''userdata''))'); %'pos',[(20+n*dx)/2+70 25],'userdata',[1 1], ... maxcol = n; if m<=n, maxcol=m-1; end for p=1:maxcol a = A(p:m,p); v = a - norm(a)*[1;zeros(m-p,1)]; H = eye(m-p+1)-2*v*v'/(v'*v); Q = [eye(p-1) zeros(p-1,m-p+1); zeros(m-p+1,p-1) H]; Aold = A; A = Q*A; pause set(ttl,'string',sprintf('Householder Transformation %d',p)) for k=p:n, for j=p:m set(t(j,k),'color',TempColor) drawnow pause(paws) if j==p, set(t(j,k),'string',spf(A(j,k)),'color',Rcolor) elseif k==p, set(t(j,k),'string',spf(A(j,k)),'color',Qcolor) else, set(t(j,k),'string',spf(A(j,k)),'color',Acolor) end drawnow pause(paws) end, end pause(10*paws); end pause if maxcol= 1.e4) f = '%10.1e'; else f = '%10.4f'; end s = sprintf(f,aij);