approximation of pi with arctan

term = @(x,n) (-1).^(n+1).*((x.^(2*n-1))./(2*n-1));
a1 = 0;
a2 = 0;
for n = 1:10
a1 = a1 + term(1/5,n);
a2 = a2 + term(1/239,n);
end
pi = (4*a1) - a2 % calculating pi/4
pi = 4*pi
this is my code
  1. Write a script that does the following: Approximate π using the formula from trigonom- etry (π = 4arctan1) and arctan values using the first 10 terms in the power series letting x=1.Then calculate the absolute error of your approximation using the above formula (hint: the error from k arctan x is k times the error from arctan x). Use fprintf to nicely display your results.

5 Kommentare

Star Strider
Star Strider am 2 Feb. 2020
... and what is your question?
John D'Errico
John D'Errico am 2 Feb. 2020
FIRST, you need to realize it is a t errible idea to use pi as a variable name, that approximates pi, since pi ALREADY exists as a variable that contains the value of pi. By creating that variable, you create potential bugs in the future. Just a really bad idea.
Regardless, what is your problem? Why do YOU think there is an issue? Surely you are not just posting your homework as a question for the rest of the students in your class to copy?
(To me, it looks as if you are not using the series to solve for atan(1), but for other approximations. But then, what do I know? What matters is why you are posting.)
Benjamin Trivers
Benjamin Trivers am 2 Feb. 2020
Bearbeitet: Walter Roberson am 2 Feb. 2020
ok so having pi as a variable is not good? I was working on this and got the a1 = a1 + term(1/5,n);
a2 = a2 + term(1/239,n);
but now i cant remember how i got that.
John D'Errico
John D'Errico am 2 Feb. 2020
Bearbeitet: John D'Errico am 2 Feb. 2020
Using pi as a variable name is a bad idea. It will cause you many bugs in the futre, along with anguished questions like "Why does pi no longer work for me as pi?"
The question here is NOT to compute atan(1/5) or atan(1/239) at all. READ THE QUESTION. It is to compute pi using atan(1), using the indicate series. (Then you will worry about the factor of 4.) Read your assignment. It is very clear.
" using the first 10 terms in the power series letting x=1."
You need to use x=1 there, NOT 1/5 OR 1/239, even though OTHER approximations for pi use those values.
I will add that the series form you are using will be correct, IF you use the correct value of x inside the loop.
Benjamin Trivers
Benjamin Trivers am 2 Feb. 2020
Please don't get an attitude. This is a place to post questions. Not everyone gets how to get matlab to work right away. If you don't have something that will help, keep you comments to yourself

Melden Sie sich an, um zu kommentieren.

Antworten (1)

David Hill
David Hill am 3 Feb. 2020

0 Stimmen

I provided this previous and you never commented.
Atan=0;
for k=1:10
Atan=Atan+(-1)^(k-1)/(2*k-1);
end
We tend to be blunt because we are all volunteers answering questions. You should listen to John. He is a matlab pro and well respected by all.

Tags

Gefragt:

am 2 Feb. 2020

Beantwortet:

am 3 Feb. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by