필터 지우기
필터 지우기

function with two variables

조회 수: 3 (최근 30일)
ali akbar
ali akbar 2020년 5월 1일
답변: Walter Roberson 2020년 5월 2일
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.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

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

카테고리

Help CenterFile Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by