MATLAB: Eig() shows positive eigenvalues while chol() failed

eig chol

Hi guys,
I was LU-decomposing a supposed-to-be positive-definite matrix using "chol" in a loop. However, occasionally it failed with the warning
" Error using chol
Matrix must be positive definite. "
eig() shows that the smallest eigenvalue is 1.8*10^(-4) which I think should be well above zero. BTW, the largest one is 7.7*10^9.
How can I get around this dilemma? Your suggestions are greatly appreciated. Many thanks.

Best Answer

I wrote nearestSPD (download it from the file exchange) for this purpose. It will tweak the values of your matrix slightly so that chol should always succeed. Of course, if the required tweak is necessarily large, it will still make a minimal change so that the result is as close as possible, yet will be positive definite.