MATLAB: Do I receive a “Matrix is singular” warning when using the FMINCON function in the Optimization Toolbox 4.0 (R2008a)

Optimization Toolbox

When I use the FMINCON function, I sometimes receive the following warning,
Warning: Matrix is singular, close to singular or badly scaled.
Results may be inaccurate. RCOND = NaN.
> In compdir at 29
In optim\private\qpsub at 376
In optim\private\nlconst at 736
In fmincon at 696
In myFunction at 19
Warning: Rank deficient, rank = 0, tol = NaN.
> In optim\private\qpsub at 612
In optim\private\nlconst at 736
In fmincon at 696
In myFunction at 19

Best Answer

This warning may be displayed if for certain values of input parameters "x", the objective function returns a NaN value (not a number). Because of this, FMINCON is unable to determine the gradient of the objective function and throws the warning above.
To avoid the warning, you may need to modify your objective function to return non-NaN values for inputs of this type. There are also known limitations with the Active-Set algorithm in that in certain intermediate iterations it may choose a value of "x" that violates the constraints. This can pose a problem if the objective function is not defined (returns NaN) for values of "x" in which the constraints are invalid.
If this is relevant to your application, as workarounds you can either generalize the objective function to handle such values of "x" or use the Interior-Point algorithm which does not have this limitation.