MATLAB: Do functions within the Optimization Toolbox sometimes return the wrong answer stating convergence where there is no minimum

convergeinnacurrateoptimizationOptimization Toolboxwrong

For example, I run the following code using some specified function myfunc.m:
options = optimset('Display','Iter');
x0 = [1 1];
x = fminunc(@myfunc, x0, options)
This results in the display:
Directional
Iteration Func-count f(x) Step-size derivative
1 2 6 1 0
Optimization terminated successfully:
Search direction less than 2*options.TolX
x =
1.0000 1.0000
There is actually no minimum at "[1 1]" and the directional derivative is not "0". Why am I obtaining these incorrect results?

Best Answer

This will occur when myfunc.m is not sensitive to small changes. We find that the commands:
x1 = myfunc([1 1])
x2 = myfunc([1 1]+1e-8)
result in the exact same answer.
x1 - x2
ans =
0
Therefore, we must set the "DiffMinChange" property of the "options" structure using the OPTIMSET function so that the Optimization Toolbox functions account for the sensitivity of the specified function.
myfunc([1 1]) - myfunc([1 1]+1e-5)
results in a nonzero answer.
ans =
-1.2016e-004
Therefore, the function specified within myfunc.m can be minimized using:
options = optimset('DiffMinChange',1e-5);
x0 = [1 1];
x = fminunc(@myfunc,x0,options);