MATLAB: How should I use Fmincon function when I just have linear constraints but Nonlinear objective function

fmincon + linear constraints

Hi my objective function is nonlinear, and my only constraints are:
x(1)<0.00125
x(1)>0
x(2)>0
And I wanna solve it with interior point optimzation method I get error for this code :
x0=[1.1;3]; % Starting guess
B = [1.25;20.0];
A = [1,0;0,1];
lb=[0.5;0.4];
up=[1.25;20.0];
Aeq=[];
beq=[];
options=optimset('outputfcn',@outfun,'Largescale','off','Algorithm','interior-point','Display','iter','Tolx',1e-15,'Tolfun',1e-16,'MaxFunEvals',60000,'MaxIter',1000);
[x,fval,exitflag,output]=fmincon(@optim,x0,A,B,Aeq,beq,lb,ub,options);
it said I need a nonlcons in my Fmincon function So I add this function
function [c,ceq]=const(x)
c(1)=x(1)-0.00125;
c(2)=-x(1);
c(3)=-x(2);
ceq=[];
end
and add these to my code:
nonlcon=@const
[x,fval,exitflag,output]=fmincon(@optim,x0,A,B,Aeq,beq,lb,ub,'nonlcon',options);
But I still get error for this nonlcon 🙁

Best Answer

Your problem is that you are using options in the place where fmincon expects to see nonlinear constraints. In other words, your call should be
[x,fval,exitflag,output]=fmincon(@optim,x0,A,B,Aeq,beq,lb,ub,[],options);
But wait, there's more. You should NOT use A and B to set bounds. And you have lb and ub that do not make sense to me, because you say that your only constraints are x(i) > 0 and x(1) < 0.00125. So get rid of A, B, nonlcon, and set
lb = [0,0];
ub = [0.00125,Inf];
[x,fval,exitflag,output]=fmincon(@optim,x0,[],[],[],[],lb,ub,[],options);
Alan Weiss
MATLAB mathematical toolbox documentation