how to calculate alternating summation for pi

1 回表示 (過去 30 日間)
John Jamison
John Jamison 2017 年 1 月 31 日
コメント済み: Star Strider 2017 年 1 月 31 日
How do I write code to estimate pi up to 1 million terms (n=1million) of: pi = 4(1/1 - 1/3 + 1/5 - 1/7 +...etc)

回答 (2 件)

Star Strider
Star Strider 2017 年 1 月 31 日
Use MATLAB vectors:
N = 1E+6; % Number Of Terms
N = 2*fix(N/2); % Assure Even Number Of Terms
sgn = -(-1).^[1:N]; % Sign Vector
denom = 1:2:2*N; % Denominator Vector
pie = 4*sum(1./(denom.*sgn)) % Desired Result
error = pi - pie % Difference Between Our Calculation And MATLAB ‘pi’
pie =
3.14159165358978
error =
1.00000001213019e-06
  2 件のコメント
John Jamison
John Jamison 2017 年 1 月 31 日
for the 'sgn' and 'denom' steps, how could you make those into vector equations—i.e 'v =' ?
Star Strider
Star Strider 2017 年 1 月 31 日
I’m not certain what you mean by ‘vector equations—i.e 'v =' ’ A more detailed description or explanation would be helpful.
They are already ‘vector equations’ as I interpret it. I combined them in the ‘pie’ assignment. You can break them out as a single vector and then calculate ‘pie’ as:
v = denom.*sgn;
pie = 4*sum(1./v)
While I’m thinking about it, remember to celebrate International Pi Day (to three significant digits) on March 14 (3 14 2017).

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


KSSV
KSSV 2017 年 1 月 31 日
編集済み: KSSV 2017 年 1 月 31 日
  1 件のコメント
John Jamison
John Jamison 2017 年 1 月 31 日
I don't understand loops. How can I solve it simpler without the 'while' and 'end'?

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

カテゴリ

Help Center および File ExchangeMultirate Signal Processing についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by