Using a For Loop to calculate the pi for a taylor series

44 ビュー (過去 30 日間)
Jose De La Pena
Jose De La Pena 2019 年 10 月 27 日
コメント済み: John D'Errico 2019 年 10 月 28 日
Write a program (using a loop) that determines for a given n. Run the program with n = 10, n = 100, and n = 1,000. Compare the result with pi. (Use format long.)
This is my code thus far,
clear;clc;
format Long
n=input('Enter the Value of n ');
Sum_n=0;
for ii=1:length(n)
Sum_n=Sum_n+((-1).^n)/((2.*n+1).^3);
end
value= nthroot(32,3)*Sum_n;
I know what I want to do with the code, I just dont know how to input it. I want the the FOR loop to add each increment of n+1 until it reaches the n the user inputs. I want the the sum of all those n's and in the end it should give me something that outpts a number that gives me pi.
  6 件のコメント
darova
darova 2019 年 10 月 27 日
John D'Errico
John D'Errico 2019 年 10 月 28 日
Admittedly, I had to look at a few pages of pi approximations before I saw the approximation that was requested in this assignment, and even there, I had to find one that was close, because the actual code used by Jose is not in fact a formula for pi, because he got the cube root thing wrong at the end. So it took a little searching to find the one he needed to use.

サインインしてコメントする。

採用された回答

John D'Errico
John D'Errico 2019 年 10 月 27 日
編集済み: John D'Errico 2019 年 10 月 28 日
I had to look online to find a formula for pi that used an alternating sum of reciprocals of cubes of odd numbers. There had to be one, and Wikipedia has it, though I had to check a few different sites before I saw that series.
In there, I see that we have
pi^3/32 = 1 - 1/3^3 + 1/5^3 -1/7^3 + ...
That means, you solve for the series, then to compute the current approximation to pi, you must multiply by 32, and then take the cube root. I think you got confused there, because you were multiplying by the cube root of 32 in the code you wrote.
A simple code to implement this for 21 terms is:
n = (0:20)';
[n,nthroot(32*cumsum((-1).^n.*1./(2*n+1).^3),3)]
ans =
0 3.1748021039364
1 3.13511291696101
2 3.14377083641878
3 3.14062114485715
4 3.14210388509366
5 3.14129194905678
6 3.14178389122763
7 3.14146367259822
8 3.14168365475933
9 3.14152608792951
10 3.14164278860378
11 3.1415539618301
12 3.14162313060566
13 3.14156822245079
14 3.14161253590592
15 3.14157625789938
16 3.14160633164585
17 3.14158112444928
18 3.14160246099173
19 3.14158424155425
20 3.14159992269186
Of course, this is not what Jose wants to use, but that is how I would write the series sums, using MATLAB as it might be used. As you can see, it does reasonably well in converging to about 6 digits after only 21 terms.
A simple looped code might be:
N = [10;100;1000];
sum_n = zeros(size(N));
for iN = 1:numel(N)
for k = 0:N(iN)
sum_n(iN) = sum_n(iN) + (-1)^k/(2*k+1)^3;
end
end
piapprox = nthroot(sum_n*32,3);
[N,piapprox,piapprox - pi]
ans =
10 3.14164278860378 5.01350139914258e-05
100 3.14159271914105 6.55512568670247e-08
1000 3.14159265365714 6.7346128673762e-11

その他の回答 (1 件)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019 年 10 月 27 日
編集済み: Thiago Henrique Gomes Lobato 2019 年 10 月 27 日
You can do it in a way to test multiple n's at giving input, as for example "[10,100,1000]" as input:
clear;clc;
format Long
n=input('Enter the Value ofs n ');
for idxN=1:length(n)
Sum_n=0;
for ii=1:n(idxN)
Sum_n=Sum_n+(-1).^(ii+1)/(2*ii-1);
end
Sum_n = Sum_n*4;
values(idxN)= Sum_n;
end
fprintf('Calculated Pi Values: \n')
values
fprintf('Difference from pi: \n')
pi-values
Enter the Value ofs n [10,100,1000]
Calculated Pi Values:
values =
3.041839618929403 3.131592903558554 3.140592653839794
Difference from pi:
ans =
0.099753034660390 0.009999750031239 0.000999999749999
The main matlab problem of your code was that you received "n" as input and looped to "length(n)", so, for example, if you give n=10 you will only have length(n)=1 loop. The formula that you wrote was also, as far as I know and tested, incorrect.
  2 件のコメント
Jose De La Pena
Jose De La Pena 2019 年 10 月 27 日
Thank you, I was having a hard time understanding what exact code to input. I also did notice that my inut for n was just one term, or a scalar, and needed to use the square brackets for a vector! I am a little confused on this line "ii=1:n(idxN)", is that telling the code to look at every element of n?
Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2019 年 10 月 27 日
No problem. Your n is a vector and has a specific number of entries, the line "ii=1:n(idx)" is just saying "Do a loop from 1 to the value at n(idx)", you can't loop the whole vector, so "ii=1:n" would be wrong since n may have many entries and then matlab can not know which one you are reffering to.

サインインしてコメントする。

カテゴリ

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