Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Variable cannot be overwritten in an if loop

조회 수: 1 (최근 30일)
Chun Zhang
Chun Zhang 2019년 9월 19일
마감: MATLAB Answer Bot 2021년 8월 20일
function [my1,my2,coeff1,coeff2]=tvcsysirf(theta,Q,R,H,x,p,n,c,T,indx,MaxRep); %MacRep = maximum repeat time
for dr=1:800
Sigma11 = Q(:,:,dr);
Sigma12 = Q(:,:,dr)*kron(H(:,1,dr)'*chol(R(:,:,dr))',eye(n*p+c))'; %n*p+c=19
Sigma22 = kron(H(:,1,dr)'*chol(R(:,:,dr))',eye(n*p+c))*Q(:,:,dr)...
*(kron(H(:,1,dr)'*chol(R(:,:,dr))',eye(n*p+c)))';
a=[0 sqrt(Sigma22(3,3)) 0 sqrt(Sigma22(6,6)) 0 sqrt(Sigma22(9,9)) 0 sqrt(Sigma22(12,12)) 0 sqrt(Sigma22(15,15)) 0 sqrt(Sigma22(18,18))]';%/2;
Mu = Sigma12(:,indx)*inv(Sigma22(indx,indx))*a;
Sigma = Sigma11-Sigma12(:,indx)*inv(Sigma22(indx,indx))*Sigma12(:,indx)';
[u d v] = svd(Sigma);
SS = u*sqrt(d)*v';
S = real(SS);
for rep=1:MaxRep
for t=1:T
roo1=1;
roo2=1;
if t==1;
c0=[theta(1:n*p+c:end,t,dr); theta(1:n*p+c:end,t,dr)];
y(:,t)=x(2:end);
while roo1==1
u1=Mu+S*randn(size(S,1),1);
u2=mvnrnd(zeros(size(Q,1),1),(Q(:,:,dr)+Q(:,:,dr).')/2,1)';
th1(:,t,rep)=theta(:,:,dr)+u1;
th2(:,t,rep)=theta(:,:,dr)+u2;
a1=companion(th1(:,t,rep),p,n,c);
a2=companion(th2(:,t,rep),p,n,c);
if (max(abs(eig(a1))))<1.0 & (max(abs(eig(a2))))<1.0
c1=[th1(1:n*p+c:end,t,rep); zeros(n*(p-1),1)];
c2=[th2(1:n*p+c:end,t,rep); zeros(n*(p-1),1)];
y1(:,t+1,rep)=c1+a1*y(:,t);
y2(:,t+1,rep)=c2+a2*y(:,t);
roo1=0; %%%%%%%%%%%%%%%%%%%%%%%%%
end
roo1=1;
end
else
while roo2==1
u2=0;
u1=u2;
th1(:,t,rep)=th1(:,t-1,rep)+u1;
th2(:,t,rep)=th2(:,t-1,rep)+u2;
a1=companion(th1(:,t,rep),p,n,c);
a2=companion(th2(:,t,rep),p,n,c);
if (max(abs(eig(a1))))<1.0& (max(abs(eig(a2))))<1.0
c1=[th1(1:n*p+c:end,t,rep); zeros(n*(p-1),1)];
c2=[th2(1:n*p+c:end,t,rep); zeros(n*(p-1),1)];
y1(:,t+1,rep)=c1+a1*y1(:,t,rep);
y2(:,t+1,rep)=c2+a2*y2(:,t,rep);
roo2=0;
else
roo2=1;
end
end
end
end
end
my1(:,:,dr)=squeeze(mean(y1(1:n,2:end,:),3));
my2(:,:,dr)=squeeze(mean(y2(1:n,2:end,:),3));
coeff1(:,dr)=squeeze(mean(th1(:,1,:),3));
coeff2(:,dr)=squeeze(mean(th2(:,1,:),3));
end
My problem here is that roo1 can never become 0 even if the two conditions in the if loop (a.k.a. (max(abs(eig(a1))))<1.0 and (max(abs(eig(a2))))<1.0) are fulfilled. Since the roo1 is always equal to 1, I ended up in an infinite loop. I am wondering how I can fix the problem. Heaps of thanks in advance!

답변 (2개)

Harsha Priya Daggubati
Harsha Priya Daggubati 2019년 9월 23일
편집: Harsha Priya Daggubati 2019년 9월 23일
Hi,
The code clearly shows roo1 gets updated only if max(abs(eig(a1)))<1.0 and max(abs(eig(a2))))<1.0. As per my understanding there is nothing wrong with the logic you have implemented. I doubt it might be due to the use of randomised values in your code. I suggest debugging your code using breakpoints and find the cause.
  댓글 수: 1
Chun Zhang
Chun Zhang 2019년 9월 23일
Thankyou so much Harsha. I'll try out your advice.

darova
darova 2019년 9월 23일
Just remove
11Untitled.png

Community Treasure Hunt

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

Start Hunting!

Translated by