MATLAB Answers

Correspondence between optimization outputs and symbolic variables

28 ビュー (過去 30 日間)
ali akbar
ali akbar 2021 年 2 月 12 日
コメント済み: John D'Errico 2021 年 2 月 12 日
I have solved an optimization problem with five symbolic variables. I don't know which optimization output corresponds to which variable.
syms p q theta phi xi
fun=q*cos(phi)^2)/4 + (p*sin(phi)^2)/4 + (p*cos(theta/2)^2)/8 - (p*cos(xi)^2*cos(xi)^2)/4-0.238
gg=matlabFunction(fun)
funny=@(x)gg(x(1),x(2),x(3),x(4),x(5));
opts=optimoptions(@fmincon,'Algorithm','interior-point');
problem = createOptimProblem('fmincon','x0',[0 0 0 0 0],'objective',funny,'lb',[],'ub',[],'options',opts)
result=run(gs,problem)
The code works fine and the output is
result= 0.6538 0.4783 0.2654 -1.2536 0.3538
Now I don't know which ouput value of result corresponds to which variable? Any help will be appreciated.

採用された回答

Walter Roberson
Walter Roberson 2021 年 2 月 12 日
編集済み: Walter Roberson 2021 年 2 月 12 日
syms p q theta phi xi
fun = q*cos(phi)^2)/4 + (p*sin(phi)^2)/4 + (p*cos(theta/2)^2)/8 - (p*cos(xi)^2*cos(xi)^2)/4-0.238
funny = matlabFunction(gg, 'vars', {[p, q, theta, phi, xi]});
Order of the variables on output would then be p, q, theta, phi, xi
  2 件のコメント
Walter Roberson
Walter Roberson 2021 年 2 月 12 日
Somewhere between the syms and the matlabFunction you redefined one of the names to not be a symbolic variable. You should exclude it from the list of variables.

サインインしてコメントする。

その他の回答 (1 件)

John D'Errico
John D'Errico 2021 年 2 月 12 日
The problem lies in your code. First, you have an obvious error in the code you show. I'll guess you just used too many or too few parens in the first term for fun. I'll delete the extra right paren on this term:
q*cos(phi)^2)/4
as it is spurious. But now look at the expression:
syms p q theta phi xi
fun=q*cos(phi)^2/4 + (p*sin(phi)^2)/4 + (p*cos(theta/2)^2)/8 - (p*cos(xi)^2*cos(xi)^2)/4-0.238
fun = 
Next, you write this line:
gg=matlabFunction(gg)
which is completely meaningless in context, since gg is undefined. Sigh. If you want help, one would think you would at least test the code you show to insure it does what you claim it does This forces me to make wild guasses as to what you mean.
If looks like you may have wanted to write
gg=matlabFunction(fun)
gg = function_handle with value:
@(p,phi,q,theta,xi)(q.*cos(phi).^2)./4.0-(p.*cos(xi).^4)./4.0+(p.*sin(phi).^2)./4.0+(p.*cos(theta./2.0).^2)./8.0-1.19e+2./5.0e+2
So matlabFunction created a function handle, which you then go on to use in an optimization. (MAYBE that is what you did. But at this point, I'm just making wild random guesses.)
Do you see the order of the parameters in fun? matlabFunction chose them in what looks like alphabetical order, if you do not tell it to do otherwise. But you can force it to use the order you wish. For example:
gg=matlabFunction(fun,'vars',{p,q,theta,phi,xi})
gg = function_handle with value:
@(p,q,theta,phi,xi)(q.*cos(phi).^2)./4.0-(p.*cos(xi).^4)./4.0+(p.*sin(phi).^2)./4.0+(p.*cos(theta./2.0).^2)./8.0-1.19e+2./5.0e+2
And now your optimization will use these variables in the expected order.
  4 件のコメント
John D'Errico
John D'Errico 2021 年 2 月 12 日
"... but isn't. The order can get really strange if you have a mix of variables "
Yes. I realize that the symbolic toolbox sometimes does strange things with variable ordering as well as ordering of terms. My claim about alphabetical order is not really pertinent except that it clearly chooses the order in some obscure way, but more importantly, that you can override the choice.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by