Discussion:
Different eigenvector matrix in Scilab
(too old to reply)
s***@yahoo.com
2006-05-19 23:34:33 UTC
Permalink
Hallo Dear Scilab profis,


I am trying to translate a MCode into Scilab code and I am experiencing
the following Problem.
I would like to find a matrices of eigenvalues (D) and eigenvectors
(V) of matrix A

A = [ 0.86704636614488 -0.06299441206758 0.43864117996312
-0.03877523903679;
0.04481251200767 0.91855990949380 0.12651267436474
0.34142302233678;
0.00874242025365 0.02020568955919 0.79997826398638
0.06993676528265;
-0.02082126954048 0.01288328778550 -0.09511048005178
0.77291062528872 ];

The Matlab command [V,D] = eig(A)

produces the following results



D =

0.7714 0 0 0
0 0.7865 0 0
0 0 0.8658 0
0 0 0 0.9349


V =

-0.9685 -0.9503 0.9124 0.0230
0.1041 -0.2220 0.3621 0.9892
0.2261 0.1562 0.0327 0.1448
-0.0015 0.1527 -0.1879 -0.0093




Matrix D is the canonical form of A--a diagonal matrix with A's
eigenvalues on the main diagonal.
Matrix V is the modal matrix--its columns are the eigenvectors of A.




The Scilab command [V,D]=spec(A+%i*0)

produces


D =

0.7714 0 0 0
0 0.7865 0 0
0 0 0.8658 0
0 0 0 0.9349



V =

0.9685 0.9503 0.9124 0.0230
- 0.1041 0.2220 0.3621 0.9892
- 0.2261 - 0.1562 0.0327 0.1448
0.0015 - 0.1527 - 0.1879 - 0.0093


As you see, the eigenvalues matrix is identical to Matlab's one.

The eigenvectors matrix has the same numbers BUT different signs.

I need to get the same matrix V in Scilab as in Matlab. Would you
please help me?


Thank you in advance
arvro395
2006-05-20 07:31:35 UTC
Permalink
The sign of the eigenvectors bears no meaning. What sign you get
probably depends on the internal implementation of spec/eig and will be
very hard to predict. Is this realy important for your application?

Cheers
Arvid
s***@yahoo.com
2006-05-20 09:44:00 UTC
Permalink
Hallo Arvid.

Thank you for your reply.

Yes it is very important to generate the same Matrix as in MAtlab,
because all further calculations are based on this Matrix. I want to
make a simulation with scicos, and negative sign at the wrong place oft
results in false behaviour of my system.

Thanks
arvro395
2006-05-20 11:38:56 UTC
Permalink
Hm...
Strange. How do you use these eigenvectors? I can't think of a case
where the sign matters. Another thing that might differ when you
compare the results from spec with the results you get from eig, is the
ordering of the eigenvectors (and eigenvalues). If the eigenvectors are
ordered differently, you will end up with non-identical matrices.
However, as with the sign, the ordering bears no meaning.
Jean-Pierre Vial
2006-05-20 14:43:06 UTC
Permalink
Post by arvro395
Hm...
Strange. How do you use these eigenvectors? I can't think of a case
where the sign matters. Another thing that might differ when you
compare the results from spec with the results you get from eig, is the
ordering of the eigenvectors (and eigenvalues). If the eigenvectors are
ordered differently, you will end up with non-identical matrices.
However, as with the sign, the ordering bears no meaning.
Even worse: if two or more eigenvalues are equal, you may get completely
different eigenvectors, and there is no way to chose one set rather than
another in a consistent and predictable way.

For your simpler problem of sign of eigenvectors when all eigenvalues
are different, if you really need one sign rather than the other, you
must define the convention to use. The Matlab convention is not obvious
on your example (and maybe there is no convention, the sign is just what
the computation ends to: completely unpredictable)
Any way, as Avro says, the sign is completely meaningless.
s***@yahoo.com
2006-05-20 16:28:33 UTC
Permalink
Hallo.


