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

2 次查看(过去 30 天)
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?

采纳的回答

Matt J
Matt J 2019-6-24
编辑:Matt J 2019-6-24
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 个评论
Matt J
Matt J 2019-6-28
编辑:Matt J 2019-6-28
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 2019-6-28
编辑:sxj 2019-6-28
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Optimization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by