Asking for help with syntax for fmincon

4 vues (au cours des 30 derniers jours)
Arif Ahmed
Arif Ahmed le 21 Oct 2020
Commenté : Arif Ahmed le 21 Oct 2020
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

Réponse acceptée

Stephan
Stephan le 21 Oct 2020
Modifié(e) : Stephan le 21 Oct 2020
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 commentaires
Stephan
Stephan le 21 Oct 2020
Modifié(e) : Stephan le 21 Oct 2020
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 le 21 Oct 2020
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by