Tuesday, February 01, 2011

Power Method to find the maximum Eigenvalue of a 3 x 3 matrix


Source Code:

#include "stdio.h"

float matrix[3][3];
float col[3];
float max_eig_val1 = 1.0;
float max_eig_val2 = 1.0;

void mul() {
float x,y,z;
x = matrix[0][0]*col[0]+matrix[0][1]*col[1]+matrix[0][2]*col[2] ;
y = matrix[1][0]*col[0]+matrix[1][1]*col[1]+matrix[1][2]*col[2] ;
z = matrix[2][0]*col[0]+matrix[2][1]*col[1]+matrix[2][2]*col[2] ;
col[0]=x ; col[1]=y ; col[2]=z ;

}

float max(float a,float b) {
if(a>b) return a;
else return b;
}

void normalize() {
max_eig_val2 = max(max(col[0],col[1]),col[2]);
int i;
for(i=0;i<3;i++)
col[i] = col[i]/max_eig_val2 ;

}

void main() {
int i,j;
printf("\n***** POWER METHOD FOE FINDIND LARGEST EIGEN VALUE OF A 3X3 MATRIX ******\n\n");
printf("Enter a 3 x 3 matrix :\n\n\t");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
scanf("%f",&matrix[i][j]);

printf("\nSupplied 3x3 matrix is :");
printf("\n_______________________________");
printf("\n");
for(i=0;i<3;i++){
for(j=0;j<3;j++)
printf("%f ",matrix[i][j]);
printf("\n"); }
printf("_______________________________\n");
for(i=0;i<3;i++)
col[i]=1.0;

for(i=0;i<12;i++) {
mul();
normalize();
}

printf("\n\tCorresponding Eigenvector is: \n\t [");
for(i=0;i<3;i++)
printf("%f ",col[i]);
printf(" ]");
printf("\n\n\tThe largest eigen value is approximately %f",max_eig_val2);
}


Output: 


A tutorial is available at http://www.miislita.com/information-retrieval-tutorial/matrix-tutorial-3-eigenvalues-eigenvectors.html#power-method


No comments:

Post a Comment

Do you think this information useful or was not up to the mark? Comment if you have any advices or suggestions about the post.