infinite series for pi
Show older comments
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 Comments
per isakson
on 3 Feb 2013
Try to format the code in the question. It is difficult to see the line breaks. I added
n = 0;
and now it runs just fine
AngelsaAtWar
on 3 Feb 2013
AngelsaAtWar
on 3 Feb 2013
per isakson
on 3 Feb 2013
Edited: per isakson
on 3 Feb 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
on 3 Feb 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?
Accepted Answer
More Answers (1)
Richard Zapor
on 3 Feb 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)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!