building a for loop with t = t+1

14 ビュー (過去 30 日間)
Tim Elbers
Tim Elbers 2019 年 6 月 6 日
編集済み: Tim Elbers 2019 年 6 月 17 日
Hey All,
I have desperate question conserning building a for loop in matlab.
I want to move on in time (t) and use my old values obtained for x(t) and u(t) in the next computation\loop
from the example i know that the last line in the for loop has to be t= t+1, but i am a little bit confused because the for loop must iterate over i
I already created the code but i am struggeling to create this for loop.
let me introduce the code that i already have:

採用された回答

Dennis
Dennis 2019 年 6 月 6 日
A few things first: i is often used to increment for loops, but it is completly up to you if you use i, t or something else.
for MyLoopIncrement=1:5
disp(MyLoopIncrement)
end
The for loop in Matlab will increment without adding t=t+1. Contrary while loops will not.
t=0;
while t<5
disp(t)
t=t+1;
end
I cannot run your code, because i am missing H2bar, but my first advice would be to use proper indices. This means instead of x1, x2 x3.... use x(1), x(2), x(3). In your case x(:,1), x(:,2), x(:,3) since x is a vector.
I do not understand what you are trying to calculate, since your u values appear to be 0 (K0 is always 0) and you multiply a 2 element vector with a 2x2 matrix, but the loop you might be looking for should look somewhat like this:
x=zeros(2,10); %preallocation, works without but increases speed
x(:,1)=[1;2]; %your x0
for t=2:10
x(:,t)=x(:,t-1)+1
end

その他の回答 (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