Edmond Halley (the astronomer famous for discovering Halley\'s Comet) invented a
ID: 2319341 • Letter: E
Question
Edmond Halley (the astronomer famous for discovering Halley's Comet) invented a fast algorithm for computing the squareroot of a number, A. Halley's algorithm approximates Squareroot A as follows: Start with an initial guess x_1. The new approximation is then given by Y_n = 1/A x_n^2 x_n+1 = x_n/8 (15 - y_n (10 - 3y_n)) These two calculations are repeated until some convergence criterion, epsilon, is met |x_n+1 - x_n| lessthanorequalto epsilon Write a MATLAB program that utilizes a for loop to approximate the squareroot of a number. The program should have two inputs, the initial guess and the convergence criterion. One way to stop the loop when the convergence criterion is satisfied is to use an if statement to test the criteria and the term break, both within the loop. Test your function by approximating the squareroot of 5 and comparing it to the value calculated with the built-in MATLAB functionExplanation / Answer
Matlab function:
function y = squareroot1(x0,e,A)
xn =0;
while 1==1
yn = (x0^2)*(1/A);%yn
xn = (x0/8)*(15-(yn*(10-3*yn)));%xn+1
if abs(xn-x0) <= e %convergence criteria
break
end
x0 = xn;
end
y=xn;
end
output:
>> sqrt1 = squareroot1(2,10^-6,5)
sqrt1 =
2.2361
>> sqrt1 = squareroot1(1,10^-6,5)
sqrt1 =
2.2361
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.