Asking for help with syntax for fmincon
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Arif Ahmed
am 21 Okt. 2020
Kommentiert: Arif Ahmed
am 21 Okt. 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
0 Kommentare
Akzeptierte Antwort
Stephan
am 21 Okt. 2020
Bearbeitet: Stephan
am 21 Okt. 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 Kommentare
Stephan
am 21 Okt. 2020
Bearbeitet: Stephan
am 21 Okt. 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 ;-)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!