You are on page 1of 4

Eigenvalue Extraction by Power Method

#include<stdio.h> #include<conio.h> #include<math.h> int main() { printf("\n Eigen Value Extraction Power Method \n"); // variable declaration int i,j,k,nv; printf("\nEnter the size of the matrix:\t"); scanf("%d",&nv); float matA[30][30],err,temp,total=0; float b[30],c[30],d[30],max,con,min=0; for(i=0;i<nv;i++) { // Getting the coefficients of the matrix for(j=0;j<nv;j++) { printf("\nEnter the element[%d][%d]: ",i+1,j+1); scanf("%f",&matA[i][j]); } } // Getting the trial vector top: printf("\nEnter the trial vector:\t"); for(i=0;i<nv;i++) {

printf("\nEnter the trial vector row%d:\t", i+1); scanf("%f",&c[i]); } // Getting the error limit printf("\nEnter the error limit: "); scanf("%f",&err); for(i=0;i<nv;i++) d[i]=0; // logic to find the eigenvalues and eigenvectors do{ for(i=0;i<nv;i++) b[i]=0; for(i=0;i<nv;i++) { for(j=0;j<nv;j++) b[i]+=matA[i][j]*c[j]; } max=0; for(i=0;i<nv;i++) { if(fabsl(b[i])> fabsl(max)) max=b[i]; } con=0; for(i=0;i<nv;i++) { b[i]/=fabsl(max);

c[i]=b[i]; if(fabsl(fabsl(b[i])-fabsl(d[i]))<err) con+=0; else con+=1; d[i]=b[i]; } }while(con!=0); //Printing the Eigenvalues and Eigenvectors if (min==0) { printf("\nThe maximum Eigenvalue is "); temp=max; } else { printf("\nThe minimum Eigenvalue is "); max=temp-max; } printf("%f",max); for(i=0;i<nv;i++) { printf("\n Corresponding Eigenvector A%d is %f",i+1,b[i]); } if (min==0) { for(i=0;i<nv;i++)

for(j=0;j<nv;j++) if(i==j) { total+=matA[i][j]; matA[i][j]-=max; } min=1; printf("\n\nTo find the minimum Eigenvalue: \n"); goto top; } if(nv==3) { total-=(max+temp); printf("\n\nThe other Eigenvalue is %f", total); } getch(); return 0; }

You might also like