Question: I need to know how to write a code in Matlab for determining the number of iterations needed for my pi approximation code to accurately
I need to know how to write a code in Matlab for determining the number of iterations needed for my pi approximation code to accurately approximate pi up to 100 digits. My problem is that the built in matlab function vpa which provides the correct answer from which to compare my approximation is symbolic. I know I need to compare my approximation to that provided by the vpa command using a while loop and breaking it once the two values are at least equal, but I'm having trouble
syms y a b t j approxpi
a = 1;
b = 1/sqrt(2);
t = 1/4;
j = 1;
accuratepi = vpa(pi,100);
approxpi = 3.14;
counter = 1;
for ii = 1:1000000
y = a;
a = (a+b)/2;
b = sqrt(b*y);
t = t - j*(a-y)^2;
j = 2*j;
approxpi = (a^2)/t;
if approxpi == accuratepi
break
end
counter = counter + 1;
end
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
