How can I repeat the code for multiple timesteps?

8 ビュー (過去 30 日間)
Danny Helwegen
Danny Helwegen 2018 年 11 月 26 日
コメント済み: Walter Roberson 2018 年 11 月 26 日
Hi, I need to write a code to let a particle move multiple timesteps, where the timesteps can be any number (e.g. 2 or 50). The situation is the following, I have an array where a x-coordinate, an y-coordinate and the direction are in:
Q =
1 2 1
1 3 1
3 3 2
3 1 1
2 3 2
To make it easy, x and y can be 1, 2 or 3 and the direction can be 1 or 2. The direction I have defined as:
if Direction == 1 %If the direction ==1 then x stays equal but y becomes 1 higher
x = x
y = y + 1
end
if Direction == 2 %If the direction ==2 then y stays equal but x becomes 1 higher
x = x + 1
y = y
end
But how can i let this repeat for multiple time steps, because, like the code above is now, this should happen in one timestep where this timestep (dt) is set.

採用された回答

Walter Roberson
Walter Roberson 2018 年 11 月 26 日
for stepnum = 1 : 100
.... your existing lines
end
  2 件のコメント
Danny Helwegen
Danny Helwegen 2018 年 11 月 26 日
Matlab doesn't recognises stepnum, do i first have to define this?
Walter Roberson
Walter Roberson 2018 年 11 月 26 日
It is a for loop. for acts like a repeated assignment statement.
You can use whatever variable name is convenient for your purposes. For example,
for timestep_number = 1 : 100
... your existing lines
end
You could also use
for time = 0 : 0.01 : 5
.... your existing lines
end
but in practice it is usually easier to iterate over step numbers than to iterate over times if you need to record data as you go along.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by