How to solve optimization problems when the constraints have derivative (symbolic) functions
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello,
I am trying to solve an optimization problem as following. There are two parabolas f and g. When they have a common tangent line, I want the common tangent points on these two curves to be as close to x1 and x2 in the parabolic functions as possible (see f and g functions in the code below). I have defined optimvar, constraints, and objective in the code. I know the main issue of the code is that I am mixing symbolic variable (x) and optimvar in functions f and g, but I do not know how to approach this in another way. I have tried to use function handles and matlabFunction, but I cannot seem to decouple symbolic variables and optimvar. Could someone please help me? Any idea will be appreciated. Thank you.
syms x
x1_tan = 3; % x-coordinate of the tangent point on f
x2_tan = 6; % x-coordinate of the tangent point on g
prob = optimproblem('ObjectiveSense', 'minimize');
a = optimvar('a', 'LowerBound', 0, 'UpperBound', 100);
b = optimvar('b', 'LowerBound', 0, 'UpperBound', 100);
x1 = optimvar('x1', 'LowerBound', 0, 'UpperBound', 10);
x2 = optimvar('x2', 'LowerBound', 0, 'UpperBound', 10);
f = a*(x - x1)^2 + 10; % this won't work because it mixes syms and optimvar
g = b*(x - x2)^2 + 2; % this also won't work, same reason
% constraints to make sure that the tangent points x-coordinates are x1_tan and x2_tan
prob.Constraints.eq1 = subs(diff(f, x), x, x1_tan) == subs(diff(g, x), x, x2_tan); % tangent line slopes are the same
prob.Constraints.eq2 = subs(diff(f, x), x, x1_tan)*x1_tan - subs(f, x, x1) == subs(diff(g, x), x, x2_tan)*x2_tan - subs(g, x, x2); % tangent line y-axis intercepts are the same
prob.Objective = sqrt((x_tan - x1)^2 + (y_tan - x2)^2); % the goal is to minimize this function
0 commentaires
Réponse acceptée
Walter Roberson
le 7 Déc 2020
f = a*(x - x1)^2 + 10; % this won't work because it mixes syms and optimvar
g = b*(x - x2)^2 + 2; % this also won't work, same reason
Use symbolic X X1 X2 for f and g. Take the derivative. subs any constant. matlabFunction with vars [X X1 X2] to get a numeric function. Now pass in optimvar variables to get out an optimization expression to use. That is, I expect that you will need an additional optimization variable named x
5 commentaires
Walter Roberson
le 8 Déc 2020
You have to pass in the options
https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.solve.html#namevaluepairarguments
Plus de réponses (1)
Matt J
le 7 Déc 2020
Modifié(e) : Matt J
le 7 Déc 2020
The calculus needed to take the derivatives in your constraint expressions seems pretty trivial. Do you really need to go through the Symbolic Toolbox at all?
prob.Constraints.eq1 = 2*a*(x1_tan-x1) == 2*b*(x2_tan-x2)
prob.Constraints.eq2 = 2*a*(x1_tan-x1)*x1_tan - 10 == 2*b*(x2_tan-x2)*x2_tan - 2;
prob.Objective = (x_tan - x1)^2 + (y_tan - x2)^2; % omit the square root
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!