Dirk
Dirk

Reputation: 131

Functionally constrained optimization using fmincon (in Matlab)?

I want to set a constraint for every x value, 0<=f(x,vctr)<=100. The result is:

??? Undefined function or variable 'x' due to functional constraint "cnstrnt".

Although it is a variable, fmincon asks for a value of x in cnstrnt. x represents multiple variables. b, k, and vctr (vector of constants) are constants. What is the correct way of coding the constraint?

function [ h ] = f(x,vctr)

g=@(x) (vctr(1,6).*x.^5+vctr(1,5).*x.^4+vctr(1,4).*x.^3+ ...
vctr(1,3).*x.^2+vctr(1,2).*x.^1+vctr(1,1));
h=fzero( @(x)(g(x)-y),0);
end

function [ cc,ceq ] = cnstrnt(x,b,vctr)

for k=1:length(b)*2

       cc(k)=f(x(k),vctr)-100;
       cc(k+1)=-f(x(k),vctr);
end

ceq=[];

end


[result,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]= fmincon(@(x)obj(x,b,c,L,vctr) ,x0,A,B,Aeq,beq,[],ub,cnstrnt(x,b,vctr),opts);

??? Undefined function or variable 'x'.

Upvotes: 0

Views: 91

Answers (1)

Dirk
Dirk

Reputation: 131

The variable must be declared with @(x) as @(x)cnstrnt(x,b,vctr). Then fmincon can recognize the variables and differentiate from the constants.

[result,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN]= fmincon(@(x)obj(x,b,c,L,vctr) ,x0,A,B,Aeq,beq,[],ub,@(x)cnstrnt(x,b,vctr),opts);

Upvotes: 1

Related Questions