it seems like for some points my function returns complex doubles, is there a way to surpress this? Also I don't see why my function would return complex numbers..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)
fmincon error in intial value
2 次查看(过去 30 天)
显示 更早的评论
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?
2 个评论
Matt J
2020-7-7
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 =
-9.1198e+10
采纳的回答
Star Strider
2020-7-7
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.
0 个评论
更多回答(1 个)
Matt J
2020-7-7
编辑:Matt J
2020-7-7
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 ...
.*(7.3732+0.048178.*x(1)+0.28244.*x(2)-0.049607.*x(1).*x(2)-0.13446.*x(2)^2).^2;
[xopt6,fval6]=fmincon(fun6squared,x0,Aueq,bueq,Aeq,beq,lb,ub);
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.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!