필터 지우기
필터 지우기

Referencing values generated in for loop

조회 수: 2 (최근 30일)
Jack Upton
Jack Upton 2020년 4월 21일
댓글: Jack Upton 2020년 4월 21일
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에서 Multidimensional Arrays에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by