I have a very long calculation and at one point I need to find
eigenvalues(Dneu)
and the eigenvektors(Vneu) matrices of the Matrix V. So what I do is: I
give the following
command

in Matlab: [Vneu,Dneu] = eig(V)

and

In Scilab: [Vneu,Dneu] = spec(V)

The results that I get in Matlab and Scilab are completely
different(this time not only in signs
but also in numbers). And that is why I end up with a wrong values at
the end of my long calculation.


Jean-Pierre you wrote that there is a possibility to define a
convention for Scilab to calculate the
eigenvektros and eigenvalues. How can I do that?


Thanks
Jean-Pierre Vial
2006-05-21 06:35:41 UTC
Permalink
Post by s***@yahoo.com
Hallo.
I have a very long calculation and at one point I need to find
eigenvalues(Dneu)
and the eigenvektors(Vneu) matrices of the Matrix V. So what I do is: I
give the following
command
in Matlab: [Vneu,Dneu] = eig(V)
and
In Scilab: [Vneu,Dneu] = spec(V)
The results that I get in Matlab and Scilab are completely
different(this time not only in signs
but also in numbers). And that is why I end up with a wrong values at
the end of my long calculation.
This is perfectly normal for the eigenvectors.
If you have multiple eigenvalues (two or more eigenvalues are equal),
there is nothing to be done: you must live with non unique answers,
it is an intrinsic characterisic of your problem, even if you don't like it.
If all eigenvalues are different, you can for instance normalize
all eigenvectors, if it is not already done (divide each vector by its
norm), then choose a sign convention and write a loop (see <<help for>>)
to force this convention. Which convention ? nobody can help you for
that: it is a convention, nothing more, you choose it and stick to it
if you really need it.
As several others have already written, any computation that rely
on a precise sign of an eigenvector is meaningless if it does not
explain how the eigenvector is chosen.

If the eigenvalues also are different in two differnt computations, you
have a problem of numerical instability, and most probably both answers
are meaningless. The only sensible thing you can do is to completely
change the process to get rid of the instability; obviously you will
need expert help for that, not just a casual advice by e-mail.

Never forget that you cannot trust the result of a computation simply
because it come out of a computer, even without bug. Some computations
are just too sensible to rounding, truncation, approximation errors ...
to be of any practical use.
There are good books on numerical analysis, read one of them if your
problem is not VERY easy.
Donald R. Fredkin
2006-05-20 16:00:29 UTC
Permalink
Post by s***@yahoo.com
Yes it is very important to generate the same Matrix as in MAtlab,
because all further calculations are based on this Matrix. I want to
make a simulation with scicos, and negative sign at the wrong place oft
results in false behaviour of my system.
I am sorry if I appear a little vinegary but: If this is the case, you are
doing an incorrect calculation.
s***@yahoo.com
2006-05-20 21:38:56 UTC
Permalink
Hallo Donald.

A mistake in the calculation is possible. You are right and not a bit
vinegary.
The code I have to translate is rather long and I did't want to flood
this forum with my code. That is why I demostrated the difference of
matrices on a simple example.

The problem is that I get different values as a result of a different
conventions.

Hier is the code I have. When you give it in Matlab and then in Scilab
you will see that the results are different.
HIER IS THE MATLAB CODE<<<<<<<<<<<<<<<<<<<<<<<<<
Ag = [ 0.86704636614488 -0.06299441206758 0.43864117996312
-0.03877523903679;
0.04481251200767 0.91855990949380 0.12651267436474
0.34142302233678;
0.00874242025365 0.02020568955919 0.79997826398638
0.06993676528265;
-0.02082126954048 0.01288328778550 -0.09511048005178
0.77291062528872 ];

Bg = [ 0.22904427078953 0.50968131460528;
0.29248395319341 -0.02520184893537;
-0.00877980086192 -0.00507959766044;
0.00019856027454 0.01170298693483 ];

[Tdiag, A] = eig(Ag)
B = inv(Tdiag)*Bg

Atilde1 = A(3:4, 3:4)
Btilde1 = B(3:4, :)

Ag1 = diag([0.4 0.4])

R1 = [ zeros(2) inv(Btilde1)*(Atilde1-Ag1) ]

Am1 = A-B*R1
RESULT<<<<
Am1 =

0.7714 0 0.5084 0.4303
0 0.7865 -0.6180 -0.0338
0 0 0.4000 0.0000
0 0 -0.0000 0.4000
HIER IS THE SCILAB CODE<<<<<<<<<<<<<<<<<<<<<<<<<
Ag = [ 0.86704636614488 -0.06299441206758 0.43864117996312
-0.03877523903679;
0.04481251200767 0.91855990949380 0.12651267436474
0.34142302233678;
0.00874242025365 0.02020568955919 0.79997826398638
0.06993676528265;
-0.02082126954048 0.01288328778550 -0.09511048005178
0.77291062528872 ];

Bg = [ 0.22904427078953 0.50968131460528;
0.29248395319341 -0.02520184893537;
-0.00877980086192 -0.00507959766044;
0.00019856027454 0.01170298693483 ];

[Tdiag, A] = spec(Ag)
B = inv(Tdiag)*Bg

Atilde1 = A(3:4, 3:4)
Btilde1 = B(3:4, :)

Ag1 = diag([0.4 0.4])

R1 = [ zeros(2,2) inv(Btilde1)*(Atilde1-Ag1) ]

Am1 = A-B*R1
RESULT<<<<
Am1 =

0.8657688 0 - 0.2912661 0.0254379
0 0.7713546 - 0.3179539 - 0.4025489
0 0 0.4 1.735D-18
0 0 - 4.337D-19 0.4

That's what I meant. Am1 in Matlab differs from Am1 Matrix in scilab.
HOW DO I FIX IT?

Please comment

Regards
s***@yahoo.com
2006-05-21 08:09:58 UTC
Permalink
Thank you Jean-Piere
s***@gmail.com
2017-10-26 21:44:37 UTC
Permalink
Post by s***@yahoo.com
Hallo Dear Scilab profis,
I am trying to translate a MCode into Scilab code and I am experiencing
the following Problem.
I would like to find a matrices of eigenvalues (D) and eigenvectors
(V) of matrix A
A = [ 0.86704636614488 -0.06299441206758 0.43864117996312
-0.03877523903679;
0.04481251200767 0.91855990949380 0.12651267436474
0.34142302233678;
0.00874242025365 0.02020568955919 0.79997826398638
0.06993676528265;
-0.02082126954048 0.01288328778550 -0.09511048005178
0.77291062528872 ];
The Matlab command [V,D] = eig(A)
produces the following results
D =
0.7714 0 0 0
0 0.7865 0 0
0 0 0.8658 0
0 0 0 0.9349
V =
-0.9685 -0.9503 0.9124 0.0230
0.1041 -0.2220 0.3621 0.9892
0.2261 0.1562 0.0327 0.1448
-0.0015 0.1527 -0.1879 -0.0093
Matrix D is the canonical form of A--a diagonal matrix with A's
eigenvalues on the main diagonal.
Matrix V is the modal matrix--its columns are the eigenvectors of A.
The Scilab command [V,D]=spec(A+%i*0)
produces
D =
0.7714 0 0 0
0 0.7865 0 0
0 0 0.8658 0
0 0 0 0.9349
V =
0.9685 0.9503 0.9124 0.0230
- 0.1041 0.2220 0.3621 0.9892
- 0.2261 - 0.1562 0.0327 0.1448
0.0015 - 0.1527 - 0.1879 - 0.0093
As you see, the eigenvalues matrix is identical to Matlab's one.
The eigenvectors matrix has the same numbers BUT different signs.
I need to get the same matrix V in Scilab as in Matlab. Would you
please help me?
Thank you in advance
great

Loading...