Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
generate a positive semi-definite matrices??

Subject: generate a positive semi-definite matrices??

From: VijaKhara

Date: 10 Feb, 2008 02:50:10

Message: 1 of 12

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

Subject: generate a positive semi-definite matrices??

From: James Tursa

Date: 10 Feb, 2008 03:17:33

Message: 2 of 12

On Sat, 9 Feb 2008 18:50:10 -0800 (PST), VijaKhara
<VijaKhara@gmail.com> wrote:

>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

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

Subject: generate a positive semi-definite matrices??

From: Roger Stafford

Date: 10 Feb, 2008 03:48:01

Message: 3 of 12

VijaKhara <VijaKhara@gmail.com> wrote in message
<b082bc0e-5d7a-4bcf-
ab79-478ad6a820f9@e4g2000hsg.googlegroups.com>...
> 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
-----------
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

Subject: generate a positive semi-definite matrices??

From: James Tursa

Date: 10 Feb, 2008 08:38:01

Message: 4 of 12

James Tursa <aclassyguywithaknotac@hotmail.com> wrote in
message <0rqsq3tqr22j3e1h3emodegqmra5bd76rt@4ax.com>...
> On Sat, 9 Feb 2008 18:50:10 -0800 (PST), VijaKhara
> <VijaKhara@gmail.com> wrote:
>
> >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
>
> 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

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

Subject: generate a positive semi-definite matrices??

From: Roger Stafford

Date: 10 Feb, 2008 09:17:02

Message: 5 of 12

"Roger Stafford" <ellieandrogerxyzzy@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

Subject: generate a positive semi-definite matrices??

From: James Tursa

Date: 10 Feb, 2008 19:52:17

Message: 6 of 12

On Sun, 10 Feb 2008 09:17:02 +0000 (UTC), "Roger Stafford"
<ellieandrogerxyzzy@mindspring.com.invalid> wrote:

>"Roger Stafford" <ellieandrogerxyzzy@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
>

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

Subject: generate a positive semi-definite matrices??

From: Roger Stafford

Date: 10 Feb, 2008 21:55:03

Message: 7 of 12

James Tursa <aclassyguywithaknotac@hotmail.com> wrote in message
<3dkuq3ls0516g4inapn6hvll7sac7k44np@4ax.com>...
> On Sun, 10 Feb 2008 09:17:02 +0000 (UTC), "Roger Stafford"
> <ellieandrogerxyzzy@mindspring.com.invalid> wrote:
> > 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
>
> 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
----------
  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

Subject: generate a positive semi-definite matrices??

From: James Tursa

Date: 11 Feb, 2008 08:58:50

Message: 8 of 12

On Sun, 10 Feb 2008 19:52:17 GMT, James Tursa
<aclassyguywithaknotac@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.

Subject: generate a positive semi-definite matrices??

From: Lanyi Xu

Date: 11 Feb, 2008 14:23:02

Message: 9 of 12

James Tursa <aclassyguywithaknotac@hotmail.com> wrote in
message <7930r31i4p1uenb58ac90nve3giru5el1a@4ax.com>...
> On Sun, 10 Feb 2008 19:52:17 GMT, James Tursa
> <aclassyguywithaknotac@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.

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

Subject: generate a positive semi-definite matrices??

From: Pablo Ñañez

Date: 9 Nov, 2012 17:29:20

Message: 10 of 12

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

Subject: generate a positive semi-definite matrices??

From: Muhammad Asim Mubeen

Date: 25 Apr, 2014 19:54:00

Message: 11 of 12

You can use the following function:

http://www.mathworks.com/matlabcentral/fileexchange/45873-positive-definite-matrix

Subject: generate a positive semi-definite matrices??

From: Steven Lord

Date: 25 Apr, 2014 21:32:18

Message: 12 of 12


"Muhammad Asim Mubeen" <muhammmdeletemad_asimm@yahoo.com> wrote in message
news:ljeego$sln$1@newscl01ah.mathworks.com...
> You can use the following function:
>
> http://www.mathworks.com/matlabcentral/fileexchange/45873-positive-definite-matrix

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
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us