Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

When running my program in both Mathematica and MathCad, I end up with the same eigenvalues, but different eigenvectors. The ones in MathCad are normalized, which the documentation for Mathematica says they too are normalized. I also tried Normalize[] and they still differed. Any advice for how to change it so that they match up?

EDIT:: Code

{{0. + 0. I, -0.00105652 + 0.00306633 I, -0.000992072 - 0.000550179 I,
   0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, -0.00080248 - 0.000276499 I, 
  3.94975*10^-6 - 7.12212*10^-6 I, 
  0.0000192495 + 6.63251*10^-6 I, -7.8995*10^-6 + 
   0.0000142442 I, -2.53122*10^-6 - 2.0506*10^-6 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I}, {0.00119848 - 0.00323969 I, 
  0. + 0. I, -0.00089753 + 0.00242618 I, -0.000743608 - 0.000434551 I,
   0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, -1.5511*10^-6 + 8.53409*10^-6 I, -0.000868186 - 
   0.000321173 I, 8.75277*10^-6 - 0.0000149778 I, 
  0.0000203377 + 7.52365*10^-6 I, -8.75277*10^-6 + 
   0.0000149778 I, -2.64607*10^-6 - 2.24413*10^-6 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I}, {-0.00124465 - 0.00025056 I, 0.000991057 - 0.00253121 I,
   0. + 0. I, -0.000658751 + 0.00168248 I, -0.000461387 - 
   0.000281434 I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 
  0.0000212182 + 8.30765*10^-6 I, -3.59755*10^-6 + 
   0.0000178707 I, -0.000909167 - 0.00035597 I, 
  9.49275*10^-6 - 0.0000155626 I, 
  0.0000212182 + 8.30765*10^-6 I, -9.49275*10^-6 + 
   0.0000155626 I, -2.73555*10^-6 - 2.41019*10^-6 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 
  0. + 0. I}, {0. + 0. I, -0.000915438 - 0.000197309 I, 
  0.000705296 - 0.00173145 I, 
  0. + 0. I, -0.00034951 + 0.000858025 I, -0.000154043 - 
   0.0000968679 I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 3.97422*10^-6 - 0.0000184388 I, 
  0.0000218358 + 8.89464*10^-6 I, -3.97422*10^-6 + 
   0.0000184388 I, -0.000935629 - 0.000381122 I, 
  0.000010041 - 0.0000159676 I, 
  0.0000218358 + 8.89464*10^-6 I, -0.000010041 + 
   0.0000159676 I, -2.79628*10^-6 - 2.53224*10^-6 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I}, {0. + 0. I, 
  0. + 0. I, -0.000555325 - 0.000123831 I, 
  0.000361803 - 0.000870433 I, 0. + 0. I, 
  6.4961*10^-6 - 0.0000156284 I, 0.000167434 + 0.000106958 I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, -3.83436*10^-6 - 1.7215*10^-7 I, 
  4.17683*10^-6 - 0.0000187311 I, 
  0.0000221515 + 9.20749*10^-6 I, -4.17683*10^-6 + 
   0.0000187311 I, -0.000949159 - 0.000394527 I, 
  0.0000103313 - 0.0000161729 I, 
  0.0000221515 + 9.20749*10^-6 I, -0.0000103313 + 
   0.0000161729 I, -2.82664*10^-6 - 2.59656*10^-6 I, 0. + 0. I, 
  0. + 0. I}, {0. + 0. I, 0. + 0. I, 
  0. + 0. I, -0.000180652 - 0.0000402588 I, -6.49204*10^-6 + 
   0.0000156244 I, 0. + 0. I, 0.000374561 - 0.000901456 I, 
  0.00049078 + 0.000313423 I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, -3.83319*10^-6 - 1.71599*10^-7 I, 
  4.17309*10^-6 - 0.0000187258 I, 
  0.0000221458 + 9.20173*10^-6 I, -4.17309*10^-6 + 
   0.0000187258 I, -0.000948914 - 0.00039428 I, 
  0.000010326 - 0.0000161692 I, 
  0.0000221458 + 9.20173*10^-6 I, -0.000010326 + 
   0.0000161692 I, -2.82609*10^-6 - 2.59538*10^-6 I, 
  0. + 0. I}, {0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0.000190732 + 0.0000410344 I, -0.000361392 + 0.000888151 I, 
  0. + 0. I, 0.000716521 - 0.00176091 I, 0.00080346 + 0.000504823 I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, -3.76634*10^-6 - 1.40845*10^-7 I, 
  3.96361*10^-6 - 0.0000184233 I, 
  0.0000218189 + 8.87822*10^-6 I, -3.96361*10^-6 + 
   0.0000184233 I, -0.000934908 - 0.000380418 I, 
  0.0000100257 - 0.0000159566 I, 
  0.0000218189 + 8.87822*10^-6 I, -0.0000100257 + 
   0.0000159566 I, -2.79465*10^-6 - 2.52885*10^-6 I}, {0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0.000541679 + 0.000108715 I, -0.00066847 + 0.00171026 I, 0. + 0. I, 
  0.000999783 - 0.00255791 I, 0.00109362 + 0.000666203 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, -3.63939*10^-6 - 8.56735*10^-8 I, 
  3.58173*10^-6 - 0.0000178461 I, 
  0.0000211913 + 8.28283*10^-6 I, -3.58173*10^-6 + 
   0.0000178461 I, -0.000908015 - 0.000354907 I, 
  9.46946*10^-6 - 0.0000155449 I, 
  0.0000211913 + 8.28283*10^-6 I, -9.46946*10^-6 + 
   0.0000155449 I}, {0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 
  0.000857822 + 0.000155255 I, -0.000904508 + 0.00245062 I, 0. + 0. I,
   0.00120425 - 0.00326273 I, 0.00135116 + 0.000788249 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, -3.46257*10^-6 - 1.56372*10^-8 I, 
  3.08334*10^-6 - 0.0000170362 I, 
  0.0000203025 + 7.49351*10^-6 I, -3.08334*10^-6 + 
   0.0000170362 I, -0.000869931 - 0.000321086 I, 
  8.72413*10^-6 - 0.0000149543 I, 
  0.0000203025 + 7.49351*10^-6 I}, {0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0.00112915 + 0.000178104 I, -0.00106069 + 0.00308684 I, 0. + 0. I, 
  0.0013247 - 0.00385516 I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, -3.24913*10^-6 + 5.87823*10^-8 I, 
  2.53158*10^-6 - 0.0000160499 I, 
  0.0000192081 + 6.60019*10^-6 I, -2.53158*10^-6 + 
   0.0000160499 I, -0.000819963 - 0.000281752 I, 
  3.93415*10^-6 - 7.10805*10^-6 I}, {0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0.00135075 + 0.000179137 I, -0.00114114 + 0.00360727 I, 0. + 0. I, 
  0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 0. + 0. I, 
  0. + 0. I, -3.01338*10^-6 + 1.28696*10^-7 I, 
  1.98264*10^-6 - 0.0000149497 I, 
  0.0000179729 + 5.68564*10^-6 I, -9.91319*10^-7 + 
   7.47487*10^-6 I, -0.000749264 - 0.000237026 I}, {1, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 1, 0, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 1, 0, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 1, 0, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 1, 0, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 1, 
  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
  1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
  0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
  0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
  0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 
  0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

I will work on getting the MathCad, but the first few values of the 19th eigenvector should be -.001048-.001033i, -.002461-.00404i...

share|improve this question
1  
Do you have degenerate eigenvalues? Then the eigenvectors would not be unique. In any case, it's not possible to answer this without seeing at least an example matrix and the MathCad output. Are you suspecting one of the systems is returning a wrong result? Did you verify that the eigenvectors are correct by substituting them back? –  Szabolcs May 30 '13 at 13:53
    
There should not be any degenerate eigenvalues. The ones from MathCad have been verified, so I can post the matrix and try to post the mathcad output. –  yankeefan11 May 30 '13 at 13:57
    
"Should not be" does not sound very confident. Why can't you just check if there are any instead of guessing that there aren't? –  Szabolcs May 30 '13 at 13:59
    
There aren't any. –  yankeefan11 May 30 '13 at 14:01
    
I just compared the eigenvectors between MATLAB and Mathematica for this matrix and they aren't the same either. It doesn't look right. I'll play with it a bit more to see if I've made any mistakes and I'll get back to you later. –  Szabolcs May 30 '13 at 14:08

2 Answers 2

I verified that Mathematica returns the correct set of eigenvalues and eigenvectors for this matrix by comparing them to MATLAB's output. I'll show how to do the comparison, as this might help reveal mistakes in your own comparison, if there were any.

(Note that if there are degenerate eigenvalues, then the eigenvectors are not unique, so there may be differences in the results obtained with different systems. This is not the case here.)

The matrix is stored in mat. To transfer the data from MATLAB to Mathematica, I'll use MATLink. You will need to do this differently for MathCad.

(* make MATLAB function callable from Mathematica *)
Needs["MATLink`"];
eig = MFunction["eig", "OutputArguments" -> 2]

Let's get the eigenvector and values from MATLAB:

{evec, eval} = eig[mat];

According to the MATLAB docs, evec is a matrix where each column is an eigenvector and eval is a diagonal matrix of the eigenvalues. Mathematica's Eigensystem returns a list of {eigenvalues, eigenvectors} where eigenvectors is a list of eigenvectors, or in other words: a matrix where each row (not column) is an eigenvector. To get MATLAB's output in the same format, we need to do {Diagonal[eval], Transpose[evec]}.

The two systems do not necessarily return the results in the same order so we must sort them. We can sort them based on eigenvalues. To do this, we pair up eigenvalues on eigenvectors, that is, transform

{{eval1, eval2, ...}, {evec1, evec2, ...}}

into

{{eval1, evec1}, {eval2, evec2}, ...}}

The Transpose function does precisely this. Then we can sort the tuples by the first element using SortBy[..., First]. It doesn't matter according to what rule the complex numbers are sorted---the purpose of sorting is to canonicalize the expressions so they can be compared.

So we need to compare

SortBy[Transpose[{Diagonal[eval], Transpose[evec]}], First] ==
    SortBy[Transpose@Eigensystem[mat], First]

This returns False. The reason is that the values are not precisely identical because of numerical errors. So instead let's do this:

SortBy[Transpose[{Diagonal[eval], Transpose[evec]}], First] - 
    SortBy[Transpose@Eigensystem[mat], First] // Chop

Chop replaces small numbers with exact zeros. This returns all zeros, confirming that the results are identical.

Try to do the same for comparing MathCad's output and see if it's the same or not. If it's not, MathCad might be buggy.

share|improve this answer
    
This still doesnt return the values that I should be getting. (For reference, I am trying to replicate data from a paper. We already have the values verified through MathCad so we are trying to use that to verify each step in mathematica) –  yankeefan11 May 30 '13 at 14:37
    
@James What I showed here is not how to compute eigenvalues, but how to compare the output across systems... Mathematica's result is very likely to be correct because it agrees with MATLAB's. –  Szabolcs May 30 '13 at 14:51
    
Gotcha. I have a copy of the printed values I should be getting in front of me. I also do not have a version of MathCad on my system, so I am not completely sure how to go about doing this –  yankeefan11 May 30 '13 at 14:53
    
@James, maybe you should have linked to the paper with the data you're verifying to begin with... –  J. M. May 31 '13 at 1:19
    
@James, I presume you don't want to reveal the paper you're taking the "correct" data from? –  J. M. Jun 3 '13 at 17:33

I verified that for your matrix (which I name m) holds:

m.ev = λ ev or m.ev - λ ev = 0:

(es[[1, #]] es[[2, #]] - m.es[[2, #]]) & /@  Range[Length@es[[1]]] // Chop

{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, << >> {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

The Eigenvectors are indeed normalized:

Norm /@ es[[2]]

{1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}

If you plot the Eigenvalues you can see that quite a few are very near to each other:

ListPlot[Transpose@Through[{Re, Im}[es[[1]]]]]

enter image description here

It's quite conceivable that MathCad yields different Eigenvectors corresponding to those Eigenvalues as this is a more or less degenerate situation, and any linear combination of those Eigenvectors will be (numerically close) to an Eigenvector itself.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.