infinite series for pi

조회 수: 1 (최근 30일)
AngelsaAtWar
AngelsaAtWar 2013년 2월 2일
Hi I am trying to create an infinite series for pi using INF SIGMA http://mathworld.wolfram.com/images/equations/PiFormulas/NumberedEquation14.gif
I am having a hard time figuring out how to even start this....
i have tried.....
value=0;
k=1;
for k=1:3;
if k<=3;
value= .0625^k
n=n+1;
end
end
for the first part but it says "unexpected expression error". I have to have it go up to 20 terms from 0. Can someone please help me with this. I am new to MATLAB and having a hard time doing this series. Thank You. This is the student version of MATLAB
  댓글 수: 5
per isakson
per isakson 2013년 2월 3일
편집: per isakson 2013년 2월 3일
I cannot reproduce the error you see. A couple of comments
  • k = 1; -- has no effect
  • n = n+1; -- n must have an initial value
  • value is a scalar that stores the last value, i.e (1/16)^3
  • "n's to k's at (n=n+1)" -- do not assign values to the loop counter
Walter Roberson
Walter Roberson 2013년 2월 3일
When you try, what is the shortest part that produces the unexpected expression error? Does it say which line it is occurring on? Does it print out the line with a vertical bar under one of the characters? If it does then it is saying that character begins the unexpected one.
What character set are you using?

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

채택된 답변

Image Analyst
Image Analyst 2013년 2월 3일
Hint:
theSum = 0.0;
r = -41.2; % I have no idea what r is supposed to be.
% Plug in the correct value for r.
for k = 1 : 14 % or whatever k you want to stop at
term1 = (4 + 8*r) / (8*k + 1);
term2 = -8*r / (8*k+2);
term3 = -4*r / (8*k+3);
term4 = -(2+8*r) / (8*k+4);
term5 = -(1+2*r) / (8*k+5);
term6 = -(1+2*r) / (8*k+6);
term7 = r / (8*k+7);
term8 = (1/16)^k;
thisSum = (term1+term2+term3+term4+term5+term6+term7)*term8;
theSum = theSum + thisSum
end
  댓글 수: 3
Image Analyst
Image Analyst 2013년 2월 3일
Your shorter version works with no error, though the k=k+1 line is useless. What do you think the "for" line does? It iterates over k, so why are you trying to increment k yourself? Also, you didn't say what the theoretical value for r should be.
AngelsaAtWar
AngelsaAtWar 2013년 2월 4일
the theoretical value apparently was 0 for r and thnk you so much for your help it did work!!! God bless you and sorry that I am new to all this stuff!

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

추가 답변 (1개)

Richard Zapor
Richard Zapor 2013년 2월 3일
Vectorization and output
%AngelsaAtWar
%http://mathworld.wolfram.com/PiFormulas.html
% The ./ and .^ are needed for the vectorization
ferguson=@(r,k)( (4+8*r)./(8*k+1)-8*r./(8*k+2)-4*r./(8*k+3)-(2+8*r)./(8*k+4) -...
(1+2*r)./(8*k+5)-(1+2*r)./(8*k+6)+r./(8*k+7) )./16.^k;
r=0;
kmax=19; % Twenty terms including the zeroth
terms=ferguson(r,0:kmax);
pi_approx=sum(terms);
fprintf('%.8g\n',terms)
fprintf('Pi approx %.8g\n',pi_approx)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by