# Correspondence between optimization outputs and symbolic variables

1 view (last 30 days)

Show older comments

ali akbar
on 12 Feb 2021

Commented: John D'Errico
on 12 Feb 2021

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.

##### 0 Comments

### Accepted Answer

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

##### 2 Comments

Walter Roberson
on 12 Feb 2021

### More Answers (1)

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:

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

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)

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})

And now your optimization will use these variables in the expected order.

##### 4 Comments

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.

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!