Estimating the value of pi using a summation through creation of an m.file by using a loop.

My script is missing something. Perhaps I do not understand the meaning of 'while' or 'for' correctly. But here is my code:
{% this script approximates the value of pi
n=0;
x=0;
while n<100000 % loop ends at 10^6
n=n+2; % n will always be a multiple of 2
while x<3.14
x=4*((-1).^n/(2*n+1)) +x; % the summation formula
end
disp(x)
end }
I want it such that my value of pi in this case 'x' is no greater than 1e-6 away from the actual value of pi. So when it satisfies this condition the script will cease. However my script gives me endless loop of 3.2 and it pissing me off, all day on this crap :S . Why is matlab so hard?????

 채택된 답변

Adam, your while-within-a-while construct is certainly not going to work for you. The terms "4*(-1).^n/(2*n+1)" are supposed to be added once for each successive value of n, starting with n = 0, but, as it is, your code will add this term for an unchanging n a number of times before going on to the next n because of your inner while-loop arrangement. Also the n = n+2 is wrong. It should be for each successive n, n = n + 1, not every multiple of 2.
This approximation is based on the infinite series expansion of arctan:
arctan(t) = t - 1/3*t^3 + 1/5*t^5 - 1/7*t^7 + ...
with t set to 1 giving
pi/4 = arctan(1) = 1 - 1/3 + 1/5 - 1/7 + ...
With t equal to 1 this is a very, very slowly converging series, and you should have a single while-loop with the condition that 1/(2*n+1) < 1e-6 to achieve the accuracy you desire, which will require a great many loops. You should start with n = 0 and compute your first x before adding 1 to n. (Your present code erroneously uses n = 2 for its first term.)
Note that if you were to use t = 1/sqrt(2) instead of t = 1, your series would converge much, much faster, but the added terms would have to be changed to 6*(-1)^n/(2*n+1)*t^(2*n+1), since arctan(1/sqrt(2)) = pi/6.
Note 2: Sean is teasing you. He starts with pi itself and his loop never executes.
Roger Stafford

댓글 수: 5

My apologies. I wrote down the wrong value for t in the last paragraph. The paragraph should read:
Note that if you were to use t = 1/sqrt(3) instead of t = 1, your series would converge much, much faster, but the added terms would have to be changed to 6*(-1)^n/(2*n+1)*t^(2*n+1), since arctan(1/sqrt(3)) = pi/6.
Roger Stafford
% this script approximates the value of pi
n=0;
x=0;
while (-1).^n/(2*n+1)<1e-6
x=4*((-1).^n/(2*n+1)) +x;
n=n+1;
end
x.
my question states that I have to use my summation formula with a single while loop. I don't think i can use the arctan thing
@Adam - you might want to consider that
(-1).^n/(2*n+1)
can be positive or negative; but you are only interested in the magnitude. Also consider the comparison to 1e-6, I believe you mean greater than (>)
%e
n=0;
x=0;
k=4*((-1)^n/(2*n+1));
while k>1e-6
n= n+1;
x=x+k;
k=4*((-1)^n/(2*n+1));
end
x
This doesn't work either and i only want n to increase by 2 each time through the loop. It still won't converge anywhere near pi... I don't get what i'm missing.
yes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
i got it!
% if true
n=0;
x=0;
k=4*((-1)^n/(2*n+1));
while abs(k)>1e-6
n= n+1;
x=x+k;
k=4*((-1)^n/(2*n+1));
end
x

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

function myPi = piEst()
myPi = pi;
for ii = 1:0
myPi = myPi+ii;
end
end

댓글 수: 4

having difficulty comprehending what this means. you've equated myPi to pi, to it defeats the purpose of the estimation no?
A K
A K 2012년 12월 13일
편집: A K 2012년 12월 13일
Your code is so simple! The sad thing is I have to use a loop.
I used a loop!
3.141592653589793238466 is still an approximation to actual pi. So my code meets all of your constraints!
Yes i had completely forgotten on the for loop because I felt so adamant on using the while loop.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

A K
2012년 12월 13일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by