function r = newtsqrt(x,delta,maxit)
% newtsqrt  Use Newton's method to compute the square root of a number.
%           Convergence is determined with an absolute tolerance.
%
% Synopsis:  r = newtsqrt(x,delta,maxit)
%
% Input:     x     = number for which the square root is desired
%            delta = (optional) absolute convergence tolerance.  Default: delta = 5e-9
%                    Iterations continue until abs(r-rold) < delta, where r and rold
%                    are the current and previous estimates of the square root
%            maxit = (optional) maximum number of iterations.  Default:  maxit = 25
%
% Output:    r = square root of x to within delta/2

if x<0,  error('Negative input to newtsqrt not allowed');  end
if x==0,      r=x;  return;   end
if nargin<2,  delta = 5e-9;   end
if nargin<3,  maxit=25;       end

r = x/2;  rold = x;   %  Initialize, making sure that convergence test fails on first try
it = 0;

while % ***  YOUR CODE GOES HERE  ***
  
   rold = r;                   %  Save old value for convergence test
   r = 0.5*(rold + x/rold);    %  Update the guess
   it = it + 1;                %  Increment the iteration counter

end
