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

optimcaroptimizationsyms

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

Best Answer

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