% matrix_timing checks how the time to do matrix operations scales % with the dimension of the matrix. It uses the MATLAB tic and toc % functions to do the timing. % Programmer: Dick Furnstahl furnstahl.1@osu.edu % Revision history: % 20-May-2005 -- original version for Physics 263 close all; clear; % this closes plots and sets variables to zero exp_min = 2; % the smallest matrix dimension is 10^(exp_min) exp_max = 3; % the largest matrix dimension is 10^(exp_max) num_matrices = 10; % the number of different matrices to test % sizes is an array with the range of matrix dimensions, % spaced logarithmically (and rounded to integers) sizes = round( logspace(exp_min,exp_max,num_matrices) ); % consider each matrix size in turn for n = 1:num_matrices size = sizes(n); % "size" is the dimension of the current matrix disp('now calculating matrix dimension: '); disp(size); % print out M = rand(size,size); % create a size-by-size matrix with random % entries between 0 and 1 % timing for taking a determinant tic; % start the stopwatch detM = det(M); % do the calculation we're timing time_det(n) = toc; % stop the stopwatch and save the result in % the vector time_det % timing for taking an inverse tic; % start the stopwatch inverseM = inv(M); % do the calculation we're timing time_inv(n) = toc; % stop the stopwatch and save the result in % the vector time_inv end figure(1); % plot the first curve with circles ('o') loglog(sizes,time_det,'o'); title('time for matrix operations'); % add a title xlabel('dimension of matrix'); ylabel('time'); % add labels hold on; % now plot another curve with 'x' (others: '*', '+') loglog(sizes,time_inv,'x'); legend('determinant','inverse'); hold off;