Borrar filtros
Borrar filtros

function with two variables

3 visualizaciones (últimos 30 días)
ali akbar
ali akbar el 1 de Mayo de 2020
Respondida: Walter Roberson el 2 de Mayo de 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.

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (1)

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

Categorías

Más información sobre Get Started with Optimization Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by