Approximation not falls within the expected range

1 回表示 (過去 30 日間)
Zhuoying Lin
Zhuoying Lin 2017 年 11 月 14 日
編集済み: Walter Roberson 2024 年 12 月 4 日
Here's my script:
n=1;
while abs(pi-sum(4./((2*(1:n)-1).*(-1).^((1:n)+1))))>0.001
n=n+1;
end
fprintf('The approximation using Leibniz''s formula falls within 0,001 of pi when it equals to\n')
fprintf('%.9f with n equls %d\n\n',sum(4./((2*(1:n)-1).*(-1).^((1:n)+1))),n)
end
As shown, I try to make the result falls within 0.001 of pi, however, I get n=1000 an approximation equals 3.140592654.
My friend gets n=2002, so is mine wrong?
Thank you!

回答 (1 件)

ag
ag 2024 年 12 月 4 日
Hi Zhuoying,
I kindly suggest you to refer the following MathWorks file exchange page, which demonstrates implementation of the Leibniz formula: Approximation of Pi - https://www.mathworks.com/matlabcentral/fileexchange/102659-approximation-of-pi-leibniz-formula
You can find the code on the "Functions" tab.
Hope this helps!

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by