infinite series for pi

1 次查看(过去 30 天)
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