MATLAB Answers

fmincon error in intial value

3 views (last 30 days)
Frida Seewald
Frida Seewald on 7 Jul 2020
Edited: Matt J on 7 Jul 2020
Im trying to use fmincon but I can't get rid of this error
Error using barrier
Objective function is undefined at initial point. Fmincon cannot continue.
Error in fmincon (line 834)
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in opt_v03 (line 105)
These are my Input Values
x0=[0.8,20]; lb=[0.8,15]; ub=[1.5,40];
Aueq=[]; bueq=[]; Aeq=[]; beq=[];
fun6 =
function_handle with value:
If i use the command window and type fun6([0.8,15]) it returns a scalar, so I don't understand why MATLAB is telling me the function is undefined. Can anyone please help?
Matt J
Matt J on 7 Jul 2020
I exchanged the power to 1.5 with 2 but this didn't change anything (i use the 1.5 factor as a weighting factor)
The complex numbers definitely come from the power of 1.5. Changing it to 2 should fix it, and does indeed do so when I run the code:
>> [xopt6,fval6]=fmincon(fun6,x0,Aueq,bueq,Aeq,beq,lb,ub)
xopt6 =
1.5000 40.0000
fval6 =

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 7 Jul 2020
The ‘fun6’ function produces a purely imaginary result at the initial point. That is throwing the error.
One option is to specify to use only the real result:
Other options would be to use the imag() function to return the imaginary result, or the abs() function to return the magnitude of the complex result. It depends on what you want to do.

More Answers (1)

Matt J
Matt J on 7 Jul 2020
Edited: Matt J on 7 Jul 2020
You must do something that not only gets rid of complex-valued results, but also respects fmincon's requirement that your objective be twice differentiable. One method which would take care of both problems is to square your objective:
fun6squared = @(x)(1033.6304-25.9573.*x(1)+271.9537.*x(2)-33.6851.*x(1).*x(2)-19.5246.*x(2)^2).^3 ...
The downside of this, however is that the sub-expression
c(x) = 7.3732+0.048178.*x(1)+0.28244.*x(2)-0.049607.*x(1).*x(2)-0.13446.*x(2)^2
currently has the ability to force your original objective function to go negative. In the modified problem, it can no longer do so, and therefore the location of the optimum might change. Did you even intend for the optimization to include the region c(x)<=0 ? If not, then there is no problem, although you must tell fmincon about the nonlinear constraint c(x)>=0.

Community Treasure Hunt

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

Start Hunting!