How to solve the problem about "Conversion to logical from optim.prob​lemdef.Opt​imizationC​onstraint is not possible!"

조회 수: 9 (최근 30일)
batteryNominalEnergy=optimvar('batteryNominalEnergy',1,'LowerBound',0,'UpperBound',20);
powerElectronicsRatedPower=optimvar('powerElectronicsRatedPower',1,'LowerBound',0,'UpperBound',10);
Ppv_load=optimvar('Ppv_load',simTime,1,'LowerBound',0,'UpperBound',9.9824e+03);
Ppv_batt=optimvar('Ppv_batt',simTime,1,'LowerBound',0);
Ppv_grid=optimvar('Ppv_grid',simTime,1,'LowerBound',0,'UpperBound',9.9824e+03);
Pcurtail=optimvar('Pcurtail',simTime,1,'LowerBound',0,'UpperBound',5000);
Pbatt_load=optimvar('Pbatt_load',simTime,1,'LowerBound',0);
Pbatt=Ppv_batt*Eta_inv-Pbatt_load/Eta_inv;
Esoh=optimexpr(simTime,1);
Esoh(1)=1;
if Pbatt(i-1)>=0 %% this line reports the arror
Esoh(i)=Esoh(i-1)-0.2*(sampletime/gvarYEARS2SECONDS/20+0.5*Pbatt(i-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(i-1))/4500);
else
Esoh(i)=Esoh(i-1)-0.2*(sampletime/gvarYEARS2SECONDS/20-0.5*Pbatt(i-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(i-1))/4500);
end
Hi, I have meet the problem in MATLAB using linprog. "Pbatt" can be positive or negative. when it is posive, the battery will be charging. And when it is negetive, the battery will be discharging. The equation above caculates the battery SOH whenever the battery is charging or discharging.
"Pbatt" is calculated from the optimization variables. But if I use "abs(Pbatt)", there will be an error that I can't use abs function.
Appreciate for your help!
  댓글 수: 18
NN
NN 2021년 5월 5일
편집: NN 2021년 5월 5일
(sqrt(X^2)/X + 1)/2 * (cost2-cost1) + cost1 now takes us to cost1 if X < 0, and takes us to cost2 if X > 0 (and takes us to NaN if X is 0 exactly.)
The expression is giving Nan when X is 0, i am not sure if that gives the error for sqrt , can you please advice how can i avoid that Nan and it gives the answer 0 whenever it is Nan

댓글을 달려면 로그인하십시오.

채택된 답변

Matt J
Matt J 2019년 7월 19일
편집: Matt J 2019년 7월 19일
Introduce variables r to represent bounds on Pbatt
r=optimvar('absPbatt',simTime1,'LowerBound',0);
prob.Constraints.r_upper=Pbatt<=r;
prob.Constraints.r_lower=Pbatt>=-r;
This is equivalent to abs(Pbatt)<=r. Now, add sum(r) to your objective
prob.Objective=prob.Objective+sum(r);
and finally
prob.Constraints.SOH = Esoh(2:end)==Esoh(1:end-1)-0.2*(sampletime/gvarYEARS2SECONDS/20+0.5*r(1:end-1)*sampletime/(batteryNominalEnergy*gvarKWH2WS*Esoh(1:end-1))/4500);
  댓글 수: 2
Nicholas Deng
Nicholas Deng 2019년 7월 22일
Thank you very much, Matt. I think your answer will help me. I will try it. Then I will leave futher solution about your answer.
Matt J
Matt J 2019년 7월 22일
Akinkunmi Adegbenro's comment relocated here:
Hi Matt J, please have a look at my question too. I am facing a similar challenge as Nicholas
I am trying copy the idea you shared earlier in your answer, but the last piece of code (i.e. prob.Constraints.SOH) would be different in my case. I am struggling to understand what you did here so I am unable to adapt your answer to my application.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by