Daniel@0: function s = checkpsd(s) Daniel@0: Daniel@0: if (any(isnan(s) | isinf(s) | ~isreal(s))) Daniel@0: warning('S contains complex numbers, Inf, or NaN'); Daniel@0: end Daniel@0: % Drop any negative eigenvalues. Daniel@0: [V, D] = eig(full(s)); Daniel@0: d = real(diag(D)); Daniel@0: if (any(d < 0)) Daniel@0: warning(sprintf(['S is not positive semidefinite (min. eig. =' ... Daniel@0: ' %0.5g); projecting.'], min(d))); Daniel@0: d(find(d < 0)) = 0; Daniel@0: D = diag(d); Daniel@0: s = V * D * V'; Daniel@0: end