Hide
Don't get spied on – We respect your privacy and provide numerous options to protect it. Join Siafoo Now or Learn More

Eigenvectors and Eigenvalues Atom Feed 0

In Brief Numerically find the eigenvectors and eigenvalues of a matrix using the Power Method.
# 's
 1//powermethod.c
2//A program to find the largest eigenvector of a matrix
3//By David Isaacson, 2003
4//Released into the Public Domain
5
6#include <stdio.h>
7#include <math.h>
8
9int main ()
10{
11 float A[4][4]={{.8,.6,.4,.2},{.6,1.2,.8,.4},{.4,.8,1.2,.6},{.2,.4,.6,.8}};
12 float newx[4]={1,1,1,1};
13 float oldx[4]={0,0,0,0};
14 float eigen;
15 int i,j,done=0;
16
17 while (!done)
18 //repeat until all values within 10%
19 {
20 //set old=new
21 for(i=0;i<4;i++)
22 oldx[i]=newx[i];
23
24
25 //multiply
26 for(i=0;i<4;i++)
27 {
28 newx[i]=0;
29 for(j=0;j<4;j++)
30 {
31 newx[i]=newx[i]+(A[i][j])*(oldx[j]);
32 }
33 }
34
35 //normalize
36 j=0; //j=smallest value
37 for(i=1;i<4;i++)
38 {
39 if (fabs(newx[j])>fabs(newx[i])&&newx[1]!=0)
40 j=i;
41 }
42 eigen=fabs(newx[j]);
43 for(i=0;i<4;i++)
44 newx[i]=newx[i]/eigen;
45
46 if ((fabs(((newx[0]-oldx[0])/oldx[0]))<=0.1)
47 &&(fabs(((newx[1]-oldx[1])/oldx[1]))<=0.1)
48 &&(fabs(((newx[2]-oldx[2])/oldx[2]))<=0.1)
49 &&(fabs(((newx[3]-oldx[3])/oldx[3]))<=0.1))
50 {
51 done=1;
52 }
53
54
55 }
56
57 printf("The eigenvector is about ");
58 printf("{%f,%f,%f,%f}^T\n",newx[0],newx[1],newx[2],newx[3]);
59 printf("The largest eigenvalue is about %f",eigen);
60
61 return 0;
62}
63
64/*OUTPUT:
65
66The eigenvector is about {1.000000,1.600000,1.600000,1.000000}^T
67The largest eigenvalue is about 2.500000
68
69*/
70

Numerically find the eigenvectors and eigenvalues of a matrix using the Power Method.