loop through same equation

1 visualización (últimos 30 días)
Joel22
Joel22 el 5 de Mzo. de 2020
Comentada: Joel22 el 5 de Mzo. de 2020
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

Respuestas (1)

James Tursa
James Tursa el 5 de Mzo. de 2020
Editada: James Tursa el 5 de Mzo. de 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;

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by