Derivative of Function handle (To obtain the derivative of constraints in function fmincon)

1 view (last 30 days)
Hey, everyone, I'm using fmincon to solve the optimization problem under constraints. To increase the speed of calculation, I would like to provide the derivative matrix of the constraints. However, the constraint conditions are nonliner and complex, e.g as below.
where
the last one implies is also a function of . If I want to calculate the derivative of respect to , it will take a lot of efforts for me to calculate it by myself.
Does the MATLAB provide some tool to obtain the derivative of function handle, especially under using fmincon to solve the optimization problem ? Any suggestions to obtain the derivative of constraint conditions efficiently instead of calculating them by hand?

Accepted Answer

Matt J
Matt J on 24 Jun 2019
Edited: Matt J on 24 Jun 2019
You can use the Symbolic Math Toolbox to obtain the expression for the derivative, and then use matlabFunction() to convert from symbolic form to function form.
  22 Comments
Matt J
Matt J on 28 Jun 2019
Edited: Matt J on 28 Jun 2019
Change the first few lines in derivative_cons1,
Y = in1(1);
x1 = in1(2);
x2 = in1(3);
or make x_0 a row vector,
x_0 = [20,10,0.5];
sxj
sxj on 28 Jun 2019
Edited: sxj on 28 Jun 2019
Thanks for your quick reply, Matt. I follow your suggestion and it works well. But I think it is inconvenient to change derivative_cons1 during running the whole code. So make x_0 a row vector will be better.
I also tried the following way
cons = matlabFunction([],[f1;f2;f3],[],jacobian([f1;f2;f3]).','File','derivative_cons1','Vars',{[Y;x1;x2]});
x_0 = [20;10;0.5];
opt = fmincon(@(x)objective(x), x_0,[], [], [],[],lb,ub, @(x)cons(x), options);
where I change the vector in the option 'Vars' from 1X3 to 3X1. In this way, it also works.
So far I have solved the simple version of my optimization problem and will turn to my original one. Thank you very much for all the discussions above, especially to Matt and Walter. I hope the discussions will also help others.

Sign in to comment.

More Answers (0)

Categories

Find more on Optimization 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!