function [xx,X]=it_ref74(A,b,N,TOL);
%%function [xx,X]=it_ref74(A,b,N,TOL);
%%%%%%%%%%%%%%%  Iterative Refinement %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
%%  Solve Ax=b with iterative refinement as in Burden & Faires Algorithm 7.4
%%
%%  INPUTS:
%%     A    -   coefficient matrix os system (suspected to be ill-conditioned)
%%     b    -   Right Hand Side of System (as a column vector)
%%     N    -   Max number of iterations (Stop if exceeded)
%%     TOL  -   Tolerance (Stop if achieved between successive iterations)
%%  OUTPUTS:
%%     xx   - approximation to solution (column vector)
%%     X    - Matrix of successive iterations as rows.
n=length(b);
[L,U,P]=lu_gescp(A) %% A(NROW(1:n),:)=LU;
x=lup_solv(L,U,P,b);
k=1;flag=0;
while  (k<=N & flag==0);
   r=b-A*x;
   y=lup_solv(L,U,P,r);
   xx=x+y;X=[X;xx'];
   if norm(xx-x)<TOL
     flag=1;
   end; 
   k=k+1;x=xx;
end;
if k==N;
 message='max number of iterations exceeded'
end;
