Reputation: 13
I'm trying to solve a non-liner optimization problem with a non-liner constraint by applying fmincon function in matlab. However, I got the following error: "Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue." I checked on the web a lot but I couldn't fixed it. It seams that it is a very general error message. I made my problem very small with just 3 variables, still I get the same result. here are my functions:
function main()
global x
global y
y(2)=15;
y(3)=15;
a=[0.01;0.05];
opts = optimoptions(@fmincon,'Algorithm','interior-point')
[x,fval] = fmincon(@objfun,a,[],[],[],[],0.01,1,@mycon,opts)
y(1)=x(2)*y(2)+x(3)*y(3);
x
y
fval
end
where
function [c,ceq ] = mycon( x )
c=-(x(3)*y(3)+x(2)*y(2))*x(1)+5;
ceq=[];
end
and
function fun = objfun( x )
fun=@(x)(x(2)*y(2)+x(3)*y(3))*(1+(1/x(1)-1)+x(1))+y(2)*(1+ (1/x(2)-1)+x(2))+y(3)*(1+(1/x(3)-1)+x(3));
end
Upvotes: 1
Views: 1735
Reputation: 35525
If you read the whole error you'll see that actually says:
Index exceeds matrix dimensions.
Error in mycon (line 2)
c=-(x(3)*y(3)+x(2)*y(2))*x(1)+5;
Thats because your initial conditions are a=[0.01;0.05];
but you actually want to solve 3 variables! (or you use 3 of them in the equations, e.g. x(3)
).
However if you solve that you'll get error "undefined function y
" which makes me think that you can not use global variable for the constrains.
Upvotes: 1