フィルターのクリア

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 Center および File ExchangeGet 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