%Run the different sections of this worksheet to get some intuition about
%the singular values of different kinds of matrices
%
%%
%hilb(N) produces the NxN Hilbert matrix
%
%hilb(N)_{ij}=1/(i+j)
%
N=25
h=hilb(N);
%We compute the svd
[u,s,v]=svd(h);
figure, plot(log10(diag(s)))
title('Hilbert matrix log10-sing vals')
%%
%Vandermonde matrix
v=1:.125:4;
Vv=vander(v);
[vu,vs,vu]=svd(Vv);
figure, plot(log10(diag(vs)))
title('Equally spaced Vandermonde matrix log10-sing vals')
%%
n=length(v);
tt=(2*(0:n-1)+1)*pi/(2*n);
%These numbers of the zeros of a Chebyshev polynomial
ct=cos(tt);
%We construct a Vanermonde matrix with these numbers as nodes
%
Vt=vander(ct);
[cu,cs,cu]=svd(Vt);
figure, plot(log10(diag(cs)))
title('Chebyshev Vandermonde matrix log10-sing vals')
%%
M=100
%These are uniformly distributed random numbers
%
rm=rand([M,M]);
[ru,rs,rv]=svd(rm);
figure, plot(diag(rs))
title('Uniform random matrix sing vals')
%The singular values of a Random matrix lie in a small range
%We'll generate a sequence of random matrices and create a histogram of
%their spectra
spec=diag(rs);
  rm3=rand([M,M,5000]);
  for j=1:1000
[ru,rs,rv]=svd(rm3(1:M,1:M,j));
spec=[spec, diag(rs)];
    end
   
   figure, histogram(spec)
   title('Empirical distribution of the sv for random matrices')
   %%
   %Does anyone have an idea why there is a eigenvalue near to 50=M/2?
   %Here's a clue:
   figure, histogram(sum(rm3(1:M,1:M,1)'))
   %%
   %These are Gaussian distributed random numbers
   %
   rm=randn([M,M]);
[ru,rs,rv]=svd(rm);
figure, plot(diag(rs))
title('Gaussian random matrix sing-vals')
%The singular values of a Random matrix lie in a small range
%We'll generate a sequence of random matrices and create a histogram of
%their spectra
spec=diag(rs);
  rm3=randn([M,M,5000]);
  for j=1:1000
[ru,rs,rv]=svd(rm3(1:M,1:M,j));
spec=[spec, diag(rs)];
    end
   
   figure, histogram(spec)
   title(sprintf('Empirical distribution of the sing-vals\n for 5000 random %g x %g matrices',M,M))
   