Using a For Loop to calculate the pi for a taylor series
Show older comments
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 Comments
darova
on 27 Oct 2019
What about this?
John D'Errico
on 27 Oct 2019
Darova - that is a REALLY poor approximation to use to compute pi. You don't seriously want to use it for anything but a basic homework assignment, where a student learns to sum a basic series.
darova
on 27 Oct 2019
You mean that i took only 4 values of series?
John D'Errico
on 27 Oct 2019
No. I meant that the Leibniz formula is a terrible way to compute an approximation for pi. If you are going to suggest some other forumla for pi, then why not suggest something even remotely decent?
While I will admit that I did not recognize the formula Jose has been told to use, they are clearly not using Leibniz. So suggesting they use a completely different formula seems silly.
darova
on 27 Oct 2019

John D'Errico
on 28 Oct 2019
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.
Accepted Answer
More Answers (1)
Thiago Henrique Gomes Lobato
on 27 Oct 2019
Edited: Thiago Henrique Gomes Lobato
on 27 Oct 2019
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 Comments
Jose De La Pena
on 27 Oct 2019
Thiago Henrique Gomes Lobato
on 27 Oct 2019
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.
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!