Need help creating a loop
古いコメントを表示
Have to create a function with a loop that formulates pi using Leibniz's formula. It has to ask the user for a positive integer n&then calculate pi to n terms (terms being 4=1 , 4/3= 2, 4/5= 3, 4/7= 4, etc..)
Leibniz's formula says that pi= 4-4/3 +4/5 - 4/7 + 4/9 - 4/11 ....
OR
pi/4= 1 - 1/3 +1 /5 - 1/7 + 1/9 - 1/11...
so far, i thought to put:
function pi= pleibniz (n)
for i= 1:n pi/4=
but then i don't know what to do! please help!
採用された回答
その他の回答 (2 件)
Thomas
2011 年 10 月 19 日
2 投票
You might find this useful:
Hope this helps..
Steven
2011 年 10 月 19 日
clear all; clc;
eps = 50; % precision
piOn4 = 1;
for i = 1:eps
piOn4 = piOn4 + (-1)^(i)*(1/(2*i+1))
end
piOn4
3 件のコメント
Sean de Wolski
2011 年 10 月 19 日
Don't overwrite eps!!!!
Jan
2011 年 10 月 19 日
About the useless "clear all", see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
Daniel Shub
2011 年 10 月 19 日
Don't overwrite i!!!!!
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!