clear all; close all; clc

m=40;
n=40;
A=randn(m,n);
b=randn(m,1);


B=A;
B(:,end)=A(:,1);

cond(B)
det(B)

pcolor(A), colorbar, colormap(hot)
cond(A)
%det(A)
x1=A\b;

figure(2), pcolor(B), colorbar, colormap(hot)
cond(B)
%det(B)
x2=B\b;



figure(3)
subplot(2,1,1), bar(x1)
subplot(2,1,2), bar(x2)




figure(4)
noise=10^(-6);
subplot(5,1,1), bar(x2)
figure(5)
subplot(5,1,1), bar(x2)
c(1)=cond(B);
no=randn(m,1)
for j=1:4
    B2=A; 
    B2(:,end)=A(:,1)+noise*j*no;
    x3=B2\b;
    % QR solution
    
    figure(5)
    [Q,R]=qr(B2,0);
    y=(Q.')*b; x4=R\y;
    subplot(5,1,j+1), bar(log(x4))
    
    figure(4)
    c(j+1)=cond(B2);
    subplot(5,1,j+1), bar(log(x3))
end


figure(7)
bar(log(c))



figure(7)
noise=10^(-14);
subplot(5,1,1), bar(x2)
figure(8)
subplot(5,1,1), bar(x2)
c(1)=cond(B);
for j=1:4
    B2=A; 
    no=randn(m,1)
    B2(:,end)=A(:,1)+noise*no;
    x3=B2\b;
    % QR solution
    
    figure(8)
    [Q,R]=qr(B2,0);
    y=(Q.')*b; x4=R\y;
    subplot(5,1,j+1), bar(log(x4))
    
    figure(7)
    c(j+1)=cond(B2);
    subplot(5,1,j+1), bar(log(x3))
end


