"Conversion to logical from optim.problemdef.OptimizationEquality is not possible" for below Code
Show older comments
I need to optimize my battery SOC .My control variable is flywheel speed( w_FW) which should have the initial and final speed equal to 78.539.
State Variable are SOC,flywheel speed(w_FW).
1) How should I set constraints to optimization variable and some intermediate variables like Motor rating,w_FW and others.
2) Error : Conversion to logical from optim.problemdef.OptimizationEquality is not possible @if w_FW(i,1)==w(i,1)
3) How do I set value to Final Fywheel Speed(w_FW) as I am needed to maintain my initail anfd final flywheel speed values to be same i.e:78.539
Flywheel Speed(w_FW) is a vector of size (1436x1). 1436 is the number of secs in my driving cycle.
I will need to implement dynammic programing using back Propagation
Any help would be appreciated.
Thanks in Advance
m=1200;
A_f=2;
rho=1.205;
g=9.8;
r=0.3;
f=0.01;
C=0.3;
delta_1 =0.04;
delta_2=0.017;
i_fd=3.29;
i_1=1;
eff_tr=0.95;
delta=1+(delta_1)+(delta_2)*(i_fd)^2*(i_1)^2;
T_whl(1:1436,1)=((delta.*m).*(a(1:1436,1))+0.5.*C.*A_f.*rho.*v(1:1436,1).^2+m.*g.*f).*r
w(1:1436,1)=v(1:1436,1)./r
P_load(1:1436,1)=T_whl(1:1436,1).*w(1:1436,1)
k_1=2;
k_2=4;
J_Fw=0.5;
P_rated=170000;
Q_new=(P_rated*108)/0.94;
Pm_rated=zeros(length(v),1);
w_m=zeros(length(v),1);
prob=optimproblem("ObjectiveSense",'min')
soc_min=0.2
soc_max=0.8
w_FW_min=0
w_FW_max=2303
w_m_min=0
w_m_max=628
Pm_rated_min=0
Pm_rated_max=170000
%I am not sure whether my flywheel could be considered optimization
%variable
soc=optimvar('soc',1436,1,"LowerBound",soc_min,"UpperBound",soc_max)
w_FW=optimvar('w_FW',1436,1,"LowerBound",w_FW_min,"UpperBound",w_FW_max)
% w_m=optimvar('w_m',1436,1,"LowerBound",w_m_min,"UpperBound",w_m_max)
% Pm_rated=optimvar('Pm_rated',1436,1,"LowerBound",Pm_rated_min,"UpperBound",Pm_rated_max)
prob.Constraints.cons1=w_FW>=w_FW_min
prob.Constraints.cons2=w_FW<=w_FW_max
prob.Constraints.w_FW_last=w_FW(1436,1)==78.539%I am not able to set the final flywheel speed value=78.593 as I can see that it is not available in workspace
for i=1:1:1436
if v(i+1,1)>=v(i,1)
T_m(i,1)=T_whl(i,1)./(k_1.*3.1255)
elseif v(i+1,1)<v(i,1)
T_m(i,1)=(T_whl(i,1).*0.95)./(k_1.*3.29)
end
% w_ir(i,1)=k_1.*w_v(i,1)
% w_or(i,1)=w_FW(i,1)./k_2
if v(i+1,1)>=v(i,1)
if w_FW(i,1)==w(i,1) % ERROR:Conversion to logical from optim.problemdef.OptimizationEquality is not possible.
%soc(i-1,1)=soc(i,1)
diff_soc(i,1)=0
w_FW(i+1,1)=(w_FW(i,1)+sqrt((w_FW(i,1)).^2-((4.*P_load(i,1))./(J_FW.*0.8075))))./J_FW
elseif w_FW(i,1)<=w(i,1)
w_FW(i+1,1)=(w_FW(i,1).*2)-T_m(i,1)
diff_soc(i,1)=(-((T_m(i,1).*w_FW(i,1))+(P_load(i,1)./0.8075)))./Q
elseif w_FW(i,1)>=w(i,1)
w_FW(i+1,1)=(w_FW(i,1).*2)-T_m(i,1)
diff_soc(i,1)=(((T_m(i,1).*w_FW(i,1))-(P_load(i,1)./0.8075)))./Q
end
end
if v(i+1,1)<v(i,1)
if w_FW(i,1)==w(i,1)
%soc(i-1,1)=soc(i,1)
diff_soc(i,1)=0
w_FW(i+1,1)=(w_FW(i,1)+sqrt((w_FW(i,1)).^2-((4.*P_load(i,1).*0.8075)./(J_FW))))./J_FW
elseif w_FW(i,1)<=w(i,1)
if soc(i,1)<0.8
diff_soc(i,1)= (P_load(i,1).*0.8075)./(Q)
%soc(i-1,1)=soc(i,1)+diff_soc(i-1,1)
w_FW(i+1,1)=w_FW(i,1)
elseif soc>=0.8
diff_soc=0
w_FW(i+1,1)=(w_FW(i,1)+sqrt((w_FW(i,1)).^2-((4.*P_load(i,1).*0.8075)./J_FW)))./J_FW
end
elseif w_FW(i,1)>=w(i,1)
w_FW(i+1,1)=J_FW.*w_FW(i,1)-T_m(i,1)
diff_soc(i,1)=((T_m(i,1).*w_FW(i,1))-P_load(i,1).*0.8075)./(Q)
end
end
if v(i+1,1)>=v(i,1)
w_FW(i+1,1)=w_FW(i,1)
diff_soc(i,1)=(P_load(i,1))/(0.8075.*Q)
else v(i+1,1)<v(i,1)
w_FW(i+1,1)=w_FW(i,1)
diff_soc(i,1)=(P_load(i,1).*0.8075)/(Q)
end
soc(i+1,1)=soc(i,1)
diff_soc=0
w_FW(i+1,1)=w_FW(i,1)
w_m=(w_FW(i,1)./k_2)-(k_1.*w(i,1))
prob.Constraints.cons3=w_m>=w_m_min
prob.Constraints.cons4=w_m<=w_m_max
Pm_rated=T_m(i,1).*w_m(i,1)
prob.Constraints.cons5=Pm_rated>=Pm_rated_min
prob.Constraints.cons6=Pm_rated<=Pm_rated_max
% if v(i,1)>=v(i-1,1)
% diff_soc(i-1,1)=Pm_rated(i,1)./(Q_new.*0.85.*0.94)
% elseif v(i,1)<v(i-1,1)
% diff_soc(i-1,1)=Pm_rated(i,1).*0.85.*0.94/(Q_new)
% end
% soc(1,1)=0.6
end
prob.Objective=sum(diff_soc,'all')
%write(prob.Objective,'objectivedescription.txt')
x0.w_FW=78.539;
x0.SOC=0.6;
[sol,fval]=solve(prob,x0)
for i=1:1:1436
soc(i+1,1)=soc(i,1)+change_soc(i,1)
end
Accepted Answer
More Answers (0)
Categories
Find more on Particle Swarm 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!