One numerical method for calculating the squareroot of a number is the Babylonia
ID: 3814618 • Letter: O
Question
One numerical method for calculating the squareroot of a number is the Babylonian method. In this method squareroot Beta is calculated in iterations. The solution process starts by choosing a value x_1 as a first estimate of the solution. Using this value, a second, more accurate solution x_2 can be calculated with x_2 = x_1 + rho/x_1/2, which is then used for calculating the value of the solution x_+1 from the solution x_i is x_i + 1 = x_i + rho/x_i. Write a MATLAB function, y = my squareroot (P), that calculates the squareroot of a number. In the function, use X= P for the first estimate the solution. Then, by using the general equation in a loop, calculate new, more accurate solutions. Stop the looping when the estimated relative error E defined by E = |X_i + 1 - X_i/X_i| is smaller than 0.00001.Explanation / Answer
function y=mysqrt(P)
x(1)=P;
for i=1:1:1000
x(i+1)=(x(i)+P/x(i))/2;
if abs((x(i+1)-x(i))/x(i))<0.00001
y=x(i+1);
break;
end
end
OUTPUT:
>> mysqrt(2)
ans =
1.4142
>> mysqrt(5)
ans =
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.