# Set Limits for x in optimization using fminunc

14 views (last 30 days)
Sebastian Körner on 15 Dec 2020
Edited: John D'Errico on 15 Dec 2020
Hi, i am optimizing some parameters in my Simulink model by using the matlab function fminunc.
this works perfectly fine:
func = @(x) Main_run(x);
x0 = [5];
[x,fval] = fminunc(func,x0)
now i want to optimize the upper limit of a saturation block. but while running my optimization, matlab trys values for x that are below the lower limit of the saturation block, which results in the following error
Lower limit must be less than or equal to upper limit in 'MMSiL/ISG-Regelung/ISG Controller_Drehzahlregelung/Saturation'
So i am searching for a way to set a lower limit for the value assigned to the variable x. i tryed by using the options of fminunc but i dont think thats the right wy to do it.
x = fminunc(fun,x0,options)

Walter Roberson on 15 Dec 2020
Never. fminunc literally has to do with UNConstrained. No chance of putting constraints on it.

John D'Errico on 15 Dec 2020
Edited: John D'Errico on 15 Dec 2020
If you have fmincon, then you also have fmincon so there cannot be a problem. This is not a question of how to get fminunc to solve your problem, but of using the correct tool.
That said, COULD you solve a constrained probem using fminunc? Of course. If you understand how I wrote fminsearchbnd, then you can do the same thing for fminunc. Thus you would need to essentially modify the objective function. In fminsearchbnd, I did that by creating a wrapper around the objective function between the optimizer (fminsearch in that case) and the objective. You can find fminsearchbnd on the file exchange to learn how I solved that problem. This does create a transformed problem, introducing nonlinearities that typically introduce multiple solutions to the problem, all of which are identical in objective. As such, those alternative solutions are irrelevant because you don't care which one you find. The introduced nonlinearities can be important and problematic though, because they change the shape of your objective.
Anyway, is that worth doing here? Are you kidding? Why in the name of god and little green apples would you go through the effort, when a better solution is already available and works quite well?
Just use fmincon. It is surely better to learn how to use all of the tools in your toolbox, than to find ways to force one tool to solve problems it is not designed to solve well.

R2019a

### Community Treasure Hunt

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

Start Hunting!