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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by