필터 지우기
필터 지우기

Whike loop does not work and there is not error message

조회 수: 1 (최근 30일)
MichaelO
MichaelO 2018년 8월 7일
댓글: MichaelO 2018년 8월 7일
I am trying to do a loop until the last value and the value before the last one are equals. I run the code without the while (manually) and does work alright but when I run with while doesn't work. In this case the convergence is in 11 but if I change the data could be in 100 or maybe more.
If you can help me I would really appreciate. Thanks in advance.
P = [0.5,0.3,0.2;0.3,0.3,0.4;0.1,0.5,0.4];
pi=2;
py=P(pi,:);
TM=[]
kk=1
pky_new=py*P^kk
pky=[0 0 0]
while (pky_new(1,1)-pky(1,1))<0.000001
kk=kk+1;
pky=pky_new;
pky_new=py*P^kk;
TM=[TM;pky_new]
end
disp(['converge at y + ' num2str(kk)])
  댓글 수: 2
jonas
jonas 2018년 8월 7일
편집: jonas 2018년 8월 7일
The condition is never met.
(pky_new(1,1)-pky(1,1))
ans =
0.2800
kk =
1
Perhaps it should say larger than (>)?
MichaelO
MichaelO 2018년 8월 7일
Oh!!!! You are right. Thank you very much. I loose about 4 hours and I didn't realize.
Thanks again!

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
Rik 2018년 8월 7일
You switched the condition: it is false on the first iteration and true when your loop should exit, which is the reverse from what it should be.
Also, in general you want a difference, so you should use abs. I don't understand the true goal of your code, so I don't know if that is what you should do.
P = [0.5,0.3,0.2;0.3,0.3,0.4;0.1,0.5,0.4];
pi=2;
py=P(pi,:);
TM=[];
kk=1;
pky_new=py*P^kk;
pky=[0 0 0];
while (pky_new(1,1)-pky(1,1))>0.000001% or maybe while abs(pky_new(1,1)-pky(1,1))>0.000001
kk=kk+1;
pky=pky_new;
pky_new=py*P^kk;
TM=[TM;pky_new];
end
disp(['converge at y + ' num2str(kk)])

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by