loop through same equation
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!