How to solve optimization problems when the constraints have derivative (symbolic) functions

15 次查看(过去 30 天)
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

采纳的回答

Walter Roberson
Walter Roberson 2020-12-7
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 个评论
Walter Roberson
Walter Roberson 2020-12-8
You have to pass in the options
https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.solve.html#namevaluepairarguments

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2020-12-7
编辑:Matt J 2020-12-7
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
  1 个评论
Jen W
Jen W 2020-12-7
编辑:Jen W 2020-12-7
Thanks. My example is a very simplified version of the actual problem I am trying to solve. The actual function derivative is complicated.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by