C Program for Power Method is used to find the dominant eigen value and the corresponding eigen vector. Eigen value problems generally arise in dynamics problems and structural stability analysis. Power method is generally used to calculate these eigen value and corresponding eigen vector of the given matrix.
The C program for power method is just a programming illustration of the power method as one of the most well suited iterative approach for machine computations. With this, the numerically greatest eigen value and the subsequent eigen vector can be computed to analyze different engineering problems.
Before going into the program for Power Method in C language, let’s look at a simple mathematical formulation of eigen values and eigen vector. For this, consider a matrix A. We have to find the column vector X and the constant L (L=lamda) such that:
[A]{X} = L{X}
Now, consider these three set of equations:
a11x1 + a12x2 + a13x3 = Lx1
a12x1 + a22x2 + a23x3 = Lx2
a31x1 + a32x2 + a33x3 = Lx3
These equations can be written as:
(a11-L)x1 +a12x2 +a13x3 = 0
a21x1 +(a22-L)x2 +a23x3 = 0
a31x1 +a32x2 + (a33-L)x3 = 0
Now the determinant of the 3*3 matrix formed of the coefficients of x1, x2 and x3 terms gives three roots, namely L1, L2 and L3 (read L as lamda). These values are called characteristic or eigen values. For each of these values, we get a set of column vector with elements x1, x2 and x3. This vector is the required eigen vector.
The Power method C program given below utilizes continuous approximation of L (lamda) to the eigen value and X to the eigen vector. For this method, the matrix should be symmetric or positive definitive i.e. aij = aji.
Source Code for Power Method in C:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n;
float A[40][40],x[40],z[40],e[40],zmax,emax;
printf("\nEnter the order of matrix:");
scanf("%d",&n);
printf("\nEnter matrix elements row-wise\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=n; j++)
{
printf("A[%d][%d]=", i,j);
scanf("%f",&A[i][j]);
}
}
printf("\nEnter the column vector\n");
for(i=1; i<=n; i++)
{
printf("X[%d]=",i);
scanf("%f",&x[i]);
}
do
{
for(i=1; i<=n; i++)
{
z[i]=0;
for(j=1; j<=n; j++)
{
z[i]=z[i]+A[i][j]*x[j];
}
}
zmax=fabs(z[1]);
for(i=2; i<=n; i++)
{
if((fabs(z[i]))>zmax)
zmax=fabs(z[i]);
}
for(i=1; i<=n; i++)
{
z[i]=z[i]/zmax;
}
for(i=1; i<=n; i++)
{
e[i]=0;
e[i]=fabs((fabs(z[i]))-(fabs(x[i])));
}
emax=e[1];
for(i=2; i<=n; i++)
{
if(e[i]>emax)
emax=e[i];
}
for(i=1; i<=n; i++)
{
x[i]=z[i];
}
}
while(emax>0.001);
printf("\n The required eigen value is %f",zmax);
printf("\n\nThe required eigen vector is :\n");
for(i=1; i<=n; i++)
{
printf("%f\t",z[i]);
}
getch();
}
Input/Output:
Also see,
Power Method MATLAB Program
Power Method Algorithm/Flowchart
Numerical Methods Tutorial Compilation
Power method is a well-known technique to solve an equation. If you are having a large number of equations and you want to solve them all at once, then this method will be the best solution for you.
Let’s say you have a equation like, 2x – 7y = 13
In this case, you have to divide the entire equation by 2, but you don’t know what x and y will be. If you divide by x, you will get 2x – 7y/x = 13/x, and if you divide by y, you will get 2x – 7y/y = 13/y.
If you have divided by x and y, then you can solve the equation, and if you have divided by just one variable, then you can use the C program for power method.
How it works?
Let’s assume that you have the above mentioned equation, now you need to find the values of x and y. For this, you need to divide the entire equation by the number of variables in the equation.
For example, if you have to divide by x, you will get, 2x – 7y/x = 13/x.
If you divide by y, you will get, 2x – 7y/y = 13/y.
In this case, you will get the value of x and y by dividing the entire equation by the number of variables in the equation.
The power method is a simple and easy way to solve the equations, and it is not possible without this method.
Now, you know how the power method works and you can easily use it to solve any programming problem. If you are having any problems, then you can use this C program for power method as mentioned above.
This C source code for Power Method is complete and error-free. You need to compile this program in Code::Blocks IDE to get the output as shown above. Any questions regarding power method, how it calculate eigen value or how the aforementioned program works can be discussed in the comments.