infinite series for pi

1 vue (au cours des 30 derniers jours)
AngelsaAtWar
AngelsaAtWar le 2 Fév 2013
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 commentaires
per isakson
per isakson le 3 Fév 2013
Modifié(e) : per isakson le 3 Fév 2013
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 le 3 Fév 2013
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?

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 3 Fév 2013
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 commentaires
Image Analyst
Image Analyst le 3 Fév 2013
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 le 4 Fév 2013
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!

Connectez-vous pour commenter.

Plus de réponses (1)

Richard Zapor
Richard Zapor le 3 Fév 2013
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)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by