Putting togethor constraints for fmincon

2 visualizaciones (últimos 30 días)
Nathan
Nathan el 1 de Oct. de 2022
Respondida: Torsten el 2 de Oct. de 2022
I have a hw problem that I have to place 3 circles which can be different radii in a box of given dimensiions in order to maximize the area of the circles. I also have to use fmincon. I think I have all the constraints I think I need, but I am having trouble on how to place them into their correct spots or if I need to input more information than I already have or if I am formulating some of these constraints incorrectly. The constraints are they cant overlap and they have to fit in the box. This is the code I currently have:
syms r1 r2 r3 xc1 xc2 xc3 yc1 yc2 yc3
w = 100; h = 100;
% For [r1 xc1 yc1 r2 xc2 yc2 r3 xc3 yc3]
xu = [h w h h w h h w h];
xl = [0 0 0 0 0 0 0 0 0];
x0 = [h/4 w/4 h/4 2*h/4 2*w/4 2*h/4 3*h/4 3*w/4 3*h/4]
op = optimset('disp', 'iter');
sol = fmincon(@maxarea,x0, [],[],[],[],xl,xu,@eqconstraint,op);
%maximize area
function f=maxarea(x)
global his;
r1=x(1);r2=x(2);r3=x(3);
f = -pi()*(r1^2 + r2^2 + r3^2);
end
%circles have to be within [0,h] and [0,w]
function [y, yeq] = ineqconstraint(X)
r1 =X(1);xc1=X(2);yc1=X(3);r2 =X(4);xc2=X(5);yc2=X(6);r3 =X(7);xc3=X(8);yc3=X(9);
y(1) = xc1-r1;
y(2) = xc1+r1-w;
y(3) = yc1+r1-h;
y(4) = yc1-r1;
y(5) = xc2-r2;
y(6) = xc2+r2-w;
y(7) = yc2+r2-h;
y(8) = yc2-r2;
y(9) = xc3-r3;
y(10) = xc3+r3-w;
y(11) = yc3-r3;
y(12) = yc3+rc-h;
yeq = [];
end
% circles cant overlap
function [c,heg] = eqconstraint(x)
r1=x(1);r2=x(2);r3=x(3);xc1=x(4);xc2=x(5);xc3=x(6);yc1=x(7);yc2=x(8);yc3=x(9);
heg = [];
c(1) = sqrt((xc2-xc1)^2 +(yc2-yc1)^2)-(r1+r2);
c(2) = sqrt((xc3-xc1)^2 +(yc3-yc1)^2)-(r1+r3);
c(3) = sqrt((xc3-xc2)^2 +(yc3-yc2)^2)-(r3+r2);
end

Respuestas (1)

Torsten
Torsten el 2 de Oct. de 2022
The conditions defined under "ineqconstraint" have to be put into the matrix A and the vector b of "fmincon".

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by