Script that calculates the following sum

1 回表示 (過去 30 日間)
Juan Zegarra
Juan Zegarra 2019 年 4 月 28 日
回答済み: Jan 2019 年 4 月 29 日
Write a script that will calculate the convertage series of S= starting at n=0. This series will converge on S=2. That is 1/1 +1/2 +1/4.... The script will terminate whn 2 >S>=1.990 and it will print a sentence that contains the values for S and n. I've done this so far but I'm getting errors. Can you please help me?\
n=0
S=0
while S > 1.990
S=1/(2^n)+S
n=n+1
S(n)=S
end
  2 件のコメント
dpb
dpb 2019 年 4 月 28 日
Look at your logical test carefully...
Torsten
Torsten 2019 年 4 月 29 日
... and remove the line
S(n) = S

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

回答 (1 件)

Jan
Jan 2019 年 4 月 29 日
n=0
S=0 % Now S is a scalar
while S > 1.990 % In the first iteration you compare a scalar
% But you do not mean >, but <= !
S=1/(2^n)+S
n=n+1
S(n)=S % If n=2, you assign: S(2)=S, but for n=3:
% it is: S(3) = S and S is a [1 x 2] vector
end
Store the current value of S in a separate variable, e.g. SV, which is a vector then.

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by