How do I create a function script to check the positive definiteness of a a square matrix of any size?

39 views (last 30 days)
Hey! I'm currently working on a lab where I need to check if a square matrix is positive and definite. My function script needs to
  1. accept one sqaure matrix from the calling program,
  2. determine if the matrix is positive and definite, and
  3. return the result to the calling program - 1 if positive definite, 0 otherwise.
I need to come up with a pattern for the matrices to be check. For a four by four matrix (M) as an example, my code needs to check M(1:1,1:1), M(1:2,1:2), M(1:3,1:3,), M(1:4,1:4), then M(2:2,2:2), M(2,3:2,3), and so on until the last martix M(4:4,4:4). I need help creating a function using for loops! Below is the code I have as of right now, but I don't think it's right. Please help me write some code! Thank you :)
M = [ 1 2 3 4 ; 5 6 7 8 ; 9 10 11 12 ; 13 14 15 16];
x = NAME_lab01(M);
%%
function x = NAME_Lab01(M)
N = length(M)
[r,c] = size (M)
pos_def = 1
for i = 1: N+1-r
for j = 1:c+r-1
if det(i,j) > 0
pos_def = 1
else
pos_def = 0
end
end
end
end
  32 Comments
Paul
Paul on 6 Sep 2020
I don’t know if NSPD matrices have additional useful properties compared to SPD matrixes. But they must have some importance in solving linear systems as in the GVL link that Bruno posted.
Maybe I’m just parsing words here, but you don’t need the symmetry assumption to apply those criteria. Rather the PD of M, symmetric or not, can always be determined by applying them to M+M’ . More generally, I’m pretty sure that everything you need to know about the quadratic form of M can be determined from the quadratic form of (M+M’)/2.
Bruno Luong
Bruno Luong on 6 Sep 2020
Edited: Bruno Luong on 6 Sep 2020
Personally I never deal with unsymmetric DP matrix, but when I was taugh bilinear form, they teach us a bilinear for can be NOT necessary symmetric, which represents by an unsymmetric matrix.
I guess the theory can also be extrended to some binear form that is applied on vector of elements that belong non-cummutative ring (such as quaternion), in which you can't swap the order.
Quantum mechanics manipulates typical such bilinear form, but I'm unable to tell you a concrete example of operator that is unsymmetric and DP, this is not my field of expertise.
But then Golub/Van-Loan showed in this paper they can exploit such definite positive property on non-symeetric matrix and doing some improvement on a basic task of solving the associate linear equations.

Sign in to comment.

Answers (3)

Matt J
Matt J on 5 Sep 2020
If you're allowed to use stock Matlab commands like det(), I don't know why you couldn't use eig().
pos_def = isequal(M,M') && all( eig(M)>0 )

Bruno Luong
Bruno Luong on 6 Sep 2020
Edited: Bruno Luong on 6 Sep 2020
Code based on Sylvester's critterion
function tf = isposdef(M)
tf = ispd(M+M');
end
function tf = ispd(M)
tf = det(M)>0 && (length(M)<=1 || ispd(M(1:end-1,1:end-1)));
end
EDIT: fix bug length(M)<=1
  7 Comments
Paul
Paul on 9 Sep 2020
Is the incomplete Cholesky factorization implemented by the chol function? I'm asking because of the thread in the first link that Ameer posted way earlier in this thread that shows that the chol function may not be reliable in determining if a matrix is SPD (repeating the link here for easy access): https://www.mathworks.com/matlabcentral/answers/101132-how-do-i-determine-if-a-matrix-is-positive-definite-using-matlab?s_tid=srchtitle
Repeating an example from that thread:
A =0.0327*ones(2,2);
[R,flag]=chol(A);flag
flag =
0
According to doc chol (R2019A): "If flag = 0 then the input matrix is symmetric positive definite and the factorization was successful." In this case, A is definitely symmetric, but I don't think it's PD.
Bruno Luong
Bruno Luong on 9 Sep 2020
IMO there is no algorithm that can 100% reliable in case the matrix is non-negative and not definite positive (one eigen value is 0), since any method will be sensitive to numerical errors that can randomly make or break the result.
But yeah intuitively I would also think CHOL could be less reliable than EIGS.

Sign in to comment.


Matt J
Matt J on 6 Sep 2020
Sadly, I do not believe I can use that. I am supposed to write code using for loops and the det() function.
The code below fulfills the "for-loops +det() function" requirement. This is to make the point that the assignment is non-sensical unles it specifically says you must use Sylvester's criterion.
function tf=isposdef(M)
e=eig(M+M');
tf=true;
for i=1:numel(e)
tf=tf & det(e(i))>0;
end
end

Categories

Find more on Linear Algebra in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!