Trying to find out that how many loop cycle it complete before the 'S' become zero. S value is not decreasing. Need to decrease S value zero and sum the total cycle number?

1 回表示 (過去 30 日間)
>> C=0.02;
>> cycle = 0;
>> FC =0;
>> S=10;
>> while (S>=0)
S=(S-F);
F=(S*C);
FC = (FC+F);
cycle =(cycle+1);
end
  5 件のコメント
Mathieu NOE
Mathieu NOE 2021 年 6 月 10 日
Correction :
F is not initialized in your code ...

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

回答 (2 件)

Mathieu NOE
Mathieu NOE 2021 年 6 月 10 日
hello again
so I made 2 modifications
  • initialized F with 0 (my guess)
  • changed the while tolerance limit because otherwise it never ends , S continues to get smaller and smaller but never goes to zero;
C=0.02;
cycle = 0;
FC =0;
F = 0;
S=10;
tol = 1e-10;
while (S>=tol)
S=(S-F);
F=(S*C);
FC = (FC+F);
cycle =(cycle+1);
end

Walter Roberson
Walter Roberson 2021 年 6 月 10 日
F is negative. You need to decide whether F should be positive and be subtracted, or if it should be negative and be added.
I would suggest to you that it would be more consistent logically for F to be negative and be added.

カテゴリ

Help Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by