0

Suppose I have a fmincon function. As we know from matlab documentation we can impose linear and nonlinear constraints.

Suppose now I have a function of 3 parameters to optimize. And I want 3 of them to be greater than 0 and 1 of them to be greater than -1 I would need 4 constraints but I get an error.

Simple example (working code):

A=eye(4)
A(4,4)=-1;
b=100*ones(4,1)
b(4,1)=+1
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2+x(3);
fmincon(fun,[0,0,0],A,b)

The error is

Error using fmincon (line 287) A must have 3 column(s).

It is strange that A can only have n constraints (that you could add with non linear)

Thanks

  • 2
    Please give us the exact error message that you get and also the `size` of the parameters that you pass to `fmincon`. – Lumen Oct 02 '16 at 20:11
  • I have added a simple example –  Oct 02 '16 at 20:31
  • YOur init guess has three elts, but the matrix A is expecting to operate on a 4 elt. You may have meant to create a 4x3 matrix (which will be 4 inequality constraints) – alexandre iolov Oct 02 '16 at 20:34
  • Alexandre, you are so right! I was very stupid. Thanks a lot. I guess you could create an answer so other people do not make the same mistake. Even if I do not understand why shouldn't I give n starting values with n number of parameters? –  Oct 02 '16 at 20:35

2 Answers2

1

Instead of treating the the two absolute constraints as 4 separate linear constrains why not treat them as a 2 nonlinear constrains specifically. x^2 < 9 ?

kabla002
  • 321
  • 4
  • 13
  • Good idea. Can you add an example to your question, where you show how to do that? – hbaderts Oct 02 '16 at 20:23
  • I was thinking about that but it was odd in my opinion that it was not possible to solve it with linear constraints! And I was courious about it –  Oct 02 '16 at 20:32
1

Your function fun expects exactly three inputs, i.e. the vector x will always be 3x1. So your starting point must be a 3x1 vector, not 4x1. The fmincon function allows you to specify any number of linear constraints of the form Ax ≤ b. Here, the Ax is a matrix multiplication: each column in A corresponds to one of the dimensions of x, thus A has to have exactly three columns. The number of rows can be any arbitrary number - though of course b will have to have the same dimension!

Small example: if you have the inequality 3*x + 4*y - z ≤ 1, then the first row of A is [3, 4, -1]. And the first entry of b is 1. Now, let's make up an additional constraint, e.g. y ≤ 4, so you have to add a row [0, 1, 0] to A and 4 to b. Your matrices are

A = [3, 4, -1;
     0, 1, 0];
b = [1; 4];

In your case, you want more conditions than variables. You can do that by calling eye with two parameters: number of rows and number of columns:

>> A = eye(4, 3);
A =
     1     0     0
     0     1     0
     0     0     1
     0     0     0

and manually add the last constraint:

A(4,:) = [0, 0, -1];

To implement the constraint, that all parameters have to be greater than 0, and z has to be smaller than 1, you can create your matrices as follows:

A = -eye(4, 3);
A(4,:) = [0, 0, 1];
b = [zeros(3,1); 1];

i.e. the equations are:

-1 * x ≤ 0, which equals x ≥ 0
-1 * y ≤ 0, which equals y ≥ 0
-1 * z ≤ 0, which equals z ≥ 0
z ≤ 1

now, you can use fmincon:

>>fmincon(fun, zeros(3,1), A, b);
ans =
    1.0000
    1.0000
    0.0000
hbaderts
  • 14,136
  • 4
  • 41
  • 48
  • Why do I have to give necessarly 4 starting point? otherwise it does not wok –  Oct 02 '16 at 21:43
  • No, *very* important: do *not* give 4 starting points! As your function expects 3 parameters, give 3 starting points - as in the last example in my answer. The error was in your matrix `A`: it had 4 columns instead of 3 columns. As explained in my answer, the number of columns always has to be the same as the number of parameters, i.e. 3. – hbaderts Oct 02 '16 at 21:47