Multiple conditions using while loop
Show older comments
Respected sir, I am facing problem in executing while loop with multiple conditions. The code is given below.
The problem is the loop is updating values for only once and after that its returning the same value. Please point out the error as i need to update all the parameters each time within the given boundation untill . The boundary limits for each parameter are:
188<Po<210;
290<Th<312;
Po+Th=500;
2.88<EP1<4.5;
22<EP2<26;
0.01<SIG1<0.022
0.2<SIG2<0.6
The initial values i have taken are ,Po=190,EP1=1,EP2=3, EP3=23,SIG1=0,SIG2=0.015,SIG3=0.3
function [ model] = updateModel( model )
dEP2 = 1;
dEP3 = 1;
dSIG2 = 0.01;
dSIG3 = 0.1;
dPo = 2;
while(model.Po+model.Th==500&& model.Po>188 && model.Po<210 && model.Th >290&& model.Th <312&& ...
(model.EP2>2.8&& model.EP2<4.5)&&(model.EP3>22&& model.EP3<26)&&(model.SIG2>0.01&& model.SIG2<0.022)&&(model.SIG3>0.2&& model.SIG3<0.6))
model.EP1 = model.EP1;
model.SIG1 = model.SIG1;
model.Po = model.Po + round(dPo*(randn/2));
model.Th = (500-model.Po) ;
model.EP2 = model.EP2 + dEP2*(randn/2);
model.EP3 = model.EP3 + dEP3*(randn);
model.SIG2 = model.SIG2 + dSIG2*(randn/2);
model.SIG3 = model.SIG3 + dSIG3*(randn/2);
end
end
3 Comments
How is this supposed to work? I don't see where anything is done with the intermediate parameter values. The function simply perturbs the parameter values until they walk outside the boundaries. The loop only exits when the parameter set is outside of the specified region, not within.
As an example:
dEP2 = 1;
dEP3 = 1;
dSIG2 = 0.01;
dSIG3 = 0.1;
dPo = 2;
model.Po = 190;
model.Th = 500-model.Po;
model.EP2 = 3;
model.EP3 = 23;
model.SIG2 = 0.015;
model.SIG3 = 0.3;
n = 0;
t = testparams(model);
while all(t)
%model.EP1 = model.EP1; % this does nothing
%model.SIG1 = model.SIG1; % this does nothing
model.Po = model.Po + round(dPo*(randn/2));
model.Th = (500-model.Po) ;
model.EP2 = model.EP2 + dEP2*(randn/2);
model.EP3 = model.EP3 + dEP3*(randn);
model.SIG2 = model.SIG2 + dSIG2*(randn/2);
model.SIG3 = model.SIG3 + dSIG3*(randn/2);
n = n+1;
t = testparams(model);
end
n % loop executed twice
t % two parameters are out of bounds (SIG2 and EP3)
model
function tvec = testparams(model)
tvec = [model.Po+model.Th==500 model.Po>188 model.Po<210 model.Th>290 model.Th<312 ...
model.EP2>2.8 model.EP2<4.5 model.EP3>22 model.EP3<26 ...
model.SIG2>0.01 model.SIG2<0.022 model.SIG3>0.2 model.SIG3<0.6];
end
The loop exits after a variable number of passes, not just one. The loop only exits when the set of parameters contains a value outside the specified limits.
My guess is that this loop shouldn't even be part of this function, but part of the calling scope instead, but that's just a wild guess.
Voss
on 21 Dec 2021
What is the initial value of model.Th?
Assuming it is 310, which is the only way the loop will execute at all, when I run this I find that the loop usually iterates once - but sometimes more than once - and the values stored in the model struct do seem to get updated correctly.
Therefore, can you please explain more about what you mean by, "The problem is the loop is updating values for only once and after that its returning the same value."? Does this mean the same model struct that is returned the first time is passed back in again later without modification? If that's the case, then of course the loop will iterate zero times on the second and subsequent times through the function, because the while condition has not changed since the first time through when it became false and the function returned.
Regardless, if you want the loop to iterate more times, you can decrease some of the dPo, etc., values and/or widen some of the boundary limits away from the initial values, if either of those things make sense to do in context.
Hemen Gogoi
on 21 Dec 2021
Answers (0)
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!