MATLAB: Fmincon Gradients not Matching with Mine

constraintderivativederivativecheckfmincongradientMATLABnonlinearOptimization Toolboxtypicalx

Having an issue with fmincon… The nonlinear constraint gradients that I compute do not match with those created by fmincon according to the dialog that appears when DerivativeCheck is 'on'. The issue is that fmincon is claiming that my derivative and its derivative are not the same within a tolerance of 1e-6. The difference it claims is usually 3e-3 to 1e-5. And the strange part is that sometimes the derivative that fmincon computes varies from run to run. Sometimes the derivatives match within the tolerance, most of the time they don't. Here is the equality constraint I am using:
ceq(1) = x(1) * e(x(2)) - x(2)
where e is a griddedInterpolant that is a function of x(2). For example, the data that generates the griddedInterpolant could be as follows:
E = [0 2.0 1.0]
P = [0 0.5 1.0]
e = griddedInterpolant(P, E, 'linear', 'linear')
So, I estimate the gradient of the equality constraint using a simple "forward" method as follows:
deltaX = 1e-6; %Small step
eForward = e(x(2) + deltaX); %Value of e at x(2) plus the small step
gradceq(1,1) = e(x(2)) %Gradient of ceq with respect to x(1)
gradceq(1,2) = x(1)*(eForward-e)/deltaX - 1 %Gradient of ceq with respect to x(2)
I have tried adjusting the step that fmincon uses to compute the gradient with 'TypicalX' to match mine, and no dice. I have tried central differences as well. Here is the dialog that it shows me:
Nonlinear equality constraint derivatives:
Maximum relative difference between user-supplied
and finite-difference derivatives = 0.00354916.
User-supplied constraint derivative element (4,1): -0.996447
Finite-difference constraint derivative element (4,1): -0.999996

Best Answer

Linear griddedInterpolants are not differentiable at the knots, so you are quite likely to get arbitrary discrepancies near them depending on the particular finite difference parameters used by each test method.
Since twice continuous differentiability is required for most fmincon algorithms, use a 'spline' griddedInterpolant instead.