function sparsemvtiming
% Measurement of runtimes for matrix$\times$vector multiplication with
% a sparse matrix. 
N = 2000;                     % Maximal size of matrix
% Initialize random column vectors of length $N$ and $N-1$, respectively.
d = rand(N,1); dl = rand(N-1,1); du = rand(N-1,1); v = rand(N,1); 
% Initialize dense \textbf{triadiagonal matrix}, see the documntation of the MATLAB command
% \texttt{diag} for details.
T_dense = diag(d)+diag(dl,-1)+diag(du,1);
% Initialize \emph{sparse} \textbf{triadiagonal matrix} 
T_sparse = inittridiag(d,du,dl);

res = []; % matrix for recording times
% conduct timings for vectors of different size $n$
for n=10:10:N 
  % Extract sub-matrices, which will be sparse and dense matrices again
  Td = T_dense(1:n,1:n); Ts = T_sparse(1:n,1:n);
  t1 = realmax; for j=1:3, tic; w1 = Td*v(1:n); t1 = min(toc,t1); end
  t2 = realmax; for j=1:3, tic; w2 = Ts*v(1:n); t2 = min(toc,t2); end
  norm(w1-w2), % Check for agreement of results
  res = [res; n, t1, t2];
end

% Create plots of runtimes. 
figure; 
plot(res(:,1),res(:,2),'b+',res(:,1),res(:,3),'r.');
xlabel('{\bf matrix size n}','fontsize',14);
ylabel('{\bf runtime [s]}','fontsize',14);
title(['runtime measurements for matrix-vector multiplication for sparse and ' ...
       'dense matrices']);
legend('dense matrix','sparse matrix','location','best');
 
print -depsc2 '../../LANMFigures/sparsemvtiming.eps'; 
end
