You are on page 1of 1

Part a

function[A]=hw2prob1a(n,m,r,c,v)
A=zeros(n,n);
fori=1:m;
A(r(i),c(i))=v(i);
end
end

Part b
function[b,flops]=hw2prob1b(n,m,r,c,v,x)
b=zeros(n,1);
flops=0;
fori=1:m
b(r(i))=b(r(i))+v(i)*x(c(i));
flops=flops+2;
end
end

Part c
function[x,flops]=hw2prob1c(b,n,m,r,c,v)
x=tril(rand(n,n)+eye(n,n));
flops=0;
forj=1:m
A(r(j),c(j))=v(j);
ifv(j)==0;
disp('ERROR');
break;
end
b(c(j))=b(c(j))/v(j);
flops=flops+1;
fori=j+1:m
b(r(i))=b(r(i))A(r(i),c(j))*b(c(i));
flops=flops+2;
end
x=A(r(j),c(j))^1*b(r(j));
flops=flops+1;
end
end

You might also like