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

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

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 Comments

Thanks. When using symbolic x, x1, and x2, what do you do with a and b? Are they also symbolic? Also, if I add an additional optimization variable named x, what is its relation with x1_tan and x2_tan? I think that x is just a function variable (place holder) that needs to be substituted by x1_tan and x2_tan in the constraints...
Good point, a and b should be symbolic too (though with different names like A B so you can use the existing a b to replace them)
I figured it out based on your suggestion of adding an additional opt variable x. Actually, two of them are needed, one for f and one for g. Thank you very much.
I have one more question regarding this script. When solving the system, I have the error: "fmincon stopped because it exceeded the function evaluation limit (3e3)". There are some solutions online to increase the evaluation number, like in the code below. However, none of them can overwrite the default MaxFunctionEvaluations for me. Do you know if there is another way to overwrite the evaluation number? Thanks again.
options = optimoptions(@fmincon, 'MaxFunctionEvaluations', 1e4) % no effect
options.MaxFunctionEvaluations = 1e4 % no effect
options = struct('MaxFunctionEvaluations', 1e4) % no effect
[sol, dist] = solve(prob, x0)
You have to pass in the options
https://www.mathworks.com/help/optim/ug/optim.problemdef.optimizationproblem.solve.html#namevaluepairarguments

Sign in to comment.

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

Thanks. My example is a very simplified version of the actual problem I am trying to solve. The actual function derivative is complicated.

Sign in to comment.

Categories

Asked:

on 7 Dec 2020

Commented:

on 8 Dec 2020

Community Treasure Hunt

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

Start Hunting!