Azzera filtri
Azzera filtri

function with two variables

3 visualizzazioni (ultimi 30 giorni)
ali akbar
ali akbar il 1 Mag 2020
Risposto: Walter Roberson il 2 Mag 2020
I have a long symbolic function which is evaluated inside matlab and contains two variables theta and xi.
realG1=((1.0*(cos(theta) - sin(theta)*sin(xi)*1.0*i)*(cos(theta)*(0.008866522 - 0.02124593*i) + 0.00765004*cos(xi)*sin(theta) + sin(theta)*sin(xi)*(- 0.02124593 - 0.008866522*i)) - cos(xi)*sin(theta)*(0.06971876*cos(theta) + cos(xi)*sin(theta)*(0.008866522 + 0.02124593*i) - sin(theta)*sin(xi)*0.06971876*i) + 0.03551045 + 0.008096911*i)^2 )
My code is following
g= matlabfunction(realG1)
rng default
gs=GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',[0,0],'objective',g,'lb',[0,0],'ub',[],'options',opts);
[theta,xi,output] = run(gs,problem)
when I run it for only theta(removing xi on the last line), it minimizes just fine. But with two variables, it says not ''Not enough input arguments''. Can anyone help.

Risposta accettata

Ameer Hamza
Ameer Hamza il 1 Mag 2020
Change the code like this
g = matlabfunction(realG1)
obj_fun = @(x) g(x(1),x(2)); %<<=== add this line
rng default
gs=GlobalSearch;
opts = optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',[0,0],'objective',obj_fun,'lb',[0,0],'ub',[],'options',opts);
[theta,xi,output] = run(gs,problem) % ^ change to obj_fun
fmincon only accepts a function handle with input if your function has two inputs, the use 'x' as a vector with two elements.
  2 Commenti
ali akbar
ali akbar il 2 Mag 2020
Thank you ameer. You're a life saviour.
Best
Ameer Hamza
Ameer Hamza il 2 Mag 2020
I am glad to be of help.

Accedi per commentare.

Più risposte (1)

Walter Roberson
Walter Roberson il 2 Mag 2020
Change
g = matlabfunction(realG1)
to
g = matlabfunction(realG1, 'vars', {[theta, xi]});
Now you do not need to modify your objective function.

Categorie

Scopri di più su Get Started with Optimization Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by