loop through same equation

I need y(1) = A*y(0)+b then y(2)=A*y(1)+b then y(3)=A*y(2)+b, etc. 12 times. It works using the command window repeating (y=A*y+b) but not using a loop.
A = [-.2 .1 0 0 0
.1 -.4 0 .1 0
.1 .2 -.5 .1 0
0 .1 .2 -.4 0
0 0 .3 0 -.5 ] ;
b = [400 -100 -100 0 0 ]' ;
x = [0 0 0 0 0 ]' ;
y = A*x + b ;
for i = 2:12
y(i) = A * y(i - 1)+ b;
disp(y)
end

回答 (1 件)

James Tursa
James Tursa 2020 年 3 月 5 日
編集済み: James Tursa 2020 年 3 月 5 日

0 投票

In your loop, y(i) and y(i-1) are scalar elements of y, not vectors. You need to use different syntax for the vectors. E.g.,
y = A*y + b;
If you want to save each iteration in columns of y, then
y(:,i) = A*y(:,i-1) + b;

1 件のコメント

Joel22
Joel22 2020 年 3 月 5 日
Thank you!

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

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2019b

タグ

質問済み:

2020 年 3 月 5 日

コメント済み:

2020 年 3 月 5 日

Community Treasure Hunt

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

Start Hunting!

Translated by