フィルターのクリア

Compute the infinite sum of pi/4 up to 5 correct decimals.

1 回表示 (過去 30 日間)
Snirisa  Gödel
Snirisa Gödel 2013 年 9 月 19 日
I want to approximate pi/4 by using the infinite sum pi/4=((-1)^(n))/(2*n+1) from 0 to infinity. Here is my MATLAB code:
if true
tol = 10^(-5) %Up to five correct decimals
d=1; %Arbitrarily chosen, only condition is that d>tol at the start
sn=0; %Initial starting value
n=0;
while d>tol
sn = ((-1)^(n))/(2*n+1); This is the approximation for pi/4.
d=abs(pi/4-sn); Value that decides whether the loop continues.
n=sn; I'm not sure but I want n to increase by one each
time to prevent
circular calculations.
end
Why do I get Inf+ Nani?
  2 件のコメント
dpb
dpb 2013 年 9 月 19 日
Why do I get Inf+ Nani?
Because of
n=sn;
Try each step directly at the command line and see what happens...
You need to set a value for the total of the summation of each term initially to zero and then accumulate the terms into that variable -- sn is ok for the name but you don't accumulate a sum of the terms in n, you replaced in with the individual term going forward.
To get the subsequent terms you need to increment n, that is correct --
n=n+1;
where the n=sn; line is instead; the implementation of accumulating the summation is left for you to consider how to do that a little more...
Snirisa  Gödel
Snirisa Gödel 2013 年 9 月 19 日
Thank you dpb, I appreciate you're answer.

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

採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2013 年 9 月 19 日
編集済み: Azzi Abdelmalek 2013 年 9 月 19 日
tol = 10^(-5)
d=1;
sn=0; %
n=0;
while d>tol
sn = sn+(-1)^n/(2*n+1);
n=n+1;
d=abs(pi/4-sn);
end
disp(sn)
  2 件のコメント
Jan
Jan 2013 年 9 月 19 日
編集済み: Jan 2013 年 9 月 19 日
This is obviously a homework question. Solving it does not allow the OP to find the solution by his own.
Azzi, please do not post solutions of homework questions, because this reduces the reputation and efficiency of the forum. As a teacher on a university you should think of your colleagues, who do not want to get solutions created by others than their students.
Snirisa  Gödel
Snirisa Gödel 2013 年 9 月 19 日
Thank you.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeTime Series Objects についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by