format long
for k=4:2:12
    a = 1.0;
    b = -(2+10^(-k));
    c = 1.0;
    x1 = (-b + sqrt(b*b-4*a*c)) / (2*a)
    x2 = (-b - sqrt(b*b-4*a*c)) / (2*a)
    x3 = 1/x1
end

%

for k=4:2:12
    a = 1.0;
    b = -(2+10^(-k));
    c = 1.0;
    
    x1 = (-b + sqrt(b*b-4*a*c)) / (2*a);
    x2 = (-b - sqrt(b*b-4*a*c)) / (2*a);
    x3 = 1/x1;
    relerr2=abs(x2-x3)/abs(x3)
end

%

% Condition Number Using Chain Rule
display('Condition number')
a = 1.0;
b = -(1e6+1e-6);
c = 1.0;
    
u1 = b^2;
k1 = abs(b*(2*b)/b^2);

u2 = u1-4;
k2 = abs(u1*1/u1);

u3 = sqrt(u2);
k3 = abs( u2 * 1/(2*sqrt(u2)))/abs(sqrt(u2));

u4 = u3-b;
k4 = abs(u3/(u3-b));    % First root
%k4 = abs(u3/(-u3-b));   % Second root

u5 = u4/2;
k5 = abs(u4 * 1/2/ (u4/2) );

k=k1*k2*k3*k4*k5

%

% Condition Number Using Chain Rule
% Homework Problem
display('Homework problem - Condition number')
n = 4;
a = 1.0;
b = -(2+10^(-n));  % Vary n
c = 1.0;
u1 = b^2;
k1 = abs(b*(2*b)/b^2);

u2 = u1-4;
k2 = abs(u1*1/u1);

u3 = sqrt(u2);
k3 = abs( u2 * 1/(2*sqrt(u2)))/abs(sqrt(u2));

u4 = u3-b;
k4 = abs(u3/(u3-b));    % First root
%k4 = abs(u3/(-u3-b));   % Second root

u5 = u4/2;
k5 = abs(u4 * 1/2/ (u4/2) );

k=k1*k2*k3*k4*k5

% Try n=+4