1. Engineering
  2. Computer Science
  3. a matlab question which need to modify the code and...

Question: a matlab question which need to modify the code and...

Question details

A Matlab question, which need to modify the code and answer it, thanks!

Modify the script to collect average solve times over a range of matrix sizes Ne, for all three of the above described matrix classes. However, we expect that solve times for a smaller matrix should be faster, therefore a larger Nez is needed to get the measured time above tic and tocs range of accuracy. Likewise, different types of matrices may require more or less Ne than others. The takeaway: using the same Nea for all sizes and types of matrix is poor experimental design.

% experimental parameters
N = 1000;
Nex = 50;


% three matrix types

% dense matrix (no zeros)
Md = randn(N,N);

% upper triangular
Mt = triu(Md);

% randomly row-exchanged upper triangular (these are tricky array commands,
% but if you run a small sample, it is clear they do the right thing)
idx=randperm(N);
Mp = Mt(idx,:);

% exact solution of all ones
x = ones(N,1);

% right-side vectors
bd = Md*x;
bt = Mt*x;
bp = bt(idx);

% dense test
tic
for jj = 1:Nex
xd = Mdd;
end
dense_time=toc;

% upper tri test
tic
for jj = 1:Nex
xt = Mtt;
end
tri_time=toc;

% permuted upper tri test
tic
for jj = 1:Nex
xp = Mpp;
end
perm_tri_time=toc;

%Computing avgerage solve times
avg_tri_time = tri_time/(Nex);

avg_perm_time = perm_tri_time/(Nex);

avg_dense_time = dense_time/(Nex);

% You may find the following code helpful for displaying the results 
% of this demo.
type_times = {'Dense',avg_dense_time,'Upper Triangular', ...
              avg_tri_time,'permuted Upper Triangular', ...
              avg_perm_time};
fprintf(' 
')
fprintf('Estimated time for a %s matrix is %f seconds. 
',type_times{:})
Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution