% plots absolute and relative errors in singular values

clc;
clear all;
close all;

% n fixed and m varied

% matrix dimensions
n=50:50:300;
m=100:50:300;

% figure index
% fig=[1:2*length(m);2*length(m)+1:4*length(m)];
f=0;

for i=1:length(n)
    
    for j=1:length(m)
        
        %         Singular value generation
        a=randn(min(m(j),n(i)),1);
        a=abs(a);
        a=sort(a,'descend');
        
        % Matrix generation
        [A,U,S,V]=matrixgen(n(i),m(j),a);
        
        % SVD decompositions
        
        % built in
        [u1,d1,v1]=svd(A);
        [x,y]=size(d1);
        
        % Comb. Built in
        [u2,d2,v2]=SVDBiBuilt(A);
        
        % Comb. Square
        [u3,d3,v3]=SVDComb(A);
        
        %                 Indirect Method
        [u4,d4,v4]=SVDecom(A);
        
        % Direct Method
        [u5,d5,v5]=SVDIndirect(A);
        
        % Francis
        [d6]=SVD(A);
        
        f=f+1;
        h=figure(f);
        hold on
        loglog(a,abs(a-diag(d1)),'bv-','MarkerFaceColor','b','LineWidth',2)
        loglog(a,abs(a-diag(d2)),'g^-','MarkerFaceColor','g','LineWidth',2);
        loglog(a,abs(a-diag(d3)),'ks-','MarkerFaceColor','k','LineWidth',2);
        loglog(a,abs(a-diag(d4)),'rd-','MarkerFaceColor','r','LineWidth',2);
        loglog(a,abs(a-diag(d5)),'co-','MarkerFaceColor','c','LineWidth',2);
        loglog(a,abs(a-(d6)),'m*-','MarkerFaceColor','m','LineWidth',2);
        grid
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of absolute error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] );
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');
        
        f=f+1;
        h=figure(f);
        hold on
        scatter (a,abs(a-diag(d1)),'filled')
        scatter (a,abs(a-diag(d2)),'g','filled')
        scatter (a,abs(a-diag(d3)),'k','filled')
        scatter (a,abs(a-diag(d4)),'r','filled')
        scatter (a,abs(a-diag(d5)),'c','filled')
        scatter (a,abs(a-(d6)),'m','filled')
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of absolute error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] )
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');
        
        f=f+1;
        h=figure(f);
        hold on
        loglog(a,abs(a-diag(d1))./a,'bv-','MarkerFaceColor','b','LineWidth',2)
        loglog(a,abs(a-diag(d2))./a,'g^-','MarkerFaceColor','g','LineWidth',2);
        loglog(a,abs(a-diag(d3))./a,'ks-','MarkerFaceColor','k','LineWidth',2);
        loglog(a,abs(a-diag(d4))./a,'rd-','MarkerFaceColor','r','LineWidth',2);
        loglog(a,abs(a-diag(d5))./a,'co-','MarkerFaceColor','c','LineWidth',2);
        loglog(a,abs(a-(d6))./a,'m*-','MarkerFaceColor','m','LineWidth',2);
        grid
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of relative error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] );
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');
        
        f=f+1;
        h=figure(f);
        hold on
        scatter (a,abs(a-diag(d1))./a,'filled')
        scatter (a,abs(a-diag(d2))./a,'g','filled')
        scatter (a,abs(a-diag(d3))./a,'k','filled')
        scatter (a,abs(a-diag(d4))./a,'r','filled')
        scatter (a,abs(a-diag(d5))./a,'c','filled')
        scatter (a,abs(a-(d6))./a,'m','filled')
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of relative error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] )
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');
    end
    
end

% m fixed and n varied
%
% matrix dimensions
m1=n;
n1=m;
%

for i=1:length(m1)

    for j=1:length(n1)

        %         Singular value generation
        a1=randn(min(m1(i),n1(j)),1);
        a1=abs(a1);
        a1=sort(a1,'descend');

        % Matrix generation
        [A1,U1,S1,V1]=matrixgen(n1(j),m1(i),a1);

        % SVD decompositions

        % built in
        [u7,d7,v7]=svd(A1);
        [x1,y1]=size(d7);

        % SVDBiBuilt
        [u8,d8,v8]=SVDBiBuilt(A1);

        % SVDComb
        [u9,d9,v9]=SVDComb(A1);

        % SVDecom
        [u10,d10,v10]=SVDecom(A1);

        % SVDIndirect
        [u11,d11,v11]=SVDIndirect(A1);

%         SVDFrancis
        [d12]=mp_mysvd(A1);

        f=f+1;
        h=figure(f);
        hold on
        loglog(a,abs(a-diag(d1)),'bv-','MarkerFaceColor','b','LineWidth',2)
        loglog(a,abs(a-diag(d2)),'g^-','MarkerFaceColor','g','LineWidth',2);
        loglog(a,abs(a-diag(d3)),'ks-','MarkerFaceColor','k','LineWidth',2);
        loglog(a,abs(a-diag(d4)),'rd-','MarkerFaceColor','r','LineWidth',2);
        loglog(a,abs(a-diag(d5)),'co-','MarkerFaceColor','c','LineWidth',2);
        loglog(a,abs(a-(d6)),'m*-','MarkerFaceColor','m','LineWidth',2);
        grid
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of absolute error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] );
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');

        f=f+1;
        h=figure(f);
        hold on
        scatter (a,abs(a-diag(d1)),'filled')
        scatter (a,abs(a-diag(d2)),'g','filled')
        scatter (a,abs(a-diag(d3)),'k','filled')
        scatter (a,abs(a-diag(d4)),'r','filled')
        scatter (a,abs(a-diag(d5)),'c','filled')
        scatter (a,abs(a-(d6)),'m','filled')
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of absolute error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] )
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');

        f=f+1;
        h=figure(f);
        hold on
        loglog(a,abs(a-diag(d1))./a,'bv-','MarkerFaceColor','b','LineWidth',2)
        loglog(a,abs(a-diag(d2))./a,'g^-','MarkerFaceColor','g','LineWidth',2);
        loglog(a,abs(a-diag(d3))./a,'ks-','MarkerFaceColor','k','LineWidth',2);
        loglog(a,abs(a-diag(d4))./a,'rd-','MarkerFaceColor','r','LineWidth',2);
        loglog(a,abs(a-diag(d5))./a,'co-','MarkerFaceColor','c','LineWidth',2);
        loglog(a,abs(a-(d6))./a,'m*-','MarkerFaceColor','m','LineWidth',2);
        grid
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of relative error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] );
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');

        f=f+1;
        h=figure(f);
        hold on
        scatter (a,abs(a-diag(d1))./a,'filled')
        scatter (a,abs(a-diag(d2))./a,'g','filled')
        scatter (a,abs(a-diag(d3))./a,'k','filled')
        scatter (a,abs(a-diag(d4))./a,'r','filled')
        scatter (a,abs(a-diag(d5))./a,'c','filled')
        scatter (a,abs(a-(d6))./a,'m','filled')
        axis tight
        xlabel('\sigma_i');
        ylabel('e_i');
        title(['Comparison of relative error in sigular values for ',num2str(n(i)),' x ',num2str(m(j)),' Dense rectangular matrix'] )
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');

    end

end
%
