How to do iteration using while loop?

조회 수: 3 (최근 30일)
Ancy S G
Ancy S G 2022년 3월 28일
댓글: Walter Roberson 2022년 3월 29일
step 1
clc;
gb=3;gs=8;s=gb;b=gs;
Eg=[50 100 150 175 200 250 300];
Egm=mean(Eg);
P=[20 15 10 5 1 0.5 0.1];
Ns = []; Nb = []; Nsb = [];
for n=1:length(Eg)
if Eg(n)>Egm
Ns = [Ns n];
elseif Eg(n)<Egm
Nb = [Nb n];
else
Nsb = [Nsb n];
end
end
step 2
X=(P(Ns)/s)-1 ;
X=(P(Nb)/b)-1 ;
X=Eg(Nsb) ;
for i=1:1:Ns
for j=1:1:Nb
Es(i)=Eg(:,i)-X(i) ;
EsL(i)=sum(Es(i));
Eb(j)=X(j)-Eg(:,j) ;
Eby(j)=sum(Eb(j));
p1=(sum(Eg(i))+Ns);
p2=(sum(Eg(j))+Nb);
if EsL(i)>Eby(j)
1s*(i)=sqrt((gb*P(i))/(p1(i)))
1b*(j)=sqrt((gb*P(j))/(p2(j)))
end
elseif EsL(i)<Eby(j)
2s*(i)=sqrt((gs*P(i))/(p1(i)))
2b*(j)=sqrt((gs*P(j))/(p2(j)))
else
end
end
end
I have to apply a condition
if s*(k+1)-s*(k)<=0.01,b*(k+1)-b*(k)<=0.01,X(k+1)-X(k)<=0.01
then terminate
else send this updated values to all n and go to step 2
where k is the no of iterations.
How to apply this iterative process using while loop?

답변 (1개)

Walter Roberson
Walter Roberson 2022년 3월 28일
k = 1;
while true
do the stuff for step 2
if s*(k+1)-s*(k)<=0.01 && b*(k+1)-b*(k)<=0.01 && X(k+1)-X(k)<=0.01; break; end
send this updated values to all n whatever that means
k = k + 1;
end
  댓글 수: 2
Ancy S G
Ancy S G 2022년 3월 28일
Ok,Thank you.But,I am applying this it shows an error ' Unrecognized function or variable 'do'.
Why is like this?
Walter Roberson
Walter Roberson 2022년 3월 29일
At the place where my code outline says
do the stuff for step 2
you should insert your code that implements step 2.
At the place where my code outline says
send this updated values to all n whatever that means
you should insert your code that implements
else send this updated values to all n

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by