
clc, clear all,

n = 100;

a = sprandn(n,n,.05);  % try 0.01

% 1)
% for i=1:n
%     a(i,i)=1;
% end

% 2)
a(1:n+1:n*n)=1;

% 3)
%mask = logical(eye(n));
%a(mask) = 1;


b = sparse(rand(n,1));

a_full = full(a);

spy(a)
title('distribution of matrix a')

b_full=full(b);

tic
for i=1:100
    x = a\b;
end
t_avg = toc/100;

tic
x = a\b;
t_sparse = toc;

tic
for i=1:100
X = a_full\b_full;
end
t_full = toc/100;

disp('Elapsed times:')
fprintf('averaged\t sparse\t\t full\n%f\t%f\t%f\n'...
                ,t_avg,t_sparse,t_full)

disp(['difference of 2 solutions ',num2str(norm(X-x))])


% Name               Size                Bytes   Attributes
% 
%   a                100x100            10296   sparse    
%   a_full           100x100            80000               
%   b                100x1               1616   sparse    
%   b_full           100x1                800                
%   x                100x1               1616   sparse    
%   X                100x1                800             

% remeber use of rand may create a singular matrix but now diagonal 1
%makes it invertible
