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:ut);
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



LinkBack URL
About LinkBacks
ut);



