How to reference a previous step in a for-loop? ALSO how to trigger a loop to stop?

14 ビュー (過去 30 日間)
Wesser
Wesser 2021 年 1 月 28 日
回答済み: Star Strider 2021 年 1 月 28 日
Hi,
I have the following equation:
for t=1:length(TimeXaxis)
C(t) = D(t-1) + I(t) * 0.91 / Vd(t) - E(t-1)
end
that I want to put in a for-loop and have it run through a long time series. All variable values at t=0 are 0. How do I properly referenc a previous step in the loop? I imagine something along the lines of what I wrote (t-1) in the equation...?
SECOND QUESTION:
In a for-loop, how do put in a clause telling the loop to stop if an iteration's results exceed a value?

採用された回答

Star Strider
Star Strider 2021 年 1 月 28 日
How do I properly referenc a previous step in the loop?
One way is to define the values of ‘D’ and ‘E’ before the loop, then begin the loop index at 2, since MATLAB only allows integers greater than 0 as loop indices:
E(1) = ...;
D(1) = ...;
for t=2:length(TimeXaxis)
C(t) = D(t-1) + I(t) * 0.91 / Vd(t) - E(t-1)
end
In a for-loop, how do put in a clause telling the loop to stop if an iteration's results exceed a value?
Put in the test as an if block, then use the break function.

その他の回答 (0 件)

カテゴリ

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