Approximating Pi using a series

35 次查看(过去 30 天)
Chewystein
Chewystein 2017-9-9
So I have no idea where to start. The problem is approximating pi using (-1^(n+1))*((x^(2*n-1))/(2*n-1)) and the first 10 values in the series. Here is what I have:
function y=f(x)
y=(-1^(n+1))*((x^(2*n-1))/(2*n-1));
end
x=0;
z=10;
N=1000000;
sum=0;
deltax=(z-x)/N;
for i=1:N;
xeval1=x+(i-1)*deltax;
xeval2=x+(i)*deltax;
sum=sum+0.5*(f(xeval1)+f(xeval2))*deltax;
end
fprintf('The approximate vaule of pi %15.10f.\n',sum)
I also have to do this using Machin's formula with the first 10 values in a power series

回答(2 个)

Jose Marques
Jose Marques 2017-9-9
PI=0;
N=10; %qty of elements in the serie
v=zeros(N+1,1);
for n=0:N
PI=PI+4*(((-1)^n)/(2*n+1));
v(n+1)=PI;
PI
end

John D'Errico
John D'Errico 2017-9-9
编辑:John D'Errico 2017-9-9
Ok. So you made an effort. Not terrible, but there are some problems.
NEVER use a variable named sum. If you do, then the function sum is now useless.
Next, it looks like your function f(x) tries to return the nth term of the atan function.
http://mathworld.wolfram.com/MachinsFormula.html
https://www.craig-wood.com/nick/articles/pi-machin/
It will not work correctly, because you never pass in the value of n. Instead, try this:
machterm = @(x,n) (-1).^(n+1).*((x.^(2*n-1))./(2*n-1));
Note that I was careful to put parens around -1 there. There is a difference between -1^4 and (-1)^4.
So Machin's formula tells us that
4*atan(1/5) - atan(1/239)
is pi/4. Lets first see if the formula computes accurate values of the arctan.
atan(1/5)
ans =
0.197395559849881
sum(machterm(1/5,1:10))
ans =
0.197395559849881
machterm(1/5,1:10)
ans =
Columns 1 through 6
0.2 -0.00266666666666667 6.4e-05 -1.82857142857143e-06 5.68888888888889e-08 -1.86181818181818e-09
Columns 7 through 10
6.30153846153847e-11 -2.18453333333334e-12 7.71011764705883e-14 -2.75941052631579e-15
As you can see, machterm is set up so that we can generate all the terms at once.
4*sum(machterm(1/5,1:10)) - sum(machterm(1/239,1:10))
ans =
0.785398163397448
pi/4
ans =
0.785398163397448
As you can see, it does a pretty good job of approximating pi/4.
I suppose, given that this is homework, you are not allowed to use the vectorized solution I give here. But nothing stops you from using a loop.
machterm = @(x,n) (-1).^(n+1).*((x.^(2*n-1))./(2*n-1));
at1 = 0;
at2 = 0;
for n = 1:10
at1 = at1 + machterm(1/5,n);
at2 = at2 + machterm(1/239,n);
end
piapprox = (4*at1) - at2; % yielding pi/4
piapprox = 4*piapprox
piapprox =
3.14159265358979
pi
ans =
3.14159265358979
As you can see, it did well, as we would expect.
How might you build a direct power series approximation for pi? I would note that atan(1)=pi/4. This is the classic, basic series solution. We can test that by:
atan(1)*4
ans =
3.14159265358979
So, now just use
4*sum(machterm(1,1:10))
ans =
3.0418396189294
>> 4*sum(machterm(1,1:100))
ans =
3.13159290355855
>> 4*sum(machterm(1,1:1000))
ans =
3.14059265383979
>> 4*sum(machterm(1,1:10000))
ans =
3.14149265359003
>> 4*sum(machterm(1,1:100000))
ans =
3.14158265358979
>> 4*sum(machterm(1,1:1000000))
ans =
3.14159165358978
>> 4*sum(machterm(1,1:10000000))
ans =
3.14159255358974
As you can see, even taking 10 million terms in the simple power series for atan(1), we get a pretty darn poor approximation for pi. But that series converges quite slowly, so what can you expect?

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by