MATLAB Answers

how to do conditional statements using optimisation variables?

10 views (last 30 days)
NN
NN on 4 May 2021
Commented: NN on 19 Jul 2021 at 10:30
hi all, how can i perform logical comparison using optimisation variable ?
I have tried this below code for optimisation using optimisation variable PbattV , but getting error :
Cost = Costa.*(PbattV<=200) +Costb.*(PbattV>=200);
P1= dt*Cost'*PbattV;
P2=dt*Cost'*PbattV;
P=P1-P2;
prob.ObjectiveSense = 'minimize';
prob.Objective = P;
An error occurred while running the simulation and the simulation was terminated Caused by: times Please help if you have any suggestion
basically i need to check if Pbatt is positive or negative and accordingly i have to chose Cost and use it for objective function;

Accepted Answer

Walter Roberson
Walter Roberson on 4 May 2021
Cost = Costa.*(PbattV<=200) +Costb.*(PbattV>=200);
Optimization variables cannot be converted to logical values. The only time you can compare an optimization variable to a value is in constructing a constraint.
Give up on trying to code it in terms of comparison operators: you will not succeed.
If there is a solution, then it will involve using mathematical tricks such as sqrt(x^2)/x to get the sign(x)
  6 Comments
NN
NN on 19 Jul 2021 at 10:30
In the below example,
one battery is considered and optimproblem has been defined for that.What if there are one more battery and is it possible to define two optimproblem like
prob=optimproblem;
prob1=optimproblem;
If so is this the correct format ?Please advice

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by