% Runtime measurements for solution of \emph{tridiagonal} linear system of
% equations in MATLAB
N = 2000; % maximum size of matrix
% Initialize tridiagonal matrix using Code~\ref{inittridiag}
A = inittridiag((1:N)',-ones(N-1,1),-ones(N-1,1)); 
v = rand(N,1); % initialize random right hand side vector

res = []; % matrix for collecting the results
for i=10:10:N
    B = A(1:i,1:i); % extract \emph{sparse} submatrix
    w = v(1:i);     % extract subvector 
    C = full(B);    % Create \emph{dense} matrix from \texttt{B}
    ts = realmax; td = realmax;
    for j=1:3, tic; z = B\w; ts = min(toc,ts); end
    for j=1:3, tic; z = C\w; td = min(toc,td); end
    res = [res; i, ts, td],
end

figure; 
plot(res(:,1),res(:,2),'r*',...
     res(:,1),res(:,1)/(res(end,1))*res(end,2),'k-');
xlabel('{\bf matrix size}','fontsize',14);
ylabel('{\bf time [s]}','fontsize',14);
title('Runtime for solving sparse linear system');
legend('sparse solve','O(n)','location','best');

print -depsc2 '../../LANMFigures/sparselseslvtiming.eps';

figure; 
loglog(res(:,1),res(:,2),'r*',...
       res(:,1),res(:,3),'b+',... 
       res(:,1),res(:,1)/(res(end,1))*res(end,2),'k-');
xlabel('{\bf matrix size}','fontsize',14);
ylabel('{\bf time [s]}','fontsize',14);
title('Runtime for solving sparse linear system');
legend('sparse solve','dense solve','O(n)','location','best');

print -depsc2 '../../LANMFigures/cmplseslvtiming.eps';
