Referencing values generated in for loop

5 views (last 30 days)
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;

Accepted Answer

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

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!