How to pass extra parameter as a symbolic variable to objective and constraint functions?

1 view (last 30 days)
I have an objective function like this:
syms State1 State2 State3 State4 State5 State6 State7 State8 State9 State10 State11
States = [State1; State2; State3; State4; State5; State6; State7; State8; State9; State10; State11];
a = 20;
g = 9.8;
function [ f, gradf, hessf, fh] = obj( States, U_target, g)
f = -a * States(10) / g;
gradf = jacobian(f, States).';
hessf = jacobian(gradf, States);
fh = matlabFunction(f, gradf, hessf, 'vars', {States});
end
Now I want to keep States as the vector I'm going to optimize but let "a" be a "symbolic" parameter, thus for different values of "a", I don't need to generate the symbolic objective and Jacobian every time when "a" has a different value.
Could you help me with this? Thank you!
  1 Comment
Walter Roberson
Walter Roberson on 25 May 2015
Merging from a duplicate question, it was asked:
I'm using fmincon to do nonlinear optimization. In order to make program more robust and faster, I used symbolic toolbox to generate objective and constraint forcing functions and corresponding gradient and Jaccobians. However, there is one parameter (let's call it A here, a is a real and fixed number when fmincon is called) in these objective, constraint, and corresponding gradient and Jaccobians. I know how to deal the problem when A is 20, 30, 40 ect.. But in my main program where fmincon is called, I need A to change, for example, A = 20:1:30. And I don't want to generate the symbolic functions every time when A changes a value, and I don't want to use "subs" either since "subs" is very time consuming. Could anyone give me a help?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 May 2015
This kind of thing is normally straight-forward by doing something like
syms a
inside the function, and adding a to the list of input variables in the 'vars' parameter of the matlabFunction call. Then to hide the extra parameter for any given series of calls you would normally parameterize via something like (presuming you had stored the result into "fh")
a = 33;
thisa_fh = @(x,state1,state2,state3...) fh(x,state1,state2,state3...,a);
and then pass in thisa_fh where you would have passed fh.
However, your returned anonymous function has several outputs, and working with multiple outputs from anonymous functions is a challenge. When a function returns multiple outputs and the function value is used in an expression, the "rule" is that all but the first output value are discarded. In theory when you make that extra layer of anonymous call, that should invoke the value-discarding semantics, because the value is being "used" as the return result rather than being assigned directly. So adding this extra layer "shouldn't" be possible.
It turns out though, in practice, in this limited context where the result of a function returned directly upwards, the multiple outputs are preserved! So in practice you can what I described above and it all magically works out.
Example:
bar = @(varargin) unique(varargin{:});
foo = @(X) bar(X,'rows');
[a, b, c] = foo([3 5;3 5;7 9])
The foo call is slamming in an extra parameter to the bar handle that is not just a plain function pointer such as foo = @unique might have been, so in theory we are returning an expression and in theory we should be invoking those semantics of discarding extra outputs. But the outputs get preserved and the number-of-outputs information is being carried right down. It's magic! :(
IMHO Mathworks needs to fix the model to place returning multiple outputs under control rather than being magic propagated from assignment semantics.
I would be interested to see the generated anonymous function from applying matlabFunction on multiple expressions (which is defined to return multiple outputs); I cannot think at the moment of the mechanism to return multiple outputs from anonymous functions, cannot think at the moment how to say which value goes to which output. Not unless there are some hacks with subsasgn to varargout or something like that...

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!