I am trying to solve numerically a constrained optimisation problem in MATLAB, and I am wondering how the fmincon
function calculates gradients when one isn't provided. Does anyone here know, or know how I might be able to find out?
Running the optimisation problem takes more time than I'd like it to, so I was hoping to speed it up by providing the gradient analytically. However, when I do this, I end up with wildly different solutions that seem less plausible than the solution that MATLAB generates when I do not provide the gradient.
As a check, I used the CheckGradients
option in fmincon
. Predictably, the gradient I provided did not pass this test. The same happens even when I set FiniteDifferenceType
to 'central'
. One obvious explanation is that the derivatives I provided truly are incorrect. However, I've gone over them several times and I'm fairly certain they are not.
As a sanity check, I tried to calculate the gradient of my objective numerically, using gradient
, which the documentation suggests is calculated using finite differences. Unfortunately, the output of gradient
is nowhere near the gradient calculated by fmincon
.
I'm really not sure what's going on, and I'd appreciate it if anyone can help shed light on this situation.
Edit: I'm more interested in why fmincon
and gradient
produce different numerical derivatives, despite ostensibly both being calculated using finite differences. Unless I've misunderstood the options, the difference persists even when I give them the same finite difference step size.
Also, in case it's relevant:
I am actually using GlobalSearch
(which then calls fmincon
) to solve a constrained optimisation problem of the form
\begin{equation*} \begin{aligned} & \underset{\mathbf p, \mathbf q}{\max} & & V(\mathbf p, \mathbf q) - C(\mathbf p, \mathbf q) \\ & \ \ \text{s.t.} & & \sum_{i=1}^n p_i = 1 \\ & & & \sum_{i=1}^n q_i = 1. \end{aligned} \end{equation*}
$V(\mathbf p, \mathbf q)$ is actually the value function of some linear programming problem, and I've written a script that invokes linprog
to calculate the value of my objective. $(\mathbf p, \mathbf q)$ also enters linearly into the objective and constraints in that problem. $C$, however, is non-linear.