Smallest value for n in Leibniz Formula for Pi

3 vues (au cours des 30 derniers jours)
Neto Dos Santos Rodrigues
Neto Dos Santos Rodrigues le 15 Oct 2018
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.

Réponse acceptée

Torsten
Torsten le 15 Oct 2018
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

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by