How to solve optimization problems when the objective function includes a symbolic function
5 views (last 30 days)
Show older comments
Hi,
I'm trying to use fmincon to optimize a certain function that I will call Func1, subject to several inequality constraints and one nonlinear constraint Func3. The Func1 file calls another function Func2 several times. Func2 solves a system of nonlinear equations using vpasolve. When I try to run it I receive the following error:
"Unable to convert 'optim.problemdef.OptimizationExpression' to 'sym'"
Apparently fmincon is unable to optimize a function that uses sym. What options are available to optimize such nonlinear functions?
The functions are detailed below:
function [var1Opt, var2Opt, var3Opt] = OptimizeFunc1(input1, input2, input3, input 4)
var1=optimvar('var1')
var2=optimvar('var2')
var3=optimvar('var3')
fun=@Func1
x0 = [1 1 1]
lb = [0 0 0]
ub = [3 3 9]
A=[];
b=[];
Aeq = [];
Beq = [];
nonlcon = @Func3;
x = fmincon(fun(var1, var2, var3, input1, input2, input3, input4), x0, A, b, Aeq, Beq, lb, ub, nonlcon)
The function that I am trying to optimize looks like this:
function X = Func1(var1, var2, var3, input1, input2, input3, input4)
Func2in = (var1+var2)/(var3)-(input1*input2)/(input3-input4)
Func2out = Func2(Func2in)
X = Func2in+Func2out
Now for Func2, which is giving me the headache:
function Func2out = Func2(Func2in)
C1=20
C2=30
syms Func2Var1 Func2Var2
eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1;
eqn2 = C2*Func2Var2/C1 == Func2in
sol = vpasolve([eqn1 eqn2], [FuncVar1, FuncVar2];
Func2out=double(Func2Var1)*double(Func2Var2)
These aren't the actual functions I've used but they are representative.
I've seen solutions to problems like this that used matlabFunction but I'm not entirely sure where I would call it from. Any help is very much appreciated.
0 Comments
Answers (2)
Torsten
on 26 Jan 2023
Moved: Torsten
on 26 Jan 2023
The usual way to deal with this problem is to define Func2Var1 and Func2Var2 as additional optimization variables and to specify the two equations
eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1;
eqn2 = C2*Func2Var2/C1 == Func2in
as nonlinear constraints in nonlcon.
Paul
on 27 Jan 2023
Hi @NJ2Cali
I was able to get fmincon to run to completion using an objective function that uses syms. I changed Func2 to something I could understand better, but just trying to show it's feasible to use syms.
x = fmincon(@(x) Func1(x(1),x(2),x(3),x(4),x(5),x(6),x(7)),1:7,eye(7),1e6*ones(7,1))
function X = Func1(var1, var2, var3, input1, input2, input3, input4)
Func2in = (var1+var2)/(var3)-(input1*input2)/(input3-input4);
Func2out = Func2(Func2in);
X = Func2in+Func2out;
end
function Func2out = Func2(Func2in)
C1 = 20;
C2 = 30;
syms Func2Var1 Func2Var2
%eqn1 = C1*Func2Var1^Func2Var2/(Func2in-Func2Var2) == Func2Var1*C1
%eqn2 = C2*Func2Var2/C1 == Func2in
eqn1 = Func2Var1 + Func2Var2 == C1 + Func2in;
eqn2 = Func2Var1 - Func2Var2 == C2 + Func2in;
sol = vpasolve([eqn1 eqn2], [Func2Var1, Func2Var2]);
Func2out=double(sol.Func2Var1)*double(sol.Func2Var2);
end
3 Comments
Torsten
on 31 Jan 2023
Edited: Torsten
on 31 Jan 2023
The point is that you seem to use the problem-based optimization approach. I don't have another explanation for your error message
"Unable to convert 'optim.problemdef.OptimizationExpression' to 'sym'"
If you used the solver-based approach (as Paul does in his example), your function "Func2" would work without problems.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!