Correspondence between optimization outputs and symbolic variables

1 Ansicht (letzte 30 Tage)
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 12 Feb. 2021
Bearbeitet: Walter Roberson am 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 Kommentare
ali akbar
ali akbar am 12 Feb. 2021
Thanks for your reply walter.
Matlab is throwing the following error now.
Variable names must be valid MATLAB variable names.
if I use code like this
funny = matlabFunction(gg)
Code is working fine.
Walter Roberson
Walter Roberson am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

John D'Errico
John D'Errico am 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.
  4 Kommentare
John D'Errico
John D'Errico am 12 Feb. 2021
Bearbeitet: John D'Errico am 12 Feb. 2021
Yes, but the problem is you are not showing us the real code you wrote. And the code you show us is full of errors, so we can only guess what you did.
It looks like you are haphazardly changing variables around, trying to make something work, to the point where you have no idea what you wrote, and you have no idea what is in the variables you do have.
Can we help you in that, if you don't actually show us the code you are trying to use? If the code you do show is not what you are trying to do, but only a fragmented part with missing variables and errors in your own typing when you insert it here?
You need to start at the beginning. Verify what you have in each variable. Go slowly. Then fix each problem as it arises.
When you have an error, and you want help, don't just tell prople the generic error message, thus "Variable names must be valid MATLAB variable names.".
Tell people the COMPLETE eror message, everything in red. That tells us the line of code that caused the error. As importantly, then you need to look at the variables in that line of code. In this case, MATLAB sees a problem with at least one of your variables. So LOOK AT THEM! Verify what the variables are in that line of code. What class are they? Use the whos function to tell you. Don't assume you know what you have. Prove it to yourself.
Again, go slowly. Learn to walk before you run. You are trying to fly at jet speed on a freeway, but when you do that, you crash into random things at every opportunity. so take ONE step at a time. Yes, it may seem as if the code takes you longer to write. But is this really going quickly for you, as we try to debug your code without seeing any real code?
John D'Errico
John D'Errico am 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.

Melden Sie sich an, um zu kommentieren.


Mehr zu Get Started with Optimization Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by