Matrix input and matrix variables in minimization of an objective function

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.

 採用された回答

Titus Edelhofer
Titus Edelhofer 2015 年 3 月 26 日

0 投票

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 件のコメント

Hannaneh
Hannaneh 2015 年 3 月 26 日
編集済み: Hannaneh 2015 年 3 月 26 日
Hello Titus,
Thank you so much for your reply. I made the changes you mentioned. I also changed the variables into an array:
% x=[S;V;B];
x0=ones(3*N,1);
[x,fval]=fmincon('objfun',x0,[],[],[],[],[-Inf(N,1);-Inf(N,1);zeros(N,1)],[Inf(N,1);Inf(N,1);ones(N,1)],'constraint');
However, I still get the same error. Maybe the problem is the objfun.m file itself.
Reagrds, Hannaneh
O.k., now I took a look at the objfun. And yes, the error states exactly the problem ;-). Your objfun should have exactly one input, but yours has two. I assume (!), that C, S, R in your objective function are the 3 variables (each with N entries)? In this case your objective function should be something like
function y = objfun(x)
C = x(1:N);
S = x(N + (1:N));
R = x(2*N + (1:N));
% do computation
Titus
Hannaneh
Hannaneh 2015 年 3 月 26 日
編集済み: Hannaneh 2015 年 3 月 26 日
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 2015 年 3 月 26 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by