MATLAB: Fminunc stopped because it cannot decrease the objective function along the current search direction.

decreasefminuncfminunc stoppedoptimizationOptimization Toolboxoptimtool

Hi, I'm trying to use fminunc function with the gradient but it doesn't work properly.
my start point is: [1.5;-2.5]
this is my objective function:
function [f, g, H] = rosenboth(x)
% Calculate objective f
a = 0.5;
b = -1.5;
f = (1 - x(1) + a)^2 + 100 * (x(2) - b - (x(1) - a)^2)^2;
if nargout > 1 % gradient required
g = [2 * (-200 * (x(1) - 1) * (-x(1)^2 + 2 * x(1) + x(2)) + x(1) - 2);
200 * (-x(1)^2 + 2 * x(1) + x(2))];
if nargout > 2 % Hessian required
H = [1200 * x(1)^2 - 2400 * x(1) - 400 * x(2) + 802, -400 * (x(1) - 1);
-400 * (x(1) - 1), 200];
end
end
and that's the generated code from optimtool
function [x,fval,exitflag,output,grad,hessian] = fmin_gradient(x0,OptimalityTolerance_Data,StepTolerance_Data)
%% This is an auto generated MATLAB file from Optimization Tool.
%% Start with the default options
options = optimoptions('fminunc');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'OptimalityTolerance', OptimalityTolerance_Data);
options = optimoptions(options,'FunctionTolerance', OptimalityTolerance_Data);
options = optimoptions(options,'StepTolerance', StepTolerance_Data);
options = optimoptions(options,'PlotFcn', @optimplotfval);
options = optimoptions(options,'Algorithm', 'quasi-newton');
options = optimoptions(options,'SpecifyObjectiveGradient', true);
options = optimoptions(options,'Hessian', 'off');
[x,fval,exitflag,output,grad,hessian] = fminunc(@rosenboth,x0,options);
end

Best Answer

Please run with the CheckGradients option set to true. I think that you will find that you did not calculate the derivatives correctly.
Alan Weiss
MATLAB mathematical toolbox documentation