function [ it, nres ] = itref ( n, nit );
% ItRef
%
% This m-file is part of the material of the lectures on
%   Numerical Mathematics
% at the Math Department of Halle University, Germany.
%
% Author :      Martin Arnold, martin.arnold@mathematik.uni-halle.de
% Version of :  Apr 29, 2009
%
% Iterative refinement
%
% Parameters:
%   n      (input)  : dimension of the system of linear equations
%   nit    (input)  : number of iterative refinement steps
%   it     (output) : counter of iteration steps
%   nres   (output) : Euclidean norm of residuals
%
% Examples:
%   format long e
%   [ it, nres ] = itref ( 1000, 4 )

% -> assign workspace
it = (1:nit)';
nres = zeros ( nit, 1 );

% -> initialize random number generator
rand ( 'seed', 0 );

% -> define test problem
A = rand ( n, n );
b = rand ( n, 1 );

% -> solve system of linear equations
x = A\b;

% -> iterative refinement
for i=1:nit
    
    res = b - A*x;
    nres(i) = norm(res);
    
    del = A\res;
    x = x + del;
    
end

end
