Smallest value for n in Leibniz Formula for Pi

2 ビュー (過去 30 日間)
Neto Dos Santos Rodrigues
Neto Dos Santos Rodrigues 2018 年 10 月 15 日
回答済み: Torsten 2018 年 10 月 15 日
Hello, I am new to MATLAB and am having some trouble to solve this problem. Given the Leibniz Formula for Pi:
In which A is the result for a given N, and R becomes Pi as N increases:
Here it is defined the relative error E=|(A-R)/R)|. What I need to look for is the smallest value for N in the case of E<=10^-6. This is what I`ve been trying in matlab so far:
What I get is a list for all the values for A and R, as well as ans=[]. but I don`t know how to ask exactly for the value of N for my desired E. I`m sorry if this is a stupid question, like I said I just started using matlab. Any help would be appreciated.

採用された回答

Torsten
Torsten 2018 年 10 月 15 日
a = 0;
rel_error = 1.0;
n = 0;
while (rel_error > 1.0e-6)
a = a + 4*(1-2*mod(n,2))/(2*n+1);
rel_error = abs(a-pi)/pi;
n = n+1;
end
n

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecial Characters についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by