# Eigen values and vectors

• 11-19-2001
Mr. Eigen
Eigen values and vectors
I have an assignment where I have to determine both the eigen values and vectors of a 3 x 3 array :

3
2 -1 4
1 3 2
8 1 3

First number is the dimension of the array. This is what I have reached to:

#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <math.h>
#include <stdlib.h>
int main ()
{
ifstream inFile ("ineigen.txt" , ios::in);
ofstream outFile ("outeigen.txt" , ios::out);

int i,j,dim,x[3]={1,0,0},y[3],A[3][3];

float product,lambda,newx[3],oldx[3],tol=2,tolarray[3];

inFile>>dim;

for (i=0 ; i<dim ; i++)
for (j=0 ; j<dim ; j++)
inFile>>A[i][j];

for (i=0 ; i<dim ; i++)
newx[i]=x[i];

for (i=0 ; i<dim ; i++)
oldx[i]=x[i];

while (tol>=0.0001){

for (i=0 ; i<dim ; i++){
product=0;
for (j=0 ; j<dim ; j++)
product += A[i][j] * newx[j];
y[i]=product;
}

product=0;

for(i=0 ; i<dim ; i++)
product += y[i] * y[i];
cout<<product<<endl;

lambda = pow(product , 0.5);

for (i=0 ; i<dim ; i++)
newx[i]=y[i]/lambda;

for(i=0 ; i<dim ; i++)
outFile<<setprecision (4)<<setiosflags (ios::fixed | ios::showpoint)<<newx[i]<<" ";

outFile<<lambda<<" ";

for (i=0 ; i<dim ; i++)
tolarray[i]=abs (newx[i]-oldx[i]);

for (i=0 ; i<dim ; i++)
tol += tolarray[i]/3;

for(i=0 ; i<dim ; i++)
oldx[i]=newx[i];

}

return 0;

}

________________________

I just cant get the tolerance right, its supposed to be the average of all components of | x - x1 |.

Thnxx
• 11-19-2001
Salem
Dunno - but when I compiled it, I got a couple of warnings which don't look too good in the light of trying to converge floating point values
Code:

```#include <iostream.h> #include <iomanip.h> #include <fstream.h> #include <math.h> #include <stdlib.h> int main() {     ifstream inFile( "ineigen.txt" , ios::in );     ofstream outFile( "outeigen.txt" , ios::out );     int i,j,dim,x[3]={1,0,0},y[3],A[3][3];     float product,lambda,newx[3],oldx[3],tol=2,tolarray[3];     inFile>>dim;     for (i=0 ; i<dim ; i++) {         for (j=0 ; j<dim ; j++)             inFile>>A[i][j];     }     for (i=0 ; i<dim ; i++)         newx[i]=x[i];     for (i=0 ; i<dim ; i++)         oldx[i]=x[i];     while (tol>=0.0001){         for (i=0 ; i<dim ; i++){             product=0;             for (j=0 ; j<dim ; j++)                 product += A[i][j] * newx[j];             y[i]=product; // warning: assignment to `int' from `float'         }         product=0;         for(i=0 ; i<dim ; i++)             product += y[i] * y[i];         cout<<product<<endl;         lambda = pow( product , 0.5 );         for (i=0 ; i<dim ; i++)                                    newx[i]=y[i]/lambda;         for(i=0 ; i<dim ; i++)             outFile<<setprecision( 4 )<<setiosflags( ios::fixed | ios::showpoint )<<newx[i]<<"  ";         outFile<<lambda<<" ";         for (i=0 ; i<dim ; i++)             tolarray[i]=abs( newx[i]-oldx[i] ); // warning: `float' used for argument 1 of `abs(int)'         for (i=0 ; i<dim ; i++)             tol += tolarray[i]/3;         for(i=0 ; i<dim ; i++)             oldx[i]=newx[i];     }     return 0; }```