(loops, conditional) Edmond Halley (of comet fame) invented a fast algorithm for
ID: 3783674 • Letter: #
Question
(loops, conditional) Edmond Halley (of comet fame) invented a fast algorithm for computing the squareroot of a number. Given a number A greaterthanorequalto 1, Halley's algorithm calculates Squareroot A in the following manner: Start with an initial guess for the squareroot , given as x_1 Calculate the intermediate step y_a = 1/A x^n^2, where n is the iteration number Calculate the next interation 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 l (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?Explanation / Answer
function [value]=my_sqrt(x1,e)
A=input('Enter a number you want to know the approximation of the square root of: ');
x=input('Enter your initial guess for the square root: ');
e=input('Enter the convergence you want: ');
xold=x;
diff=10*e;
while diff>=e
Y=xold^2/A;
xnew=xold*(Y+3)/(3*Y+1);
diff=abs(xold-xnew);
xold=xnew;
end
value=xnew;
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.