How to do iteration using while loop?

2 次查看(过去 30 天)
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