How to do iteration using while loop?

2 ビュー (過去 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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by