Matrix input and matrix variables in minimization of an objective function

3 views (last 30 days)
I have three variables S,V,B .Each variable is a 16*1 Matrix. I'd like to minimize an objective function, which is a summation of variables in these matrices. There are also some linear constraints for this model. I get the error "Not enough input arguments" on this optimization model:
% [S,V,B,fval]=fmincon('objfun',x0,[],[],[],[],[-Inf(N,1),-Inf(N,1),zeros(N,1)],[Inf(N,1),Inf(N,1),ones(N,1)],'constraint');
I guess the way I define the input variables is not right.
I've attached the main code, the objective function and the constraints. I would be appreciated if you could take a look and help me.

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 26 Mar 2015
Hi Hannaneh,
you need to have one vector of variables, not a matrix. So
  • Your x0 should be ones(3*N,1), not ones(N,3)
  • Your boundaries should be [-Inf(N,1); -Inf(N,1); zeros(N,1)], not [-Inf(N,1),-Inf(N,1),zeros(N,1)] (note the ";" instead of ",").
  • Haven't looked into you objfun, but there you can change the x from (3*N)x1 back to Nx3 if it helps.
Titus
  4 Comments
Hannaneh
Hannaneh on 26 Mar 2015
Edited: Hannaneh on 26 Mar 2015
Thanks a lot!
I did this change both for the objective function and the constraints:
function f=objfun(x)
C = x(1:N);
S = x(N + (1:N));
R = x(2*N + (1:N));
% do computation
end
function [c,ceq]=constraint(x)
C = x(1:N);
S = x(N + (1:N));
R = x(2*N + (1:N));
% constraints
end
No more "Not enough input arguments" error!Thank you! :) Although, the code is returning an answer, another error raised up:
_ * Error using fmincon User supplied objective function must return a scalar value. * _
Sorry for asking a lot, but it's strange that answer is not returned for variables?! Shall I ask this question in another converstation?
Titus Edelhofer
Titus Edelhofer on 26 Mar 2015
Again, the error tells you what's wrong ;-).
fmincon minimizes the value of the objective function. Therefore, the objfun should return a value, not a vector. Your function takes the sum, so it should be a scalar in fact.
My suggestion: put a breakpoint into your objfun (klick in the editor on one of the small lines next to the line number). A red bullet should appear. Run the code. Go stepwise through the code and see, what sizes the variables have, and esp. why at the end the return variable f is not a scalar ...
Titus

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!