how do I calculate pi
Show older comments
question:
calculating pi:
pi/4= 1-(1/3)+(1/5)-(1/7)+(1/9)-... (a)
which comes from
arctanx= x-(x^3/3)+(x^5/5)-(x^7/7)+(x^9/9)-... (b)
write a function to cumpute pi using question a. you should find that this series converges slowly. Determine how many terms are required to calculate pi to a relative accuracy of 10^-5.
My function:
function pi= calculating_pi
pi1=0
prompt='calculate pi';
n=input(100);
x=1
for i=1:n
if mod(i,2)==1
pi1=pi1+(1/x);
else pi1=pi1-(1/x);
end
x=x+2
pi1 = 4 * pi1;
error = abs(pi - pi1);
fprintf('Calculated value of pi : %f\n', pi1);
fprintf('Actual value of pi : %f\n', pi);
fprintf('Error : %f\n', error);
end
end
i keep getting error using input. I have no idea what I am doing wrong.
1 Comment
Walter Roberson
on 3 Oct 2019
What are you expecting input(100) to do for you?
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!