Correspondence between optimization outputs and symbolic variables

3 views (last 30 days)
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
problem = createOptimProblem('fmincon','x0',[0 0 0 0 0],'objective',funny,'lb',[],'ub',[],'options',opts)
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.

Accepted Answer

Walter Roberson
Walter Roberson on 12 Feb 2021
Edited: Walter Roberson on 12 Feb 2021
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
Walter Roberson
Walter Roberson on 12 Feb 2021
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.

Sign in to comment.

More Answers (1)

John D'Errico
John D'Errico on 12 Feb 2021
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:
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:
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 = function_handle with value:
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 = function_handle with value:
And now your optimization will use these variables in the expected order.
John D'Errico
John D'Errico on 12 Feb 2021
"... 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by