%%%%%%%%%%%%%%%% Aufgabe 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function s4_2(e)

%normal equations are ill conditioned!

epsilon=10^(-e),
  A = [1 1;epsilon*[1 2;2 1]]; f = [1:3]';
ATA = A'*A; ATf = A'*f;
condATA = cond(ATA),                    %condition number of ATA
factor = (2/epsilon+9*epsilon);         %factor*c should be in Z^2
cg = factor*(ATA\ATf),                  %normal equations
cqr = factor*(A\f),                     %qr-decomposition
diff = norm(cg-cqr),                    %difference


%%%%%%%%%%%%%%%% Aufgabe 3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function s43

% best fit in LSQ-sence

a=[.25 .5 .75];                 % damping constants 
tl = [0:10]';                   % data
il = [0 8 12 13 13 12 10 8 7 6 5]';
c = exp(-tl*a)\il;              %best fit of coefficients in LSQ-sence
tp=[0:0.1:20]';
plot(tl,il,'o',tp,c'*exp(-tp*a)',':');
xlabel('t [ms]'); ylabel('i(t) [mA]');
legend('data','best fit');
