Friday, June 24, 2016

POWER METHOD

Power method is used to find the dominant Eigen value and the corresponding Eigen vector. Eigen value problem generally arises in dynamics   programs and structural stability analysis. Power method is generally used to calculate these Eigen value and corresponding Eigen vector of the given matrix. This method can find only one Eigen value i.e. the greatest absolute value at a time.
          The utility of the power method is that it finds the Eigen-value of largest magnitude and its corresponding Eigen vector in a single and straight forward manner. It has the disadvantages that convergence is slow if there is a second Eigen value of nearly the same magnitude.

          The method works because e the Eigen vector is set of basis vectors. A set of basic vectors is said to be span the space, meaning the any x-component vector can be written as a unique linear combination of them.


Program code for Power method in C programming

(This program is written and compiled in TurboC++)


#include<stdio.h>
#include<math.h>
#define e 0.00001
int main()
{
    int n,i,j;
    float z[10],evaluemax,eigenvector[20][20],eigenvalue=0,sum,y[10], a[20][20],x[10],error;
    printf("Enter number  of equations");
    scanf("%d",&n);
    printf("Enter augumented matrix");
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n+1;j++)
        {
            printf("a[%d][%d]=",i,j);
            scanf("%d",&a[i][j]);
        }
    }
    printf("Enter initial guess for eigen vector");
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=1;j++)
            {
                scanf("%d",&eigenvector[i][j]);
            }
        }
    }
    do
    {
    for(i=1;i<n;i++)
    {
        x[i]=a[i][n+1]/a[i][i];
    }
    do
    {
    for(i=1;i<=n;i++)
    {
        sum=0;
        for(j=1;j<=1;j++)
        {
            if(i!=j)
                sum=sum+a[i][j]*x[j];
        }
        z[i]=sum;
    }
    }
    evaluemax=fabs(z[i]);

    for(i=1;i<=n;i++)
    {
        if(evaluemax<z[i])
            evaluemax=z[i];
    }
    while(error>e);
     error=fabs(eigenvalue-evaluemax);
     eigenvalue=evaluemax;
     printf("Eigen value and eigen vector %d", x[i]);
    }



Program code for Power method in C++ programming

(This program is written and compiled in code blocks)


#include<iostream>
#include<cmath>
using namespace std;

float findMax(float *arr, int size){
    float max = fabs(arr[0]);
    for (int i = 1; i < size; i++){
        if ( max < fabs(arr[i]))
            max = fabs(arr[i]);
    }
    return max;
}

int main(){
    float mat[30][30], vect[30], lem, z[30], sum, check;
    int size;
    cout<<"Enter the size of matrix (<30):";
    cin>>size;
    cout<<"Enter the matrix :"<<endl;
    for (int i = 0; i < size; i++){
        for (int j = 0; j < size; j++)
            cin>>mat[i][j];
    }
    cout<<"Enter the vector :"<<endl;
    for (int i = 0; i < size; i++)
        cin>>vect[i];

    do{
        for (int i = 0; i < size; i++){
            sum = 0;
            for (int j = 0; j < size; j++){
                sum += mat[i][j]*vect[j];
            }
            z[i] = sum;
        }

        lem = findMax(z, size);

        for (int i = 0; i < size; i++)
            z[i] /= lem;

        float diff[30];
        for (int i = 0; i < size; i++)
            diff[i] = vect[i]-z[i];
        check = findMax(diff, size);

        for (int i = 0; i < size; i++)
            vect[i] = z[i];

   }while(check > 0.1);
    cout<<"The required value is : "<<lem<<endl;
    return 0;

}

No comments:

Post a Comment