%
%
%    Beispiel zum Schnittpunkt zweier Geraden
%
%    R. Unger ... 05/2018
%


clear;
close all;

m1=1;

k=0;
for m2=0:0.01:2
 k=k+1;

 A=[-m1 1 ; -m2, 1 ];

 svals= svd(A);

 smin=min(svals);
 smax=max(svals);

 x_given=[1;1];
 b_exact=A*x_given;

 % make b  10-percent noisy 
 b=b_exact* ( 1 +  0.2*rand()-0.1 ) ;
 % and solve the system
 x=A\b;
 err=norm(x-x_given);


 err_all(k)  = err;
 kappa_all(k)= smax/smin; 

 smin_all(k) = smin;
 smax_all(k) = smax;
 m2_all(k)   = m2;

end

figure(1)
%plotyy(m2_all,smin_all,m2_all,smax_all)
plot(m2_all,smin_all,m2_all,smax_all)
legend('\sigma_{min}', '\sigma_{max}')
xlabel('m2')
title('Singular values of A')
print -dpng 'x-sigma.png'


figure(2)
plot(m2_all,kappa_all)

xlabel('m2')
legend('\kappa(A)')
ylabel('\kappa')
title('Condition number of A')
print -dpng 'x-kappa.png'


figure(3)
plot(m2_all,err_all)
xlabel('m2')
legend('error')
ylabel('|| x-x\_given || ')
title('Error for noisy right hand side ')
print -dpng 'x-error.png'







