function u = mysqrt(a) %compute fifteen approximations to square root of a %using Newton's method. %In order to study the result, we collect two columns of information. %the first column will hold the approximations to the square root of a. %The second column will hold the error, that is the difference between %the square root computed using matlab's sqrt function and the value %from Newton's method. u=zeros(15,2); u(1,1)=a; %We use a as the initial guess. for j = 2:15; u(j,1) = 0.5*(u(j-1,1)+a/u(j-1,1)); end %In the statement below, : refers to the entire column. u(:,2)= abs(sqrt(a) -u(:,1)); end %---------------------end file-------------- Output from above file. >> format long e >> mysqrt(5) ans = 5.000000000000000e+00 2.763932022500210e+00 3.000000000000000e+00 7.639320225002102e-01 2.333333333333333e+00 9.726535583354368e-02 2.238095238095238e+00 2.027260595448332e-03 2.236068895643363e+00 9.181435736138610e-07 2.236067977499978e+00 1.882938249764265e-13 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 2.236067977499790e+00 0 >> 2.236067977499790e+00 * 2.236067977499790e+00 ans = 5.000000000000001e+00