Morianta
Morianta

Reputation: 21

Error while using "fmincon" in MATLAB

I have a model with linear constraints and a nonlinear objective function, and I'm trying to use "fmincon" toolbox of MATLAB to solve it. Actually, the Aineq is a 24*13 matrix, and the Aeq is a 24*13 matrix as well. But when I insert this command:

 >> [x , lambda] = fmincon(@MP_ObjF,Aineq,bineq,Aeq,beq);

I encounter this error:

Warning: Trust-region-reflective method does not currently solve this type of
problem, using active-set (line search) instead. 

In fmincon at 439??? Error using ==> fmincon at 692 Aeq must have 312 column(s).

What is probably wrong with it? Why should Aeq have 312 columns?!? I will appreciate any help. Thanks.

Upvotes: 2

Views: 2163

Answers (1)

dusty_keyboard
dusty_keyboard

Reputation: 136

If you look at the documentation for fmincon (doc fmincon ) you'll see an input called opt.In this you can set the algorithm used by matlab to solve your minimization problem. If you run Opt=optimset('fmincon');

Then you can modify the algorithm option using Opt.algorithm="active-set";

Just send Opt to fmincon and then matlab wont have this problem anymore. Take a look inside Opt and you'll find a ton of options you can change to modify the optimization routine.

As for the number of columns. If you're using linear constraints then you input argument for MPobjF must be a column vector with n rows and 1 column. Then A must be m X n. Where M is the number of constraints and n is the number of variables. This is so that matrix multiplication is well defined.

I'm sorry if my first answer was ambiguous. Maybe it will help if I do an example, as I saw several suspicious things in your comments. Lets say we want to minimize x^2 + y^2 + (z-1)^2 subject to x + y + z = 1, 2x + 3y - 4z <= 5, x,y,z>=-5. The solution is obviously (0,0,1)...

We first have to make our objective function,

fun = @(vec) vec[1]^2 + vec[2]^2 + (vec[3]-1)^2;

For fmincon to work, there can only be one input to the function, but that input can be a vector. So here x = vec[1] and so on...I think your comments are indicating that your objective function has multiple inputs. If you need to pass some parameters that aren't being optimized there is documentation for this on Matlab's site (http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html)

Then we can set the optimization settings

opt = optimset('fmincon');
opt.algorithm = 'active-set';

You may also have to modify the large-scale setting for the algorithm warning to go away, I can't remember...

Then we can set

Aeq = [1,1,1];    % equality constraint, if you had another eq constraint, it would be another row to Aeq
beq = 1;          % equality constraint
A = [2,3,-4];     % inequality
b = 5;            % inequality
lb = [-5;-5;-5];  % lower bound
x0 = [0.5;0.5;0]; % initial feasible guess, needs to be a column vector
[x,fval] = fmincon(fun,x0,A,b,Aeq,beq,lb,[],[],opt);

Then hopefully this finds x = [0;0;1]

Upvotes: 0

Related Questions