How to solve optimization problems when the constraints have derivative (symbolic) functions
Show older comments
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
Accepted Answer
More Answers (1)
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 Comment
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!