Power Method Using C Programming for Finding Dominant Eigen Value and Eigen Vector

Earlier in Power Method Algorithm for Finding Dominant Eigen Value and Eigen Vector and Power Method Pseudocode for Finding Dominant Eigen Value and Eigen Vector, we discussed about an Algorithm and Pseudocode for finding largest or dominant Eigen value and corresponding Eigen vector using Power Method. In this tutorial we are going to implement this method using C programming language.

Power Method Using C Programming Language


#include<stdio.h>
#include<conio.h>
#include<math.h>

#define SIZE 10

int main()
{
	 float a[SIZE][SIZE], x[SIZE],x_new[SIZE];
	 float temp, lambda_new, lambda_old, error;
	 int i,j,n, step=1;
	 clrscr();
	 /* Inputs */
	 printf("Enter Order of Matrix: ");
	 scanf("%d", &n);
	 printf("Enter Tolerable Error: ");
	 scanf("%f", &error);
	 /* Reading Matrix */
	 printf("Enter Coefficient of Matrix:\n");
	 for(i=1;i<=n;i++)
	 {
		  for(j=1;j<=n;j++)
		  {
			   printf("a[%d][%d]=",i,j);
			   scanf("%f", &a[i][j]);
		  }
	 }
	 /* Reading Intial Guess Vector */
	 printf("Enter Initial Guess Vector:\n");
	 for(i=1;i<=n;i++)
	 {
		  printf("x[%d]=",i);
		  scanf("%f", &x[i]);
	 }
	 /* Initializing Lambda_Old */
	 lambda_old = 1;
	 /* Multiplication */
	 up:
	 for(i=1;i<=n;i++)
	 {
		  temp = 0.0;
		  for(j=1;j<=n;j++)
		  {
		   	temp = temp + a[i][j]*x[j];
		  }
		  x_new[i] = temp;
	 }
	 /* Replacing */
	 for(i=1;i<=n;i++)
	 {
	  	x[i] = x_new[i];
	 }
	 /* Finding Largest */
	 lambda_new = fabs(x[1]);
	 for(i=2;i<=n;i++)
	 {
		  if(fabs(x[i])>lambda_new)
		  {
		   	lambda_new = fabs(x[i]);
		  }
	 }
	 /* Normalization */
	 for(i=1;i<=n;i++)
	 {
	  	x[i] = x[i]/lambda_new;
	 }
	 /* Display */
	 printf("\n\nSTEP-%d:\n", step);
	 printf("Eigen Value = %f\n", lambda_new);
	 printf("Eigen Vector:\n");
	 for(i=1;i<=n;i++)
	 {
	  	printf("%f\t", x[i]);
	 }
	 /* Checking Accuracy */
	 if(fabs(lambda_new-lambda_old)>error)
	 {
		  lambda_old=lambda_new;
		  step++;
		  goto up;
	 }
	 getch();
	 return(0);
}

Ouput: Power Method Using C Programming

 Enter Order of Matrix: 2 ↲
 Enter Tolerable Error: 0.001 ↲
 Enter Coefficient of Matrix:
 a[1][1]=5 ↲
 a[1][2]=4 ↲
 a[2][1]=1 ↲
 a[2][2]=2 ↲
 Enter Initial Guess Vector:
 x[1]=1 ↲
 x[2]=1 ↲
 
 STEP-1:
 Eigen Value = 9.000000
 Eigen Vector:
 1.000000    0.333333

 STEP-2:
 Eigen Value = 6.333333
 Eigen Vector:
 1.000000    0.263158

 STEP-3:
 Eigen Value = 6.052631
 Eigen Vector:
 1.000000    0.252174

 STEP-4:
 Eigen Value = 6.008696
 Eigen Vector:
 1.000000    0.250362

 STEP-5: 
 Eigen Value = 6.001447
 Eigen Vector:
 1.000000    0.250060

 STEP-6:
 Eigen Value = 6.000241
 Eigen Vector:
 1.000000    0.250010

 STEP-7:
 Eigen Value = 6.000040
 Eigen Vector:
 1.000000    0.250002
 
 Note: ↲ indicates ENTER is pressed.

Recommended Readings

  1. Power Method Algorithm for Finding Dominant Eigen Value and Eigen Vector
  2. Power Method Pseudocode for Finding Dominant Eigen Value and Eigen Vector
  3. Power Method Using C Programming for Finding Dominant Eigen Value and Eigen Vector
  4. Power Method Using C++ Programming (Finding Dominant Eigen Value and Eigen Vector)