% Script SqrtWhile
% Computes square roots of random numbers (While-Loop Implementation)

  clc
% e is a random number that satisfies 0< e < 6
  x = 1000000*rand;
  fprintf('x = %20.15f\n',x)
  disp(' ')
  disp(' k       relative error')
  disp('--------------------------------------')
  
% Exact square root..
  s = sqrt(x);
% Dimensions of initial rectangle..
  L = x; W = 1;
  k = 0;
  while k==0 || relError > 10^-15
     k = k+1;
     % Make the rectangle more square..
     L = (L+W)/2; W = x/L;
     relError = abs(L-s)/s;
     fprintf('%3d  %20.15e\n',k,relError)
  end
  fprintf('\nConverged after %1d steps\n',k)