how do I calculate pi
古いコメントを表示
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 件のコメント
Walter Roberson
2019 年 10 月 3 日
What are you expecting input(100) to do for you?
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!