The following formula can be used to calculate the pi value. Set a = 1, b = 1/sq
ID: 2313902 • Letter: T
Question
The following formula can be used to calculate the pi value. Set a = 1, b = 1/sqrt(2), t = 1/4 and x = 1 Repeat the following commands until the difference between a and b is within some desired accuracy: y = a a = (a + b)/2 b = sqrt(b*y) t = t - x*(y - a)^2 x = 2*x From the resulting values of a, b and t, an estimate of pi is Pi_est = ((a + b)^2)/(4*t) How many repeats are needed to estimate pi to an accuracy of 1e-8? Write a function that ask for the accuracy (as an input) and output the number of iteration used to estimate the pi value according to that accuracy.Explanation / Answer
function [n,pi_es]=pi_est(TOL)
a=1;
b=1/sqrt(2);
t=1/4;
x=1;n=0;
while(abs(a-b)>TOL)
n=n+1;
y=a;
a=(a+b)/2;
b=sqrt(b.*y);
t=t-x.*((y-a).^2);
x=2.*x;
end
pi_es=((a+b).^2)./(4.*t);
>> [k,p]=pi_est(1e-8)
k =
3
p =
3.1416
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.