Eigen Value and Eigen Vector of a matrix by Iterative Method- C++ Program

using namespace std;
int main()
    int n,i,j;
    cout<<"\nEnter the order of the matrix:\n";        //Get the order of the square matrix
    double a[n][n],b[n],c[n],k,eps,y;            //declare arrays and other variables to be used
    cout<<"\nEnter the elements of matrix row-wise:\n";
    for (i=0;i<n;i++)                    //Get the elements of the matrix
        for (j=0;j<n;j++)
    cout<<"\nEnter the initial values of the eigen-vector:\n";
    for (i=0;i<n;i++)                    //Get the iinitial guess for the eigen vector
    cout<<"\nEnter the accuracy desired:\n";       
    k=b[0];                            //Assign some initial value to the eigen value, 'k'
        for (i=0;i<n;i++)                //Calculate axb where a is the matrix and b its eigen vector
            for (j=0;j<n;j++)
                c[i]=c[i]+a[i][j]*b[j];        //After all the iterations axb=c
        for (i=1;i<n;i++)
            k=abs(c[i])>k?abs(c[i]):k;        //Find the largest element of c and assign it to k where k is the eigen value
        for (i=0;i<n;i++)
            b[i]=c[i]/k;                //Calculate the new Eigen Vector
    }while (abs(k-y)>eps);                    //Check if the error in eigen value is within the tolerance limit
    cout<<"The eigen-value is: "<<k<<endl;
    cout<<"\nAnd the Eigen-vector is [";
    for (i=0;i<n;i++)
    return 0;

c++ program eigen value eigen vector eigenvalue eigenvector iterative method numerical analysis
Sample Output

PhD researcher at Friedrich-Schiller University Jena, Germany. I'm a physicist specializing in theoretical, computational and experimental condensed matter physics. I like to develop Physics related apps and softwares from time to time. Can code in most of the popular languages. Like to share my knowledge in Physics and applications using this Blog and a YouTube channel.

One thought on “Eigen Value and Eigen Vector of a matrix by Iterative Method- C++ Program

  1. Hi My name is Milad, I need code c++ for computing sparse matrix and Eigenvalue and eigenvector, can you help me?

Leave a Reply

Your email address will not be published. Required fields are marked *