Search code examples
matlabvectorconstraintselementminimization

MATLAB fmincon constraining vector elements


Thanks for reading this, I have a matlab function 'myfun' that returns a scalar for a given input vector X. Now I am trying to minimize this function using fmincon but I have troubles constraining my output vector elements.

X0=1:1:10;

fhandle = @myfun;

lb=X0(1)*ones(length(X0),1);
ub=X0(end)*ones(length(X0),1);

[X]=fmincon(fhandle,X0,[],[],[],[],lb,ub);

First off, the elements cannot be smaller than X0(1) or larger than X0(end). So far so good I think, but I have two more constraints for my output vector which I cannot find a solution for searching the questions here. The first one being

X(1)=X0(1) 
and 
X(end)=X0(end)

So the first and last elements must be set as constants.

My final constraint has to do with the change in value from element i to i+1, it has to be limited to a certain value A and element i must always be less than or equall to element i+1

X(i)<=X(i+1)
X(i+1)-X(i)<=E

An example output X with the following inputs X0 and A would be

X0=1:1:10;
E=3;

X=[1 1.1 1.2 1.4 1.7 2.0 2.7 4.7 7 10]

If somebody has tips on which parts/functions of fmincon or other minimization functions in Matlab to use, much appreciated!

PS: As I read the full post again I realize that my 2 constraints I'm looking for will imply the first one


Solution

  • Your question consists out of two parts:

    1. Applying equality constraints on the design variables:

      Set the lower bound and upper bound to the same value:

      ub(1) = lb(1)
      lb(end) = ub(end);
      
    2. Applying inequality constraints (X(i+1)-X(i)<=E):

      Reformulate your equations in the following matrix form:

      A*X <= B
      

      with

      A = zeros(9, 10);
      A(:, 1:9) = -eye(9)
      A(:, 2:10) = A(:, 2:10) + eye(9)
      
      B = ones(9, 1)*E;
      

    Then you can call fmincon as follows:

    [X]=fmincon(fhandle,X0,A,B,[],[],lb,ub);