Multiple conditions using while loop

4 次查看(过去 30 天)
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 个评论
Voss
Voss 2021-12-21
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
Hemen Gogoi 2021-12-21
Thanks @DGM sir for ur respond.your syntax works as required, but EP2,EP3,SIG2,SIG3 also executing when it is out of bounds . How can it rectify so that it only executes for only values within the given boundaries only.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by