estimating pi with special equation

Hello,
I am in an intro to matlab course and there is a homework problem in which we must estimate pi with this given equation:
We are supposed to input the value of a certain n inputs which determines the number of terms used.
here is my code so far.
format long
format long
n=input('please input the number of terms: ')
pie(1)=sqrt(2);
for i=[1:n]
pie(i+1)=(sqrt(2+pie(i)));
end
pievalue=4./pie;
disp(pievalue(n))
Usually, the higher your input number is, the closer your answer would approach to pi. unfortunatelty, my answer approaches 2 for some reason. Can anyone help me fix this?

回答 (1 件)

Ameer Hamza
Ameer Hamza 2020 年 11 月 11 日
編集済み: Ameer Hamza 2020 年 11 月 11 日

0 投票

Here is correct code, compare it with yours to see the mistake
format long
n=input('please input the number of terms: ')
pie(1)=sqrt(2);
for i=[1:n]
pie(i+1)=(sqrt(2+pie(i)));
end
pievalue=2^(n+2)./prod(pie);
disp(pievalue)

2 件のコメント

Skyler Chaikin
Skyler Chaikin 2020 年 11 月 13 日
thank you very much, do you mind explaining that second to last line? why was mine wrong? why do you need to raise 2 to the power of n plus 2 and also divide by the product of the vector?
Ameer Hamza
Ameer Hamza 2020 年 11 月 13 日
Because on RHS of the equation all the terms are being multiplied. There are (n+1) 2s in the denominator. So if you re-arrange the equation to find the value of pi, you will get the expression used in my code.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB についてさらに検索

質問済み:

2020 年 11 月 11 日

コメント済み:

2020 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by