Convert a symbolic function in to optimization function

Hello dear Matlab Community,
I need to convert the following code automatically in the new form in order to optimize it automatically. This needs be done a couple of hundret times so there is no chance of doing it manually.
This is the symbolic function:
M1=10*sin(x1)*sin(x2) - 2*cos(x1)*sin(x2) - 12*cos(x2) - 3
This is how i would need in in order to work:
M1=@(x)10*sin(x(1))*sin(x(2)) - 2*cos(x(1))*sin(x(2)) - 12*cos(x(2)) - 3
Thank you for your help.
Greetings Richie

 Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 2 Août 2023
Modifié(e) : Dyuman Joshi le 2 Août 2023
(Assuming you already have the symbolic toolbox) Use matlabFunction -
syms x1 x2
%Original symbolic expression
M1=10*sin(x1)*sin(x2) - 2*cos(x1)*sin(x2) - 12*cos(x2) - 3
M1 = 
%Converting the symbolic expression to a function handle
M2 = matlabFunction(M1,"Vars",{symvar(M1)})
M2 = function_handle with value:
@(in1)cos(in1(:,2)).*-1.2e+1-cos(in1(:,1)).*sin(in1(:,2)).*2.0+sin(in1(:,1)).*sin(in1(:,2)).*1.0e+1-3.0
Testing on some values -
%#1
val = [0 pi/2];
subs(M1,[x1 x2],val)
ans = 
M2(val)
ans = -5.0000
%#2
val = [pi pi];
subs(M1,[x1 x2],val)
ans = 
9
M2(val)
ans = 9
%#3
val = rand(1,2);
double(subs(M1,[x1 x2],val))
ans = -13.7025
M2(val)
ans = -13.7025

4 commentaires

Thank you for your fast help, but if i want to insert in in the opimization function it doesnt work.
anonrosen = @(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2);
opts = optimoptions(@fmincon,Algorithm="interior-point");
rng default % For reproducibility
problem = createOptimProblem("fmincon",...
x0=randn(2,1),...
objective=anonrosen,...
lb=[-2;-2],...
ub=[2;2],...
options=opts);
gs = GlobalSearch;
[x,fval2] = run(gs,problem)
The function needs to be replaced with the following part in the first line
@(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2)
But if i replace it with your code it doesnt work, but if i write it manually it does
syms x1 x2
%Original symbolic expression
M1 = 10*sin(x1)*sin(x2) - 2*cos(x1)*sin(x2) - 12*cos(x2) - 3;
%Converting the symbolic expression to a function handle
M2 = matlabFunction(M1,"Vars",{symvar(M1)});
M3 = @(x) M2(transpose(x));
opts = optimoptions(@fmincon,Algorithm="interior-point");
rng default % For reproducibility
problem = createOptimProblem("fmincon",...
x0=randn(2,1),...
objective=M3,...
lb=[-2;-2],...
ub=[2;2],...
options=opts);
gs = GlobalSearch;
[x,fval2] = run(gs,problem)
GlobalSearch stopped because it analyzed all the trial points. All 9 local solver runs converged with a positive local solver exit flag.
x = 2×1
-1.3734 0.7044
fval2 = -18.7480
You need to supply a row vector, see my comments below
syms x1 x2
in = 100*(x2 - x1^2)^2 + (1-x1)^2;
out = matlabFunction(in,"Vars",{symvar(in)});
%anonrosen = @(x)(100*(x(2) - x(1)^2)^2 + (1-x(1))^2);
opts = optimoptions(@fmincon,Algorithm="interior-point");
rng default
problem = createOptimProblem("fmincon",...
%%corrected to give a row vector as an input
x0=randn(1,2),...
%%objective is the output of the conversion from symbolic
objective=out,...
lb=[-2;-2],...
ub=[2;2],...
options=opts);
gs = GlobalSearch;
[x,fval2] = run(gs,problem)
GlobalSearch stopped because it analyzed all the trial points. All 16 local solver runs converged with a positive local solver exit flag.
x = 1×2
1.0000 1.0000
fval2 = 2.1093e-11
Thank you for answers, it really helped me alot,
without you guys I wouldnt know what to do.
Have a nice day:)

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by