Clear Filters
Clear Filters

How to do iteration using while loop?

2 views (last 30 days)
Ancy S G
Ancy S G on 28 Mar 2022
Commented: Walter Roberson on 29 Mar 2022
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?

Answers (1)

Walter Roberson
Walter Roberson on 28 Mar 2022
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 Comments
Ancy S G
Ancy S G on 28 Mar 2022
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 on 29 Mar 2022
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

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!