Effacer les filtres
Effacer les filtres

Referencing values generated in for loop

2 vues (au cours des 30 derniers jours)
Jack Upton
Jack Upton le 21 Avr 2020
Commenté : Jack Upton le 21 Avr 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;

Réponse acceptée

David Hill
David Hill le 21 Avr 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 commentaire
Jack Upton
Jack Upton le 21 Avr 2020
Sorry for my unellaborate question, was in a rush. Your answer was exactly what I needed thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Multidimensional Arrays dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by