Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

generate a positive semi-definite matrices??

1,011 views
Skip to first unread message

VijaKhara

unread,
Feb 9, 2008, 9:50:10 PM2/9/08
to
Hi all


I need to generate a 3x3 positive semi-definite matrix but I don't
know what MATLAB function can do this?
Or is there any method to generate without try & check method?

Also, do you know what MATLAB function can be used to check if a
matrix is a positive semi-definite matrix?

Thank you

James Tursa

unread,
Feb 9, 2008, 10:17:33 PM2/9/08
to

One way to generate a random positive semi-definite 3x3 matrix:

a = rand(3,3)
ata = a'*a

To check, use the eig function to see that all of the eigenvalues are
non-negative

eig(ata)

James Tursa

Roger Stafford

unread,
Feb 9, 2008, 10:48:01 PM2/9/08
to
VijaKhara <Vija...@gmail.com> wrote in message
<b082bc0e-5d7a-4bcf-
ab79-478...@e4g2000hsg.googlegroups.com>...
-----------
1. Generate a random 3 x 3 unitary matrix and a random set of three non-
negative eigenvalues.

A = randn(3);
[U,ignore] = eig((A+A')/2); % (Don't really have to divide by 2)
M = U*diag(abs(randn(3,1)))*U';

Then M is a randomly determined positive semi-definite matrix.

2. Check that M is positive semi-definite:

all(diag(eig((M+M')/2))) >= 0

If this is true, then M is positive semi-definite

Roger Stafford

James Tursa

unread,
Feb 10, 2008, 3:38:01 AM2/10/08
to
James Tursa <aclassyguy...@hotmail.com> wrote in
message <0rqsq3tqr22j3e1h3...@4ax.com>...

Clarify: "ata" is the positive semi-definite matrix, not "a".

Roger Stafford

unread,
Feb 10, 2008, 4:17:02 AM2/10/08
to
"Roger Stafford" <ellieandr...@mindspring.com.invalid> wrote in
message <fols5h$e71$1...@fred.mathworks.com>...
> ..............

> 2. Check that M is positive semi-definite:
>
> all(diag(eig((M+M')/2))) >= 0
>
> If this is true, then M is positive semi-definite
---------
In the check for M to be positive semi-definite I should have written:

all(eig((M+M')/2)) >= 0

I forgot that eig with only one output produces a vector result, not a square
array, and the diag operation should not be used.

Second observation: There seem to be two definitions of a semi-definite
matrix floating around. One, as for example in Wikipedia, insists that it both
be Hermitian and have non-negative eigenvalues. The other definition, as for
example in MathWorld and which I have used here, requires only that its
Hermitian part, (M+M')/2, have non-negative eigenvalues.

Roger Stafford


James Tursa

unread,
Feb 10, 2008, 2:52:17 PM2/10/08
to

Where, exactly, are you getting these definitions from? Can you post
some web links? The Hermitian restriction does not agree with my
understanding at all. For a matrix M to be positive semi-definite, it
simply has to satisfy this (which I am sure you already know but I
repeat for others):

x' * M * x >= 0 for all non-zero vectors x

where x' is the complex conjugate transpose operator.

And testing the sign of all of the eigenvalues of M is a necessary and
sufficient test for this. I am not sure what the M + M' calculation in
your post (from other links) has to do with this. For example, see
this wiki link which agrees with my understanding:

http://en.wikipedia.org/wiki/Positive-definite_matrix

Here is an example of a random matrix I generated in MATLAB that is
not Hermitian but is positive semi-definite:

0.95012928514718 0.48598246870930 0.45646766516834
0.23113851357429 0.89129896614890 0.01850364324822
0.60684258354179 0.76209683302739 0.82140716429525

A Hermitian matrix (i.e., where isequal(M,M') is true ) *will* be
positive semi-definite (an easy thing to prove), but it is not a
necessary condition for positive semi-definiteness. Perhaps this is
where the confusion is?

James Tursa

Roger Stafford

unread,
Feb 10, 2008, 4:55:03 PM2/10/08
to
James Tursa <aclassyguy...@hotmail.com> wrote in message
<3dkuq3ls0516g4ina...@4ax.com>...
----------
I dug up a few web sites pertinent to this matter, James. You'll note that the
first one is the site you mentioned. In its first paragraph, which I quote
below, it appears to restrict positive definite matrices to those that are
Hermitian. The same is true of the MathWorld site for semi-definite matrices,
though elsewhere MathWorld states it differently. The matter is explained in
greater depth in the third site below which discusses non-Hermitian matrices
in one of its paragraphs. As it states in its concluding remark in this
paragraph, "There is no agreement in the literature on the proper definition
of positive-definite for non-Hermitian matrices." There are also some very
similar remarks to this last in your referenced Wikipedia site.

In particular you will note that if you were to require that your expression
x'*M*x above be real and non-negative for all complex vectors x, then M
must necessarily be Hermitian. If you only require that the real part of x'*M*x
be non-negative for all complex x, then that would allow M to be non-
Hermitian.

Here are the web sites and quotations which I refer you to:
-----------------------------------------------------
http://en.wikipedia.org/wiki/Positive-definite_matrix

"In linear algebra, a positive-definite matrix is a Hermitian matrix which in
many ways is analogous to a positive real number."
-----------------------------------------------------
http://mathworld.wolfram.com/PositiveSemidefiniteMatrix.html

"A positive semidefinite matrix is a Hermitian matrix all of whose eigenvalues
are nonnegative."
-----------------------------------------------------
http://www.halfvalue.com/wiki.jsp?topic=Positive-definite_matrix

"Non-Hermitian matrices
A real matrix M may have the property that xTMx > 0 for all nonzero real
vectors x without being symmetric. The matrix

[1 1
0 1]

provides an example. In general, we have xTMx > 0 for all real nonzero
vectors x if and only if the symmetric part, (M + MT) / 2, is positive definite.

The situation for complex matrices may be different, depending on how one
generalizes the inequality z*Mz > 0. If z*Mz is real for all complex vectors z,
then the matrix M is necessarily Hermitian. So, if we require that z*Mz be real
and positive, then M is automatically Hermitian. On the other hand, we have
that Re(z*Mz) > 0 for all complex nonzero vectors z if and only if the
Hermitian part, (M + M*) / 2, is positive definite.

In summary, the distinguishing feature between the real and complex case is
that, a bounded positive operator on a complex Hilbert space is necessarily
Hermitian, or self adjoint. The general claim can be argued using the
polarization identity. That is no longer true in the real case.

There is no agreement in the literature on the proper definition of positive-
definite for non-Hermitian matrices."
-----------------------------------------------------

Roger Stafford

James Tursa

unread,
Feb 11, 2008, 3:58:50 AM2/11/08
to
On Sun, 10 Feb 2008 19:52:17 GMT, James Tursa
<aclassyguy...@hotmail.com> wrote:
>A Hermitian matrix (i.e., where isequal(M,M') is true ) *will* be
>positive semi-definite ...

*groan* ... I meant to write that a Hermitian matrix has real
eigenvalues, not that it is necessarily positive semi-definite.

Lanyi Xu

unread,
Feb 11, 2008, 9:23:02 AM2/11/08
to
James Tursa <aclassyguy...@hotmail.com> wrote in
message <7930r31i4p1uenb58...@4ax.com>...

let's
a =

0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
then;
>> ata=a'*a;
>> eig(ata)

ans =

0.0329
0.7038
3.3007

>> ata2=(a'+a);
>> eig(ata2)

ans =

-0.4007
1.6130
3.5969


So, that means (a'+a)/2 may not be a positive-define matrix.

Regard

Lanyi

Pablo Ñañez

unread,
Nov 9, 2012, 12:29:20 PM11/9/12
to
I have created this little script that can help you, it creates a PD random matrix and then creates the symmetric version of the original matrix. I hope that this works for you.

Pablo.

% This script generates random positive definite symmetric matrices of size n.
% By Pablo Nanez (Ñañez)
function [Q] = randPDsymmMatix(n)
Q = randn(n);
Q = Q*Q';
Q = symmetry(Q,1);
fprintf('The eigenvalues\n')
eig(Q)
end

function [A] = symmetry(A,tipo)
% Funcion para hacer la matriz simetrica de A
% Tipo: 1: matriz simetrica con signos
% Tipo: -1: matriz simetrica con signos contrarios
% e.g., D = simetria(D,1);
[f c] = size(A);
for iff = 1:f
for ic = 1:c
if isempty(A(iff,ic)) || A(iff,ic)==0
else
A(ic,iff) = tipo*A(iff,ic);
end
end
end
end

Muhammad Asim Mubeen

unread,
Apr 25, 2014, 3:54:00 PM4/25/14
to

Steven Lord

unread,
Apr 25, 2014, 5:32:18 PM4/25/14
to

"Muhammad Asim Mubeen" <muhammmdele...@yahoo.com> wrote in message
news:ljeego$sln$1...@newscl01ah.mathworks.com...
Some of the options for the GALLERY function may be of interest in solving
this problem.

http://www.mathworks.com/help/matlab/ref/gallery.html

The options 'gcdmat', 'randcorr', and 'toeppd' specifically mention
generating positive semidefinite matrices (or being able to generate a
random positive semidefinite matrix using the matrix returned by 'gcdmat'.)

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

0 new messages