%% Datta, page 595-596, Ex 4 + test data % Compute the rank of a matrix % Module 2, Lecture 2, Demo no. 4, SVD disp(' ----------- Singular Vale Decomosition: Demo no 4: -------------') disp(' ----------------------- numerical rank --------------------------') disp('') waitt('Create the Kahan marix') c=0.2; s=0.6; n=100; A=Kahan(s,c,n); disp('Compute the singular value decomposition of A') tic,[U,S,V]=svd(A); disp(['It takes ' num2str(toc) ' sec.']) figure(1),clf,plot(diag(S)) figure(1),clf,semilogy(diag(S),'o-') title('figure(1),clf,semilogy(diag(S))') wait disp([' Check rank(A):' int2str(rank(A))]) disp(' Check sd=diag(S); sd(61:65)') sd=diag(S); sd(61:65),wait disp('Check low rank approximations:') % %% Test low rank approximations of the matrix for k=[20,50,61,62,65,80] AK=U(:,1:k)*S(1:k,1:k)*V(:,1:k)'; nrm=norm(A-AK); disp(['k= ' int2str(k) ', norm(A-AK) ' num2str(nrm) ', sigma(k+1) ' num2str(S(k+1,k+1))]) end disp('...end demo.')