loop through same equation
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
James Tursa
on 5 Mar 2020
Edited: James Tursa
on 5 Mar 2020
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;
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!