% Script SqrtFor
% Computes square roots of random numbers (For-Loop Implementation)

  clc
  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;
  for k=1:20
     % Make the rectangle more square..
     L = (L+W)/2; W = x/L;
     % See if relative error <= 10^-15
     relError = abs(L-s)/s;
     if relError > 10^-15
        fprintf('%3d  %20.15e\n',k,relError)
     else
        fprintf('%3d      Converged\n',k)
     end
  end
    