You are on page 1of 1

a)

function[y,flops]=uppertri(n,G,b)
flops=0;
y=zeros(n,1);
fori=n:1:1
forj=i+1:n
b(i)=b(i)y(j)*G(i,j);
flops=flops+2;
end
ifG(i,i)==0disp('ERROR');
break;
end
y(i)=b(i)/G(i,i);
flops=flops+1;
end

b)
n=100;
G=triu(rand(n,n)+eye(n,n));
b=rand(n,1);
[yflops]=uppertri(n,G,b)

flops=

10000

n=200;
G=triu(rand(n,n)+eye(n,n));
b=rand(n,1);
[yflops]=uppertri(n,G,b)

flops=

40000

n=400;
G=triu(rand(n,n)+eye(n,n));
b=rand(n,1);
[yflops]=uppertri(n,G,b)

flops=

160000

c) To convert the program to a lower triangular matrix from an upper triangular matrix,
you would begin the for loop for for i=1:n, and add the nested loop for j=1:i-1
instead of the original for loops.

You might also like