function showsvd(A); % SHOWSVD showsvd(A) % Picture a 2x2 matrix A by singular values and corresponding vectors. % Try: >> showsvd(randn(2)) % >> showsvd([2 1; -1 3]) % >> showsvd([4 -2; -2 1]) if ~min((size(A)==[2 2])), error('matrix A is not 2 by 2'), end [U,S,V] = svd(A); % input space picture clf, theta = linspace(0,2*pi,100); x = cos(theta); y = sin(theta); subplot(1,2,1), plot(x,y,'--'), hold on % draw circle for j=1:2 plot([0 V(1,j)],[0 V(2,j)],'r') % draw input vectors V ("right singular vectors") text(.5*V(1,j),.5*V(2,j),['v_' num2str(j)]) end % text description text(1.85,.25,'A'), text(1.7,0,'--->') text(-.7,2.4,[num2str(A(1,1),'%5.4f') ' ' num2str(A(1,2),'%5.4f')],'Color','r') text(-1.0,2.3,'A = ','Color','r') text(-.7,2.2,[num2str(A(2,1),'%5.4f') ' ' num2str(A(2,2),'%5.4f')],'Color','r') text(-1.0,1.75,['\sigma_1 = ' num2str(S(1,1),'%6.4f') ', \sigma_2 = ' num2str(S(2,2),'%6.4f')]) text(-1.5,-1.5,'A v_j = \sigma_j u_j <==> A = U \Sigma V^*','Color','r') axis equal, axis([-2 2.5 -2 3]), hold off % output space picture out = A*[x; y]; subplot(1,2,2), plot(out(1,:),out(2,:),'--'), hold on % draw ellipse for j=1:2 sig = S(j,j); ex = sig*U(1,j); ey = sig*U(2,j); plot([0 ex],[0 ey],'r') % draw output vectors U ("left singular vectors") text(.5*ex,.5*ey,['\sigma_' num2str(j) ' u_' num2str(j)]) end mox = max(abs(out(1,:))); moy = max(abs(out(2,:))); axis equal, axis([-1.4*mox 1.4*mox -1.1*moy 1.1*moy]), hold off