Edmond Hailey of comet fame invented a fast algorithm for co

Edmond Hailey (of comet fame) invented a fast algorithm for computing the squareroot of a number. Given a number A greaterthanorequalto 1, Hailey\'s algorithm calculates Squareroot A in the following manner: Start with an initial guess for the squareroot, given as x_1 (an initial guess of 1 almost always works) Calculate the intermediate step y_n = 1/A x^2_n, where n is the iteration number Calculate the next iteration approximation x_n+1 = x_n/8 (15 - y_n(10 - 3y_n)) Repeat from [2] until the convergence criterion |x_n+1 - x_n| lessthanorequalto epsilon (where epsilon is some small number) is met. Write a Matlab function called Halley_ squareroot .m which takes two inputs (A and epsilon) and returns the approximation of the squareroot of A. Compare your results with the built-in Matlab function squareroot .m. Can you determine which is faster for epsilon = 0.0001 - your function or the built-in function?

Solution

function xnew=Halley(A,epsilon)
Guess=1;
xnew=Guess;
R=Guess;
i=1;
while abs(R)>epsilon;
xold=xnew;
xnew=(xold)*((xold)^2+3*A)/(3*(xold)^2+A);
R=xnew-xold;
i=i+1;
end

In built function is faster , because of optimization used in MATLAB

 Edmond Hailey (of comet fame) invented a fast algorithm for computing the squareroot of a number. Given a number A greaterthanorequalto 1, Hailey\'s algorithm

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site