Can I supply a gradient to fmincon when my objective function is anonymous?

5 次查看(过去 30 天)
I am using fmincon and would like to supply a gradient for the objective function to speed up convergence. The trouble is I use an anonymous objective function so I can pass parameters to my real objective function. I believe the anonymous function only picks up the first argument of my real objective function, which is the function value and not the gradient. The basic structure of my code is:
if true
%%%%%%%%%%%%%%%%%%%
[f,gradf]=realobjective(parameters,x]
f=blah;
gradf=blah;
end
%%%%%%%%%%%%%%%%%%%
options=optimoptions(@fmincon,'Algorithm','active-set','MaxFunEvals',1E5,'MaxIter',1E5,'TolFun',1E-6,'TolX',1E-6,'GradConstr','on','GradObj','on');
xguess=0;
func=@(x)realobjective(parameters,x)
argmin=fmincon(func,xguess,[],[],[],[],0,1000,nonlconstr,options)
end
So I think fmincon is not using the gradient I have defined in the real objective function. I'd be incredibly grateful for any advice.
David
  1 个评论
Rustem Devletov
Rustem Devletov 2019-4-26
Hey, friend! I'm a fourth year student in Russia, currently writing my thesis. My supervisor asked me to provide gradient to fmincon
I have triend smth like this
function F = rosenbrockwithgrad(x)
F = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
But he said that he wants it to be found in another file. How can I implement this? How do I find gradient in one file and pass it to another one?

请先登录,再进行评论。

采纳的回答

Matt J
Matt J 2014-8-13
编辑:Matt J 2014-8-13
What you've posted looks fine. Call nargout inside realobjective (e.g., at a breakpoint), to test whether fmincon is calling it with two output arguments.
  5 个评论
Matt J
Matt J 2014-8-13
Did you try running with 'DerivativeCheck' set to 'on' to validate your gradient calculation? Sounds like your constraint gradient calculation is working okay, but the objective function gradient calculation might have errors.
Rustem Devletov
Rustem Devletov 2019-4-26
Hey, friend! I'm a fourth year student in Russia, currently writing my thesis. My supervisor asked me to provide gradient to fmincon
I have triend smth like this
function F = rosenbrockwithgrad(x)
F = 100*(x(2) - x(1)^2)^2 + (1-x(1))^2;
if nargout > 1 % gradient required
g = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1));
200*(x(2)-x(1)^2)];
end
But he said that he wants it to be found in another file. How can I implement this? How do I find gradient in one file and pass it to another one?

请先登录,再进行评论。

更多回答(1 个)

DavidZ
DavidZ 2014-8-13
Thanks both. It's very mysterious; when I turn the gradient calls off fmincon does converge (slowly) to the right answer (I know it's right as for certain parameter values I can work out an analytical solution to compare to the numerical solution fmincon gives) but when I turn the gradient calls on it never converges. I tried the debugging tip Matt helpfully suggested and fmincon is calling withing nargout 2 some of the time, but not all of it. Any thoughts? Thanks again for your help.

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by