clear all
format long e

n     = input('matrix size = ');
mtx_A = eye(n);
for jj = 2:n
    mtx_A(1:jj-1,jj) = -ones(jj-1,1);
end

vec_x = zeros(n,1);
for ii = 1:n
    vec_x(ii,1) = 1 / ii;
end

rhs = zeros(n,1);
for ii = 1:n
    rhs(ii,1) = 1 / ii;
    for jj = ii+1:n
        rhs(ii,1) = rhs(ii,1) - 1 / jj;
    end
end

sol      = zeros(n,1);
sol(n,1) = rhs(n,1);
for ii = n-1:-1:1
    sol(ii,1) = rhs(ii,1);
    for jj = ii+1:n
        sol(ii,1) = sol(ii,1) + sol(jj,1);
    end
end

[U,S,V] = svd(mtx_A);

fprintf('\n');
fprintf('|| A * x - b ||_2           = %24.14e \n', norm(mtx_A*sol-rhs));
fprintf('|| x - x_exact ||_2         = %24.14e \n', norm(sol-vec_x));
fprintf('\n');
fprintf('sigma_{n-1}                 = %24.14e \n', S(n-1,n-1))
fprintf('The smallest singular value = %24.14e \n', S(n,n))
