loop through same equation

1 次查看(过去 30 天)
Joel22
Joel22 2020-3-5
评论: Joel22 2020-3-5
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
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;

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by