Referencing values generated in for loop

5 次查看(过去 30 天)
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;

采纳的回答

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

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by