# Checking positive semidefiniteness in MATLAB

Let $\mathbf{A}$ be a $n\times n$ matrix. I want to check in MATLAB if it is PSD or not. Which tests, in MATLAB, should I do for this purpose?

I know that if $\mathbf{A}$ is PSD then following holds

• eigenvalue decomposition: [V,lambda]=eig(A), then $\lambda_i\geq 0 \hspace{2mm}\forall \hspace{2mm} i=1:n$
• Cholesky decomposition: [R,p] = chol(A) then $p=0$

WHY: I am asking for a list because I am having an issue. I started with a matrix $\mathbf{X}$ that was supposed to be PSD because of the way I was constructing it. However, some of its eigen values were negative. It turned out that floating point arithmetic is to be blamed for this.

Problem: I came across https://www.mathworks.com/matlabcentral/fileexchange/42885-nearestspd. According to this function, $\mathbf{A}$=nearestSPD($\mathbf{X}$), $\mathbf{A}$ is the nearest SPD to $\mathbf{X}$. In help section, author mentioned that [R,p]=chol(A) will return p=0 which proves that A is SPD. Now the problem is that after getting $\mathbf{A}$, I do get p=0 for chol($\mathbf{A}$), but I am still getting one or more negative eigen values. Although the magnitude of these negative eigen values is very small something like e-15 but still the sign is negative.

So I don't understand why these tests are giving different results and what should one do in such a case.

• – Rodrigo de Azevedo Nov 1 '16 at 15:48
• – copper.hat Nov 1 '16 at 15:51
• @ Rodrigo, I asked that question yesterday and my take away from the comments was that in MATLAB, a matrix $\mathbf{X}$ is not PSD just because the way it is constructed. So that is why I used $\mathbf{A}=nearestSPD(\mathbf{X})$ to get a SPD matrix. – NAASI Nov 1 '16 at 15:59
• @ copper.hat, your reference does lists the tests. However, last part of my question is why two eligible tests are giving different results in MATLAB. This part is not answered there. – NAASI Nov 1 '16 at 16:00