Question

MATLAB: Plot the times elapsed from four methods of solving sparse and full matrices. I have...

MATLAB: Plot the times elapsed from four methods of solving sparse and full matrices.

I have the following code. I can generate the matrices requires and get the time elapsed for each method printed on the command window. How can I plot the results in a log-log graph?

% Program 2.1 Sparse matrix setup

% Input: n = size of system

% Outputs: sparse matrix a, r.h.s. b

function [a,b] = sparsesetup(n)

e = ones(n,1); n2=n/2;

a = spdiags([-e 3*e -e],-1:1,n,n); % Entries of a

c=spdiags([e/2],0,n,n);c=fliplr(c);a=a+c;

a(n2+1,n2) = -1; a(n2,n2+1) = -1; % Fix up 2 entries

b=zeros(n,1); % Entries of r.h.s. b

b(1)=2.5;b(n)=2.5;b(2:n-1)=1.5;b(n2:n2+1)=1;

disp('For sparse a\b');

tic;a\b;toc;

disp('For sparse Inv(A)*B');

tic;inv(a)*b;toc;

disp('For sparse gmres')

tic;gmres(a,b);toc

disp('For sparse bicg')

tic;bicg(a,b);toc

disp('For full a\b');

tic;full(a)\b;toc;

disp('For full Inv(A)*B');

tic;inv(full(a))*b;toc;

disp('For full gmres')

tic;gmres(full(a),b);toc

disp('For full bicg')

tic;bicg(full(a),b);toc

Homework Answers

Answer #1

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

clear all
clc
n=2:2:50;
y1=[];
y2=[];
y3=[];
y4=[];
y5=[];
y6=[];
y7=[];
y8=[];
for i=1:length(n)
[~,~,y1(i),y2(i),y3(i),y4(i),y5(i),y6(i),y7(i),y8(i)] = sparsesetup(n(i));
end
loglog(n,y1,n,y2,n,y3,n,y4,n,y5,n,y6,n,y7,n,y8);
legend('For sparse a\b','For sparse Inv(A)*B','For sparse gmres','For sparse bicg','For full a\b','For full Inv(A)*B','For full gmres','For full bicg')
function [a,b,y1,y2,y3,y4,y5,y6,y7,y8] = sparsesetup(n)

e = ones(n,1); n2=n/2;

a = spdiags([-e 3*e -e],-1:1,n,n); % Entries of a

c=spdiags([e/2],0,n,n);c=fliplr(c);a=a+c;

a(n2+1,n2) = -1; a(n2,n2+1) = -1; % Fix up 2 entries

b=zeros(n,1); % Entries of r.h.s. b

b(1)=2.5;b(n)=2.5;b(2:n-1)=1.5;b(n2:n2+1)=1;

disp('For sparse a\b');

tic;a\b;
y1=toc;

disp('For sparse Inv(A)*B');

tic;inv(a)*b;
y2=toc;

disp('For sparse gmres')

tic;gmres(a,b);
y3=toc;

disp('For sparse bicg')

tic;bicg(a,b);
y4=toc;

disp('For full a\b');

tic;full(a)\b;
y5=toc;

disp('For full Inv(A)*B');

tic;inv(full(a))*b;
y6=toc;

disp('For full gmres')

tic;gmres(full(a),b);
y7=toc;

disp('For full bicg')

tic;bicg(full(a),b);
y8=toc;
end

Kindly revert for any queries

Thanks.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT