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:
Output order of eig(matrix)

Subject: Output order of eig(matrix)

From: Jeff

Date: 8 Oct, 2011 23:30:25

Message: 1 of 7

Can you change the order of eigenvalues returned by eig(A)?

For my master's thesis, I need to calculate the potential energy in a system (of springs and masses) in regular space-space and then in Fourier-space. But, according to my adviser, eig() returns the eigenvalues in the "wrong" order. That is, ESysPot below, should equal FSysPot (within a factor of N, the number of masses). But it's not. Note that the calculations work correctly for kinetic energy, which does not involve using the eigenvalues.

% This part probably not important to my question.
N=8; t_steps=1000; n=(1:N);
s0=s0=[sin(2*pi*n/N) zeros(1,N)];
[t_steps,s1] = ode45(@ODE_linear_periodic, t_steps, s0); ...
    spring='Linear'; boundary='Periodic';
X=s1(:,1:end/2);

% Matrix A is the coefficients of the system in @ODE_linear_periodic
A=-2*eye(N)+diag(ones(N-1,1),-1)+diag(ones(N-1,1),1);
A(1,N)=1;
A(N,1)=1;
omga=eig(A);

% Potential energy in space-space, gets correct results (if you divide by N).
Epot=((X(:,nPlusOne)-X(:,n)).^2);
ESysPot=sum(Epot,2);

% Fourier transform to Fourier-space (frequency space)
F=abs(fft(U')');

% Potential energy in Fourier-space, gets way wrong results.
FSysPot=Fpot.^2*omga.^2;
% Correct calculation may be FSysPot=Fpot*omga;

What I'm trying to say is that the potential energy of the system should be the same, whether the potential is measured in regular space (ESysPot) or in Fourier space (FSysPot). But the Fourier space calculations are coming out wrong solely because omga=eig(A) is spitting out the eigenvalues in the "wrong" order, where "wrong" means "not in the same order that the Fourier transform is applying the eigenvectors" (is any of this making sense?).

Due to the makeup of the system (it being a circulant matrix), the vectors of Matlab's FFT are the same as the eigenvectors of A. But I need the eigenvalues of A in the same order as the eigenvectors of the transform.

Anyone know how to do that? All input is appreciated (no, really, I'm getting desperate, so I appreciate anything you can say! :D )

Subject: Output order of eig(matrix)

From: Greg Heath

Date: 8 Oct, 2011 23:59:56

Message: 2 of 7

On Oct 8, 7:30 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> Can you change the order of eigenvalues returned by eig(A)?
>
> For my master's thesis, I need to calculate the potential energy in a system (of springs and masses) in regular space-space and then in Fourier-space. But, according to my adviser, eig() returns the eigenvalues in the "wrong" order.  That is, ESysPot below, should equal FSysPot (within a factor of N, the number of masses). But it's not. Note that the calculations work correctly for kinetic energy, which does not involve using the eigenvalues.
>
> % This part probably not important to my question.
> N=8; t_steps=1000; n=(1:N);
> s0=s0=[sin(2*pi*n/N) zeros(1,N)];
> [t_steps,s1] = ode45(@ODE_linear_periodic, t_steps, s0); ...
>     spring='Linear'; boundary='Periodic';
> X=s1(:,1:end/2);
>
> % Matrix A is the coefficients of the system in @ODE_linear_periodic
> A=-2*eye(N)+diag(ones(N-1,1),-1)+diag(ones(N-1,1),1);
> A(1,N)=1;
> A(N,1)=1;
> omga=eig(A);
>
> % Potential energy in space-space, gets correct results (if you divide by N).
> Epot=((X(:,nPlusOne)-X(:,n)).^2);
> ESysPot=sum(Epot,2);
>
> % Fourier transform to Fourier-space (frequency space)
> F=abs(fft(U')');
>
> % Potential energy in Fourier-space, gets way wrong results.
> FSysPot=Fpot.^2*omga.^2;
> % Correct calculation may be FSysPot=Fpot*omga;
>
> What I'm trying to say is that the potential energy of the system should be the same, whether the potential is measured in regular space (ESysPot) or in Fourier space (FSysPot). But the Fourier space calculations are coming out wrong solely because omga=eig(A) is spitting out the eigenvalues in the "wrong" order, where "wrong" means "not in the same order that the Fourier transform is applying the eigenvectors" (is any of this making sense?).
>
> Due to the makeup of the system (it being a circulant matrix), the vectors of Matlab's FFT are the same as the eigenvectors of A. But I need the eigenvalues of A in the same order as the eigenvectors of the transform.
>
> Anyone know how to do that? All input is appreciated (no, really, I'm getting desperate, so I appreciate anything you can say! :D )

You say wrong without an explanation or demonstation. Since N = 8.
how about it?

format long


Hope this gelps.
Greg

Subject: Output order of eig(matrix)

From: Jeff

Date: 9 Oct, 2011 01:40:27

Message: 3 of 7

I tried to describe "wrong" as best I could. Unfortunately, half of the explanation was before my code, and half was after. Also, I'm still learning this stuff, and trying not to include too much or too little info, at the same time as trying to learn Matlab. Thanks for bearing with me.

The short version: I need the eigenvalues of matrix A (below) given in the order of the eigenvectors of A, which are listed in matrix Fxfrm.

First, picture my Picture my data as this vertical vector:
U=[u(1);u(2);u(3);u(4);u(5);u(6);u(7);u(8)];

That data was created in the ode45 solver by the system represented by this matrix:
A=-2*eye(N)+diag(ones(N-1,1),-1)+diag(ones(N-1,1),1);
A(1,N)=1;A(N,1)=1;

To calculate the potential energy in regular space, I would use the equation:
Epot=(u(n+1)-u(n))^2;
and sum for all n=1..N.

The fast Fourier transform, as implemented by Matlab, can be envisioned as a matrix, Fxfrm, something like this:

w=exp(-2*pi*i/N);
for m=1:N,for n=1:N,Fxfrm(m,n)=w^((m-1)*(n-1));end,end

This is easier to visualize:
Fxfrm=
[ 1, 1, 1, 1, 1, 1, 1, 1]
[ 1, w, w^2, w^3, w^4, w^5, w^6, w^7]
[ 1, w^2, w^4, w^6, w^8, w^10, w^12, w^14]
[ 1, w^3, w^6, w^9, w^12, w^15, w^18, w^21]
[ 1, w^4, w^8, w^12, w^16, w^20, w^24, w^28]
[ 1, w^5, w^10, w^15, w^20, w^25, w^30, w^35]
[ 1, w^6, w^12, w^18, w^24, w^30, w^36, w^42]
[ 1, w^7, w^14, w^21, w^28, w^35, w^42, w^49]

Note that the eigenvectors of A are the columns of Fxfrm (because A is circulant).

If I multiplied Fxfrm times my data (Fxfrm on the left), I get my data in Fourier space (or frequency space):
X=[x(1);x(2);x(3);x(4);x(5);x(6);x(7);x(8)];

Now, I need to calculate the potential energy in frequency space, using, essentially, this equation:
lambda=eig(A);
Fpot=x(n)^2 * lambda(n)^2;
where lambda_n is the nth eigenvalue of A.

That is lambda(1) should be the eigenvalue associated with column 1 of Fxfrm; lambda(2) should be the eigenvalue associated with column 2 of Fxfrm, etc. But lambda=eig(A) returns the eigenvectors in declining magnitude order. I need the eigenvalues of A in the same order as the eigenvectors of A are listed in Fxfrm.

Thanks, again.
J

P.S. Isn't there a better, more Matlab-y (vectorized) way to create Fxfrm than my nested for loops and a more vectorized way create A?

Subject: Output order of eig(matrix)

From: Greg Heath

Date: 9 Oct, 2011 23:15:57

Message: 4 of 7

On Oct 8, 9:40 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> I tried to describe "wrong" as best I could. Unfortunately, half of the explanation was before my code, and half was after. Also, I'm still learning this stuff, and trying not to include too much or too little info, at the same time as trying to learn Matlab. Thanks for bearing with me.
>
> The short version: I need the eigenvalues of matrix A (below) given in the order of the eigenvectors of A, which are listed in matrix Fxfrm.
>
> First, picture my Picture my data as this vertical vector:
> U=[u(1);u(2);u(3);u(4);u(5);u(6);u(7);u(8)];
>
> That data was created in the ode45 solver by the system represented by this matrix:
> A=-2*eye(N)+diag(ones(N-1,1),-1)+diag(ones(N-1,1),1);
> A(1,N)=1;A(N,1)=1;
>
> To calculate the potential energy in regular space, I would use the equation:
> Epot=(u(n+1)-u(n))^2;
> and sum for all n=1..N.
>
> The fast Fourier transform, as implemented by Matlab, can be envisioned as a matrix, Fxfrm, something like this:
>
> w=exp(-2*pi*i/N);
> for m=1:N,for n=1:N,Fxfrm(m,n)=w^((m-1)*(n-1));end,end
>
> This is easier to visualize:
> Fxfrm=
> [ 1,   1,    1,    1,    1,    1,    1,    1]
> [ 1,   w,  w^2,  w^3,  w^4,  w^5,  w^6,  w^7]
> [ 1, w^2,  w^4,  w^6,  w^8, w^10, w^12, w^14]
> [ 1, w^3,  w^6,  w^9, w^12, w^15, w^18, w^21]
> [ 1, w^4,  w^8, w^12, w^16, w^20, w^24, w^28]
> [ 1, w^5, w^10, w^15, w^20, w^25, w^30, w^35]
> [ 1, w^6, w^12, w^18, w^24, w^30, w^36, w^42]
> [ 1, w^7, w^14, w^21, w^28, w^35, w^42, w^49]
>
> Note that the eigenvectors of A are the columns of Fxfrm (because A is circulant).
>
> If I multiplied Fxfrm times my data (Fxfrm on the left), I get my data in Fourier space (or frequency space):
> X=[x(1);x(2);x(3);x(4);x(5);x(6);x(7);x(8)];
>
> Now, I need to calculate the potential energy in frequency space, using, essentially, this equation:
> lambda=eig(A);
> Fpot=x(n)^2 * lambda(n)^2;
> where lambda_n is the nth eigenvalue of A.
>
> That is lambda(1) should be the eigenvalue associated with column 1 of Fxfrm; lambda(2) should be the eigenvalue associated with column 2 of Fxfrm, etc. But lambda=eig(A) returns the eigenvectors in declining magnitude order. I need the eigenvalues of A in the same order as the eigenvectors of A are listed in Fxfrm.
>
> Thanks, again.
> J
>
> P.S. Isn't there a better, more Matlab-y (vectorized) way to create Fxfrm than my nested for loops and a more vectorized way create A?

You can't rely on producing any specified order with eig or eigs

rand('state',0)
for i = 1:20
       x = rand(3);
       y = eig(x);
       z = eigs(x);
       result = [y-z y z ]
end


Hope this helps.

Greg

Subject: Output order of eig(matrix)

From: Steven_Lord

Date: 10 Oct, 2011 03:50:56

Message: 5 of 7



"Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
news:j6qmeh$g2a$1@newscl01ah.mathworks.com...
> Can you change the order of eigenvalues returned by eig(A)?

No. The documentation for EIG makes no promises about the order of the
eigenvalues it returns. If you need them in a specific order, use other
functions to reorder them and reorder the corresponding eigenvectors as
well. For example, if you want them sorted, use SORT to sort the eigenvalues
and use the second output from that SORT call to reorder the eigenvectors.

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

Subject: Output order of eig(matrix)

From: Jeff

Date: 10 Oct, 2011 05:17:30

Message: 6 of 7

"Steven_Lord" <slord@mathworks.com> wrote in message <j6tq31$b$1@newscl01ah.mathworks.com>...
>
>
> "Jeff " <spREMOVEHITSjeffAT@SIGNoptonline.net> wrote in message
> news:j6qmeh$g2a$1@newscl01ah.mathworks.com...
> > Can you change the order of eigenvalues returned by eig(A)?
>
> No. The documentation for EIG makes no promises about the order of the
> eigenvalues it returns. If you need them in a specific order, use other
> functions to reorder them and reorder the corresponding eigenvectors as
> well. For example, if you want them sorted, use SORT to sort the eigenvalues
> and use the second output from that SORT call to reorder the eigenvectors.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thanks, Steve. But I need them in the order of Matlab's implementation of the Fourier Transform. That is not a set order (like ascending, descending) or anything like that. I am writing my own routine to calculate eigenvalues in the order I need them.

Subject: Output order of eig(matrix)

From: Bruno Luong

Date: 10 Oct, 2011 05:45:19

Message: 7 of 7

"Jeff" wrote in message <j6tv5a$e68$1@newscl01ah.mathworks.com>...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> Thanks, Steve. But I need them in the order of Matlab's implementation of the Fourier Transform. That is not a set order (like ascending, descending) or anything like that. I am writing my own routine to calculate eigenvalues in the order I need them.

Check again with your adviser.

1) If you want Fourier order then use Fourier to do the calculation.

2) If the result relies on a specific order in order to work, then something is fishy in the method of calculation.

3) If that is indeed the case but you are unable to specify in the simple terms what the order is required (rather than "same as FFT" which is nonsense IMO), then go back to the black board and check the logic that leads you to that conclusion.

4) If your adviser is not agree, tell him *I* say that.

Bruno

Tags for this Thread

No tags are associated with 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