function sqroot(x) % SQROOT Compute square root by Newton's method % Check that value of function argument is positive. if x <= 0, error('In sqroot() : argument x must be positive'); end; % Initial guess xstart = x/2; % Iteration loop to compute square root for i = 1:100 xnew = ( xstart + x/xstart)/2; % new estimate of square root. disp(xnew); % print xnew. if abs(xnew - xstart)/xnew < eps, % check convergence of iterations. break, % iterations. end; xstart = xnew; % update estimate of square root. end