Referencing values generated in for loop

5 visualizaciones (últimos 30 días)
Jack Upton
Jack Upton el 21 de Abr. de 2020
Comentada: Jack Upton el 21 de Abr. de 2020
Hi, I have the following code and would like to use the initial conditions Xk and Pk for the first iteration of the loop, but after that I would like to use the values generated from the loop. I believe I have to take advantage of the (i) feature but can't seem to get it right. Any help is appreciated, thanks. Edit: The zk value should be equal to XAcc(i) inside the for loop but I was unsure how to define this initial condition.
Xk=0;
Pk=1;
zk=XAcc(1);
for i=1:100;
Kk(i)=Pk/Pk+0.1;
Xk(i)=Xk+Kk*(XAcc(i)-Xk(i));
Pk(i)=(1-Kk)*Pk;

Respuesta aceptada

David Hill
David Hill el 21 de Abr. de 2020
I did a lot of guessing. Lots of problems with your code.
Xk=0;
Pk=1;
%zk=XAcc(1);not needed
for i=2:100;%must start from 2 if you are looking back
Kk=Pk(i-1)/(Pk(i-1)+.1);%what are you trying to do here? Pk/Pk+.1 is always 1.1, did you mean Pk/(Pk+.1)? Is Kk needed in the final output? If not no need to index it.
Xk(i)=Xk(i-1)+Kk*(XAcc(i-1)-Xk(i-1));%do you want to look at the previous element of Xk?
Pk(i)=(1-Kk)*Pk(i-1);
end
  1 comentario
Jack Upton
Jack Upton el 21 de Abr. de 2020
Sorry for my unellaborate question, was in a rush. Your answer was exactly what I needed thank you!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by