Effacer les filtres
Effacer les filtres

function with two variables

3 vues (au cours des 30 derniers jours)
ali akbar
ali akbar le 1 Mai 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.

Réponse acceptée

Ameer Hamza
Ameer Hamza le 1 Mai 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 commentaires
ali akbar
ali akbar le 2 Mai 2020
Thank you ameer. You're a life saviour.
Best
Ameer Hamza
Ameer Hamza le 2 Mai 2020
I am glad to be of help.

Connectez-vous pour commenter.

Plus de réponses (1)

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

Catégories

En savoir plus sur Get Started with Optimization Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by