% MATLAB script: runtime measurements for wasteful and efficient multiplication  
% of a diagonal matrix with a vector.
res = []; % matrix for recording runtimes
for n=10:10:3000
   d = rand(n,1); v = rand(n,1); % Initialize random column vector
   D = diag(d);   % Build \emph{dense} diagonal matrix
   t1 = realmax; for j=1:3, tic; z1 = d.*v; t1 = min(toc,t1); end
   t2 = realmax; for j=1:3, tic; z2 = D*v; t2 = min(toc,t2); end
   norm(z1-z2),
   res = [res; n,t1,t2];
end

% Graphical output of runtimes (see MATLAB documentation)
figure; 
plot(res(:,1),res(:,2),'b+',res(:,1),res(:,3),'r.');
xlabel('{\bf vector length n}','fontsize',14);
ylabel('{\bf runtime [s]}','fontsize',14);
title('runtime measurements: multiplication with diagonal matrix');
legend('d.*v','D*v','location','best');

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