Ex 7.3

clear
N = 100;
A = sprand(N,N,0.05);
for i = 1:N
    A(i,i)=1;
end

% alt
%mask = spdiags(ones(N,1),0,N,N);
%mask = logical(mask);
%A(mask) = 1;

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

% a)
Af = full(A);

% b)
spy(A);

%c)
bf = full(b);
whos

% d)
tic;
x = A\b;
t1 = toc;

tic;
xf = A\bf;
t2 = toc;

tic;
xf1 = Af\bf;
t3 = toc;

disp(['1. Sparse A, sparse b: ',num2str(t1)]);
disp(['2. Sparse A, full b: ',num2str(t2)]);
disp(['3. Full A, full b: ',num2str(t3)]);

d1 = norm(x-xf,2);
d2 = norm(x-xf1,2);
disp(['Skillnad i resultat mellan 1 och 2: ', num2str(d1)]);
disp(['Skillnad i resultat mellan 1 och 3: ', num2str(d2)]);
  Name        Size             Bytes  Class     Attributes

  A         100x100             7412  double    sparse    
  Af        100x100            80000  double              
  N           1x1                  8  double              
  b         100x1               1208  double    sparse    
  bf        100x1                800  double              
  i           1x1                  8  double              

1. Sparse A, sparse b: 0.005436
2. Sparse A, full b: 0.000965
3. Full A, full b: 0.000864
Skillnad i resultat mellan 1 och 2: 0
Skillnad i resultat mellan 1 och 3: 9.3694e-15