For this problem, we wrote a function
function [ sqrroot ] = fp27( a )
tol = 1.0E-5;
if (a>0)
x = a/2;
while(1==1)
y = (x+a/x)/2;
e = abs((y-x)/y);
x=y;
if (e<tol) break;
end
end
sqrroot = x;
else
sqrroot = 0;
end
end
which can be implemented for verification as follows
EDU>> fp27(4)
ans =
2
EDU>> fp27(9)
ans =
3.0000
EDU>> fp27(16)
ans =
4.0000
No comments:
Post a Comment