This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen solver test! Eigen vs MATLAB

Tags: None
(comma "," separated)
clockdate
Registered Member
Posts
2
Karma
0

Eigen solver test! Eigen vs MATLAB

Thu Jan 10, 2013 2:45 am
Do you know the reason and the answer? Eigen vs MATLAB.

Code: Select all
// EigenTest.cpp : Console Application Program ...

// Microsoft Visual Studio 2008 Professional Version 9.0.30729.4108 QFE
// Microsoft Visual C++ 2008   91987-270-9525491-60785
// Microsoft .NET Framework Version 3.5 Service Pack 1

#include "stdafx.h"

#include <iostream>
#include "eigen-eigen-5097c01bcdc4/Eigen/Dense"  // Eigen 3.1.2
using namespace std;
using namespace Eigen;

int _tmain(int argc, _TCHAR* argv[])
{
  MatrixXd A(8,8);
 
  A << 2.950913630407e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-1.968359308416e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,
       0.000000000000e+00, 1.984757466434e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-9.223839305535e+06, 0.000000000000e+00, 0.000000000000e+00,
       0.000000000000e+00, 0.000000000000e+00, 6.228049879001e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-3.233356205885e+07, 0.000000000000e+00,
       0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 5.429737151217e+04, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-1.520405655416e+04,
       1.289679115083e+06, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 5.744737939091e+05, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,
       0.000000000000e+00,-1.554071149030e+09, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 1.150658180459e+09, 0.000000000000e+00, 0.000000000000e+00,
       0.000000000000e+00, 0.000000000000e+00,-4.108705467029e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 3.522510184598e+07, 0.000000000000e+00,
       0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-2.332838417285e+04, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 4.432994794974e+04;
 
  cout << "Here is the matrix A:\n" << A << endl;
  SelfAdjointEigenSolver<MatrixXd> eigensolver(A);
  if (eigensolver.info() != Success) abort();
  cout << "The eigenvalues of A are:\n" << eigensolver.eigenvalues() << endl;
  cout << "Here's a matrix whose columns are eigenvectors of A \n"
  << "corresponding to these eigenvalues:\n"
  << eigensolver.eigenvectors() << endl;

  return 0;
}


The Result is ...

Code: Select all
C:\File\Visual Studio 2008\Projects\EigenTest\Debug>EigenTest.exe
Here is the matrix A:
 2.95091e+007             0             0             0 -1.96836e+007             0             0             0
            0  1.98476e+007             0             0             0 -9.22384e+006             0             0
            0             0  6.22805e+007             0             0             0 -3.23336e+007             0
            0             0             0       54297.4             0             0             0      -15204.1
 1.28968e+006             0             0             0        574474             0             0             0
            0 -1.55407e+009             0             0             0  1.15066e+009             0             0
            0             0 -4.10871e+007             0             0             0  3.52251e+007             0
            0             0             0      -23328.4             0             0             0       44329.9
The eigenvalues of A are:
-1.06848e+009
25458.9
73168.4
517104
5.49607e+006
2.95665e+007
9.20095e+007
2.23898e+009
Here's a matrix whose columns are eigenvectors of A
corresponding to these eigenvalues:
           -0             0            -0       0.04444             0      0.999012             0             0
     0.819115             0            -0             0             0             0             0      -0.57363
  9.9509e-019 -4.13558e-016   5.6991e-016             0      0.586204             0     -0.810164  1.42094e-018
           -0       0.62892      -0.77747             0  1.24903e-015             0 -2.01092e-016             0
           -0             0            -0     -0.999012             0       0.04444             0             0
      0.57363  -4.4399e-019 -3.32525e-019             0 -1.64149e-018             0 -8.41852e-020      0.819115
           -0 -6.14478e-016   6.3386e-016             0      0.810164             0      0.586204             0
           -0       0.77747       0.62892             0   2.1126e-017             0 -4.80373e-018             0

C:\File\Visual Studio 2008\Projects\EigenTest\Debug>



But, MATLAB...
Code: Select all
>> A=[2.950913630407e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-1.968359308416e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00;
      0.000000000000e+00, 1.984757466434e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-9.223839305535e+06, 0.000000000000e+00, 0.000000000000e+00;
      0.000000000000e+00, 0.000000000000e+00, 6.228049879001e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-3.233356205885e+07, 0.000000000000e+00;
      0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 5.429737151217e+04, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-1.520405655416e+04;
      1.289679115083e+06, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 5.744737939091e+05, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00;
      0.000000000000e+00,-1.554071149030e+09, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 1.150658180459e+09, 0.000000000000e+00, 0.000000000000e+00;
      0.000000000000e+00, 0.000000000000e+00,-4.108705467029e+07, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 3.522510184598e+07, 0.000000000000e+00;
      0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00,-2.332838417285e+04, 0.000000000000e+00, 0.000000000000e+00, 0.000000000000e+00, 4.432994794974e+04];
>> A

A =

  1.0e+009 *

    0.0295         0         0         0   -0.0197         0         0         0
         0    0.0198         0         0         0   -0.0092         0         0
         0         0    0.0623         0         0         0   -0.0323         0
         0         0         0    0.0001         0         0         0   -0.0000
    0.0013         0         0         0    0.0006         0         0         0
         0   -1.5541         0         0         0    1.1507         0         0
         0         0   -0.0411         0         0         0    0.0352         0
         0         0         0   -0.0000         0         0         0    0.0000

>> A(8,8)

ans =

  4.4330e+004

>> [X,Y]=eig(A);
>> Y

Y =

  1.0e+009 *

    0.0286         0         0         0         0         0         0         0
         0    0.0015         0         0         0         0         0         0
         0         0    0.0876         0         0         0         0         0
         0         0         0    0.0099         0         0         0         0
         0         0         0         0    0.0073         0         0         0
         0         0         0         0         0    1.1632         0         0
         0         0         0         0         0         0    0.0001         0
         0         0         0         0         0         0         0    0.0000

>> X

X =

    0.9989   -0.5747         0         0         0         0         0         0
         0         0         0         0    0.5926    0.0081         0         0
         0         0    0.7870    0.5251         0         0         0         0
         0         0         0         0         0         0    0.7237    0.5278
    0.0460   -0.8184         0         0         0         0         0         0
         0         0         0         0    0.8055   -1.0000         0         0
         0         0   -0.6170    0.8510         0         0         0         0
         0         0         0         0         0         0   -0.6901    0.8493

>>



Can you help me?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your matrix is not self-adjoint, so you must use EigenSolver<MatrixXd>.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]