function  [x,Nsecure]  = newtsqrt2(a,tol)
% newtsqrt2.m 
% Stopping condition:
% Relative difference of conseutive. terms <= tol.
% Example call 
% x1=newtsqrt2(5, .00001)
% format long
% x2=newtsqrt2(5)  % Default tolerance
% [x1 x2 sqrt(5)]  % Compare results
if nargin==1     % Test nr. of input arguments
  tol=eps;       % Default tol = eps (Machine epsilon)
end    
maxiter=10;
x=-Inf;   % Elegant initialization
xnew=a;
Nsecure=0;
while (abs(xnew-x) >= x*tol) 
    if Nsecure>maxiter
        error('Max iter exceeded')
    end    
    x=xnew;
    xnew = 0.5*(x+ a/x);
    Nsecure=Nsecure+1;
end;
