% % m1 practice exam, problem 4 % clear clf format compact nn = (2000:100:3000)'; l = length(nn); tt = zeros(l, 1); % preallocate nrep = 4; rng(1); % seed the random number generator for i = 1:l n = nn(i); A = randn(n, n); b = rand(n, 1); tic() % start a timer for j = 1:nrep % repeat nrep times for more precise timing [L, U] = lu(A); y = forwardsub(L, b); x = backsub(U, y); end tt(i) = toc()/nrep % record time end %% % Plot the performance on a log-log graph and compare it to O(n^3). % % The result could vary significantly from machine to machine. loglog(nn, tt, 'ro-') hold on loglog(nn, tt(end)*(nn/nn(end)).^3, 'k--') axis tight xlabel('size of matrix') ylabel('time (sec)') title('Timing of LU factorization method of solution') legend('lu','O(n^3)','location','southeast') grid on hold off