% playing with numerical linear algebra--timings

% size of systems
n = 5000;

% random matrices and vectors
A = rand(n,n); b = rand(n,1);

% timings
fprintf('Matrix/vector size: %d\n', n);

tic; a = det(A); time = toc;
fprintf('Time for determinant computation: %f seconds.\n', time);
tic; x = A\b; time = toc;
fprintf('Time for one system solve       : %f seconds.\n', time);
tic; B = inv(A); time = toc;
fprintf('Time for inverse matrix         : %f seconds.\n', time);




%%%%%%%%%%%%%%%%%%%%%%%%%
% plot of time complexity:
%%%%%%%%%%%%%%%%%%%%%%%%%%

% size of systems
nn = [100, 250, 500, 1000, 2500, 5000, 10000];
l = length(nn);
time_det = zeros(l,1);
time_solve = zeros(l,1);
time_inv = zeros(l,1);

for k=1:l
    n = nn(k);
    % random matrices and vectors
    A = rand(n,n); b = rand(n,1);

    % timings
    fprintf('Matrix/vector size: %d\n', n);

    tic; a = det(A); time_det(k) = toc;
    tic; x = A\b; time_solve(k) = toc;
    tic; B = inv(A); time_inv(k) = toc;
end

plot(nn,time_det,nn,time_solve,nn,time_inv,'LineWidth', 2);
xlabel('size of system','Fontsize',16);
ylabel('time [seconds]','Fontsize',16);
legend('det(.)', 'solve', 'inversion');
title('times for different linear algebra operations','Fontsize',16);

% print -depsc LA_timings


