Thread: Eigen values and vectors

  1. #1
    Mr. Eigen
    Guest

    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: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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How properly get data out of vectors of templates?
    By 6tr6tr in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2008, 10:35 AM
  2. Program moving values in vectors... need help.
    By m_ziolo_86 in forum C Programming
    Replies: 11
    Last Post: 01-15-2006, 10:50 PM
  3. how do I set vectors values to one value?
    By fe00h in forum C Programming
    Replies: 11
    Last Post: 07-02-2003, 01:31 PM
  4. More fun with vectors
    By hpy_gilmore8 in forum C++ Programming
    Replies: 6
    Last Post: 05-14-2003, 05:52 AM
  5. Points, vectors, matrices
    By subnet_rx in forum Game Programming
    Replies: 17
    Last Post: 01-11-2002, 02:29 PM