% MySqrt
% Approximates the square root of an input value
% by performing a specified number of averaging steps.
% The progress of the iteration is displayed in a table.
% Solicit the input...
disp('Compute the square root of A.')
A = input('Enter a positive number:');
nSteps = input('Enter the number of averaging steps:');
% Clear the command window and print the table headings...
clc
disp(' k Estimate Error')
disp('-------------------------------------------')
% L and W are the length and width of the current rectangle.
% We start off with an A-by-1 rectangle.
L = A; W = 1;
for k=1:nSteps
% The new length is the average of the current length and width...
L = (L + W)/2;
% W is updated so that the new rectangle has area A
W = A/L;
% Compute and display the error
error = abs(L-sqrt(A));
fprintf('%3d %25.15f %5.1e\n',k,L,error)
end