Assign symbolic variables in objective function as either constants or optimisation variables
Show older comments
I am trying to optimise an objective function with a mixture of symbolic variables. Some of them are optimisation variables and others are constants. Say I have an objective function to be minimised f(x)=ax^2 + bx +c defined using symbolic variables x, a, b, c. (This is an example - my objective function has more variables and is much longer).
If a,b,c are different constants at each time step I evaluate f(x) at each timestep and minimise it for x . This requires an optimisation for every timestep. Is it possible to define x as an optimisation variable in the objective function and a,b,c as symbolic constants? Then I could minimise the function for x as a function of a,b,c. Then I only need to minimise the generic function once and just substitute values of a,b,c into the solution for each timestep which is obviously much quicker?
Here is an example of my problem:
f = a*x^2 + b*x +c;
gradf = jacobian(f,x).';
hessf = jacobian(gradf,x);
fh = matlabFunction(f,gradf,hessf,'vars',{[x;a;b;c]});
options = optimoptions('fminunc', ...
'SpecifyObjectiveGradient', true, ...
'HessianFcn', 'objective', ...
'Algorithm','trust-region', ...
'Display','off');
x0 = 0;
% I think the issue arises here when I only want to assign a value for one of my symbolic variables (x = x0)
[xfinal,fval,exitflag,output] = fminunc(fh,x0,options); % <- This line errors
Many thanks!
Accepted Answer
More Answers (0)
Categories
Find more on Logical 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!