fmincon error: Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue.
Show older comments
Hello all ,, Hope are well.
How can i fixed this error : " Caused by: Failure in initial user-supplied objective function evaluation. FMINCON cannot continue. "
//////////////////////////////////
Objective function :
function objec = objective(x)
objec = sum(x(1),2);
end
//////////////////////////////////
Non-linear constraints :
function [c,ceq]= nlcon(x,Leaf_state,leaf_Battery,Focus_state,focus_Battery,Tesla_state,tesla_Battery,aa,bb,y,d)
n1=0.55;
n2=0.40;
n3=0.05;
e=10^-10;
c1= Leaf_state(y,d) - leaf_Battery*n1*x(2);
c2= e*leaf_Battery*n1*x(2) - Leaf_state(y,d) ;
c3= Focus_state(y,d) - focus_Battery*n2*x(2);
c4= e*focus_Battery*n2*x(2) - Focus_state(y,d);
c5= Tesla_state(y,d) - tesla_Battery*n3*x(2);
c6= e*tesla_Battery*n3*x(2) - Tesla_state(y,d);
c=[c1;c2;c3;c4;c5;c6];
c7 = x(1)+aa(y,d)+n1*x(2)*x(6)+n2*x(2)*x(7)+n3*x(2)*x(8)-(bb(y,d)+n1*x(2)*x(3)+n2*x(2)*x(4)+n3*x(2)*x(5));
c8 = Leaf_state(y,18) - n1*leaf_Battery ;
c9 = Focus_state(y,18) - n2*focus_Battery;
c10 = Tesla_state(y,18) - n3*tesla_Battery;
ceq=[c7,c8,c9,c10];
end
//////////////////////////////////
Main File :
aa=P_DC; % Output power PV
bb=Loads; % Loads
A=[];
b=[];
Aeq=[];
beq=[];
n1=0.55;
n2=0.40;
n3=0.05;
eff=0.95;
rate_leaf=6600;
rate_focus=3700;
rate_tesla=16500;
% x(1): Grid
% x(2): # of EV
% x(3): Charge of Leaf % x(6): Discharge of Leaf % x(9) : State of charge (SOC) total Leaf
% x(4): Charge of Focus % x(7): Discharge of Focus % x(10): State of charge (SOC) total Focus
% x(5): Charge of Tesla % x(8): Discharge of Tesla % x(11): State of charge (SOC) total Tesla
Leaf_state = SOC_initialy_Leaf ;
Focus_state = SOC_initialy_Focus ;
Tesla_state = SOC_initialy_Tesla ;
%x0=[0,0,0,0,0,0,0,0];
%x(1)=zeros(365,24);
x0=zeros(365,24);
for y=1:365
for d=1:24
if (d>=10 && d<=18)
if aa(y,d) > bb(y,d)
lb=[-inf,0,0,0,0,0,0,0];
ub=[inf,inf,eff*rate_leaf,eff*rate_focus,eff*rate_tesla,0,0,0];
options = optimoptions('fmincon','Algorithm','interior-point','AlwaysHonorConstraints','none');
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
Leaf_state(y,d) = n1*x(2)* Leaf_state(y,d-1) + n1*x(2)*x(3);
Focus_state(y,d) = n2*x(2)*Focus_state(y,d-1) + n2*x(2)*x(4);
Tesla_state(y,d) = n3*x(2)*Tesla_state(y,d-1) + n3*x(2)*x(5);
end
if aa(y,d) < bb(y,d)
lb=[-inf,0,0,0,0,0,0,0];
ub=[inf,inf,0,0,0,rate_leaf/eff,rate_focus/eff,rate_tesla/eff];
options = optimoptions('fmincon','Algorithm','interior-point','AlwaysHonorConstraints','none');
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
Leaf_state(y,d) = n1*x(2)* Leaf_state(y,d-1) - n1*x(2)*x(6);
Focus_state(y,d) = n2*x(2)*Focus_state(y,d-1) - n2*x(2)*x(7);
Tesla_state(y,d) = n3*x(2)*Tesla_state(y,d-1) - n3*x(2)*x(8);
end
else
if aa(y,d) > bb(y,d)
lb=[-inf,0,0,0,0,0,0,0];
ub=[inf,0,0,0,0,0,0,0];
options = optimoptions('fmincon','Algorithm','interior-point','AlwaysHonorConstraints','none');
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
Leaf_state(y,d) = 0;
Focus_state(y,d) = 0;
Tesla_state(y,d) = 0;
end
if aa(y,d) < bb(y,d)
lb=[-inf,0,0,0,0,0,0,0];
ub=[inf,0,0,0,0,0,0,0];
options = optimoptions('fmincon','Algorithm','interior-point','AlwaysHonorConstraints','none');
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
Leaf_state(y,d) = 0;
Focus_state(y,d) = 0;
Tesla_state(y,d) = 0;
end
end
end
end
//////////////////////////////////
Thanks
7 Comments
Walter Roberson
on 3 Aug 2019
Your lb and ub are very different sizes from your x0.
sum(x(1),2)
should be technically valid but it would mean the same as x(1,1) without the sum.
Moner Hamed
on 3 Aug 2019
Walter Roberson
on 3 Aug 2019
x0=zeros(365,24);
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
So you are minimizing a function of 8760 variables
lb=[-inf,0,0,0,0,0,0,0];
ub=[inf,0,0,0,0,0,0,0];
But you are providing bounds for 8 variables
% x(11): State of charge (SOC) total Tesla
You document 11 variables
Moner Hamed
on 3 Aug 2019
Walter Roberson
on 3 Aug 2019
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq,lb,ub,@nlcon,options)
Leaf_state(y,d) = 0;
Focus_state(y,d) = 0;
Tesla_state(y,d) = 0;
Why do you bother minimizing if you are going to ignore output completely?
Moner Hamed
on 3 Aug 2019
Walter Roberson
on 4 Aug 2019
Uh huh. So why bother to do the fmincon() in the case that you are going to ignore the result of the fmincon() ?
Answers (1)
Walter Roberson
on 3 Aug 2019
function objec = objective(x)
so your function expects one input
[x,fval,exit,output] = fmincon(@(x) objective(x,data),x0,A,b,Aeq,beq
But your anonymous function passes in two variables.
The second of the variables, data, is undefined
5 Comments
Moner Hamed
on 3 Aug 2019
Moner Hamed
on 3 Aug 2019
Walter Roberson
on 3 Aug 2019
Your x0 must be the same length as the number of variables you are minimizing over, so length 8
Moner Hamed
on 3 Aug 2019
Walter Roberson
on 4 Aug 2019
We cannot test your revised code without a copy of your new version and without the data that you are using.
Categories
Find more on Powertrain Blockset 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!