Fmincon Errors with objective function definition

Hi All,
I am trying to solve a simple fmincon problem and keep hitting the same error message:
Error using funhw2q1
Too many input arguments.
Error in fmincon (line 552)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in HW2Q1solution (line 13)
xopt(i) = fmincon('funhw2q1', x0, [], [], [], [], LB, UB, 'const_hw2q1', [], j);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
--------------------------------------------------------------------------------------------------------
Here is my main function:
x0 = [0 0];
LB = [-5 -5];
UB = [7 7];
i = 1;
for j = 0:0.01:1
xopt(i) = fmincon('funhw2q1', x0, [], [], [], [], LB, UB, 'const_hw2q1', [], j);
solution(j) = x(1)^2 + 10 * x(2) - 3 * X(1) * X(2);
i = i + 1;
end
-------------------------------------------------------------------------------------------------------
here is 'funhw2q1':
function f = funhw2q1(x)
f = x(1)^2 + 10 * x(2)^2 - 3 * x(1) * x(2);
end
----------------------------------------------------------------------------------------------------------
and here is the constraints function if that makes any sense in this problem:
function [c, Ceq] = const_hw2q1(x)
c(1) = 2*x(1)+x(2) - 4;
c(2) = x(1) + x(2) + 5;
Ceq = [];
Please help. I know it is a very simple problem but I ran out of options.

2 件のコメント

Matt J
Matt J 2020 年 12 月 2 日
編集済み: Matt J 2020 年 12 月 2 日
It doesn't really make sense for you to be using const_hw2q1() to implement your constraints, when they are in fact linear and could be implemented with appropriate A,b matrices. Also, since your objective is quadratic, quadprog would be a better tool here than fmincon.
Onur Gurler
Onur Gurler 2020 年 12 月 2 日
HI Matt,
I wasn't aware of Quadprog but I'll definitely look into it. Thank you for the suggestion.

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

 採用された回答

Stephan
Stephan 2020 年 12 月 2 日
編集済み: Stephan 2020 年 12 月 2 日

2 投票

Try:
x0 = [0 0];
LB = [-5 -5];
UB = [7 7];
ii = 1;
for jj = 0:0.01:1
xopt(ii,:) = fmincon(@funhw2q1, x0, [], [], [], [], LB, UB, @const_hw2q1);
solution(ii) = xopt(ii,1).^2 + 10 * xopt(ii,2) - 3 * xopt(ii,1) .* xopt(ii,2);
ii = ii + 1;
end
function f = funhw2q1(x)
f = x(1).^2 + 10 * x(2).^2 - 3 * x(1) .* x(2);
end
function [c, Ceq] = const_hw2q1(x)
c(1) = 2*x(1)+x(2) - 4;
c(2) = x(1) + x(2) + 5;
Ceq = [];
end
Note that in every run of your loop you get the same result - what do want to achieve? Since you do not really change anything inside your objective and your constraints, the for loop is not needed and the code simplifies to:
x0 = [0 0];
LB = [-5 -5];
UB = [7 7];
[xopt, solution] = fmincon(@funhw2q1, x0, [], [], [], [], LB, UB, @const_hw2q1)
function f = funhw2q1(x)
f = x(1).^2 + 10 * x(2).^2 - 3 * x(1) .* x(2);
end
function [c, Ceq] = const_hw2q1(x)
c(1) = 2*x(1)+x(2) - 4;
c(2) = x(1) + x(2) + 5;
Ceq = [];
end

3 件のコメント

Onur Gurler
Onur Gurler 2020 年 12 月 2 日
編集済み: Onur Gurler 2020 年 12 月 2 日
Thank you so much for a solution. I was giving an unnecessary argument 'j' in fmincon function, the last argument. So that was my problem. Yes, you are correct I put the loop incase it was needed but it wasn't.
Stephan
Stephan 2020 年 12 月 2 日
Did you notice that you can accept and/or vote for useful answers?
Onur Gurler
Onur Gurler 2020 年 12 月 2 日
I just noticed and gave you a thumbs up.

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

その他の回答 (0 件)

カテゴリ

質問済み:

2020 年 12 月 2 日

コメント済み:

2020 年 12 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by