% plots absolute and relative errors in singular values
clc;
clear all;
close all;

% n fixed and m varied
% all inf norms
% matrix dimensions
n=50:50:300;
f=0;

for i=1:length(n)
    
    % Matrix generation
    a=randn(n(i),1);
    a=abs(a);
    a=sort(a,'descend');
    [A,U,S,V]=matrixgen(n(i),n(i),a);
    A=A+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
    [u4,d4,v4]=SVDecom(A);
    
    % direct
    [u5,d5,v5]=SVDIndirect(A);
        
    % Francis
        [d6]=mp_mysvd(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(n(i)),' Dense symmetric 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(n(i)),' Dense symmetric 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(n(i)),' Dense symmetric 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(n(i)),' Dense symmetric matrix'] )
        legend('Built-in function', 'Comb. Built in','Comb. Square','Indirect Method','Direct Method','Francis');
        print(h,'-dpng');
end