MATLAB: Fmincon error in intial value

fminconfunction undefinedinitvals

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)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Error in opt_v03 (line 105)
[xopt6,fval6]=fmincon(fun6,x0,Aueq,bueq,Aeq,beq,lb,ub);
These are my Input Values
x0=[0.8,20]; lb=[0.8,15]; ub=[1.5,40];
Aueq=[]; bueq=[]; Aeq=[]; beq=[];
[xopt6,fval6]=fmincon(fun6,x0,Aueq,bueq,Aeq,beq,lb,ub);
fun6 =
function_handle with value:
@(x)(1033.6304-25.9573.*x(1)+271.9537.*x(2)-33.6851.*x(1).*x(2)-19.5246.*x(2)^2).^1.5.*(7.3732+0.048178.*x(1)+0.28244.*x(2)-0.049607.*x(1).*x(2)-0.13446.*x(2)^2)
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?

Best Answer

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:
[xopt6,fval6]=fmincon(@(x)real(fun6(x)),x0,Aueq,bueq,Aeq,beq,lb,ub);
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.