How to use a formula using a certain series to calculate pi?

2 Ansichten (letzte 30 Tage)
Dida Mustermann
Dida Mustermann am 12 Apr. 2018
Kommentiert: Dida Mustermann am 13 Apr. 2018
Hi there,
I am stuck on solving the following problem of a question given in the picture attachment. I would be very thankful for any kind of suggestion.
For the first part of the question I wrote the following script:
function[x]=appro_pi(n);
x=0;
for k=1:n ;
x=x+(((-1)^(k-1))/(2*k-1));
end;
appropi = 4*x;
fprintf('The approximation to pi using %g terms is: %2.9f\n',n,appropi);
end
I am stuck on understanding the part (ii) of the question and don't know what should be changed in the code of pt (i).
Thank you very much in advance.
D
  2 Kommentare
Steven Lord
Steven Lord am 12 Apr. 2018
There is no attachment.
Dida Mustermann
Dida Mustermann am 12 Apr. 2018
Thanks for letting me know. Edited the question accordingly

Melden Sie sich an, um zu kommentieren.

Antworten (2)

John D'Errico
John D'Errico am 12 Apr. 2018
Bearbeitet: John D'Errico am 13 Apr. 2018
Having made some effort, I'll give you a big hint. What you are being asked to do is compute the inverse tangent of THREE numbers, but you cannot use the atan function to do so.
You need to use the arc tangent series as provided in (2) to compute the inverse tangent of any number x. However, that series won't converge well for large values of x. This is no problem at all for x=1/8, 1/57, or 1/239.
So, for example, if you write a little function. I might call it approtan. Test it. Verify that it computes the correct values, because approtan(1/8) should be essentially identical to atan(1/8).
Then use expression (3). Your approximation to pi will be as aimple as:
4*(6*approtan(1/8) + 2*approtan(1/57) + approtan(1/239))
We can verify that this approximation to pi will be quite accurate, yielding essentially the full precision you can get from a double precision computation. That is, it will do so IF you compute those series until they yield the full precision for a double. We can see this is true from the following computation:
4*(6*atan(1/8) + 2*atan(1/57) + atan(1/239))
ans =
3.14159265358979
pi
ans =
3.14159265358979
In fact, the expression (3) is exactly pi. So, if you could compute that series for each term accurate to 100 or 1000 digits of precision, you would get pi, correct to that number of digits.
If you really need it, I'll give you one more hint. 10 terms of that series will be sufficient for convergence in double precision for any value of x no larger than 1/8. Larger values of x, such as 1/2 should require about 25 terms or so. But you don't need to worry about x that large.
Sorry. I won't write the code for you. But you already have most of what you need.
  1 Kommentar
Dida Mustermann
Dida Mustermann am 13 Apr. 2018
Thank you lots for the reply. I wrote a code in the answers below but unfortunately I get the wrong output.

Melden Sie sich an, um zu kommentieren.


Dida Mustermann
Dida Mustermann am 13 Apr. 2018
Hey all,
Thank you for your replies. I came up with this code but it does not show me the correct answer.
function [x] = appro_pi2(n)
x=0;
for k=1:n
g=(2*k-1);
x=x+((((-x)^(k-1))^g)/g);
end;
p=(6*x)*(1/8)+((2*x)*(1/57))+(x*(1/239));
appropi = 4*p;
fprintf('The approximation to pi using %g terms is:
%2.9f\n',n,appropi);
end
end
Open to any hints/suggestions. Thanks in advance.
  4 Kommentare
John D'Errico
John D'Errico am 13 Apr. 2018
You did not listen to what either of us said. Write a function that takes x as an argument, and possibly n if you want. It might start like this:
function y = myarctan(x,n)
Inside that function, you will use the series to compute the arc tangent. Then you will call if THREE times, but not inside the function. Test it out. Verify that
myarctan(18/10)
returns the same number as atan(1/8).
Dida Mustermann
Dida Mustermann am 13 Apr. 2018
Thank for helping me to understand this q, I got this problem solved now!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by