%% How fast is Ax=b? % Kathryn Gillow %% % Let's explore how the speed of solving $Ax=b$ scales with the size $n$ of % the matrix $A$. We'll use a random $n \times n$ matrix with independent % normally distributed entries, and a random right-hand-side $b$. n = 1000; A = randn(n,n); b = randn(n,1); tic, x = A\b; toc %% Vary n % Now try for a range of matrix sizes nn = 2.^(1:13); % assign memory for tt vector tt = zeros(size(nn)); % or tt = 0*nn; for k = 1:length(nn) n = nn(k); A = randn(n,n); b = rand(n,1); tic, x = A\b; tt(k) = toc; end figure(1), clf loglog(nn,tt,'x-','MarkerSize',12,'Linewidth',3) xlabel('Matrix size n') ylabel('Time in secs') title('Time to solve Ax=b') %% % We know that it takes O(n^3) time to solve $Ax=b$ % note that the second graph on the plot is in a separate section so the % figure appears twice in the published version hold on loglog(nn, 1e-10*nn.^3,'r','Linewidth',3) legend('time','predicted time','Location','SouthEast')