Asking for help with syntax for fmincon

Hi all,
I am trying out the standard given examples in MATLAB for fmincon usage. I am referring to this particular example:
openExample('optim/NonlinearConstraintsExample')
fun = @(x) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
lb = [0,0.2];
ub = [0.5,0.8];
function [c,ceq] = circlecon(x)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2;
ceq = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
nonlcon = @circlecon;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
I want to modify it to have multiple variables separately in separate vectors but still be able to solve the final problem. For example I make the following modification:
fun = @(x,y) 100*(x(2)-x(1)^2)^2 + (1-x(1))^2 + 100*(y(2)-y(1)^2)^2 + (1-y(1))^2;
lb = [0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8];
function [c,ceq] = circlecon(x,y)
c = (x(1)-1/3)^2 + (x(2)-1/3)^2 - (1/3)^2 + (y(1)-1/3)^2 + (y(2)-1/3)^2 - (1/3)^2;
ceq = [];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
y0 = [1/4,1/4];
nonlcon = @circlecon;
Notice that I have introduced a new 'y' variable. I can't figure out how to correctly call the fmincon function to solve the problem.
Kindly advise please.
Regards

 採用された回答

Stephan
Stephan 2020 年 10 月 21 日
編集済み: Stephan 2020 年 10 月 21 日

0 投票

The solvers in Matlab accept only one vector as variable. So change y(1) --> x(3) and y(2) --> x(4). Then edit the code this way and it will work:
fun = @(x) 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2 + 100*(x(4)-x(3).^2).^2 + (1-x(3)).^2;
lb = [0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4,1/4,1/4];
nonlcon = @circlecon;
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon)
function [c,ceq] = circlecon(x)
c = (x(1)-1/3).^2 + (x(2)-1/3).^2 - (1/3)^2 + (x(3)-1/3).^2 + (x(4)-1/3).^2 - (1/3)^2;
ceq = [];
end

3 件のコメント

Arif Ahmed
Arif Ahmed 2020 年 10 月 21 日
編集済み: Arif Ahmed 2020 年 10 月 21 日
Thanks Stephan. Because I have multiple constraints and functions for the actual problem I will be solving later on, I wanted to avoid doing this. I have seen a similar solution to what I am thinking of but I am unable to implement it in this example.
Stephan
Stephan 2020 年 10 月 21 日
編集済み: Stephan 2020 年 10 月 21 日
Of course you can do something like this:
lb = [0,0.2,0,0.2];
ub = [0.5,0.8,0.5,0.8];
A = [];
b = [];
Aeq = [];
beq = [];
x0 = [1/4,1/4];
y0 = [1/4,1/4];
nonlcon = @circlecon;
x = fmincon(@fun,[x0 y0],A,b,Aeq,beq,lb,ub,nonlcon);
x_sol = x(1:2)
y_sol = x(3:4)
function myObj = fun(x)
y(1) = x(3);
y(2) = x(4);
myObj = 100*(x(2)-x(1).^2).^2 + (1-x(1)).^2 + 100*(y(2)-y(1).^2).^2 + (1-y(1)).^2;
end
function [c,ceq] = circlecon(x)
y(1) = x(3);
y(2) = x(4);
c = (x(1)-1/3).^2 + (x(2)-1/3).^2 - (1/3)^2 + (y(1)-1/3).^2 + (y(2)-1/3).^2 - (1/3)^2;
ceq = [];
end
But this does not change the fact, that the Matlab solvers like fmincon will only accept one vector with variables. On the other hand it might be easier to understand whats happening, by doing it this way. And a little bit you accepted this already, when you defined the lower and upper bounds ;-)
Arif Ahmed
Arif Ahmed 2020 年 10 月 21 日
That is true!
I insisted on this because I am thinking that using the Symbolic Math Toolbox will come handy and therefore I want to code it such that I can easily debug the code later on.
Thanks for your input, I can definitely use your advice.

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

その他の回答 (0 件)

質問済み:

2020 年 10 月 21 日

コメント済み:

2020 年 10 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by