# Thread: Eigenvalues for 2x2 Matrix

1. ## Eigenvalues for 2x2 Matrix

This is an example I got about eigenvalues...

Maybe a specific example, suppose you have the matrix A:
[ 2 4 ]
[ 3 5 ]
We turn this into the matrix A-xI
[ 2-x 4 ]
[ 3 5-x]
This has determinant det (A-xI) = (2-x)(5-x)-(3)(4) = x^2-7x-2. You can then solve this quadratic equation using whatever method you like to use to solve quadratic equations. Note: you did this in your linear algebra class, so dig that book back out.

---------------------

Here is my code:

Code:
```#include <stdio.h>
int main()
{

float a[2][3], b[2][1], mean[2][1], after[2][3], afterT[3][2], cov[2][2];

int i, j, k, h, p;

printf("\nEnter three 2D vectors in form of 2x3 matrix A: ");

for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
printf("\n Enter a[%d][%d]: ", i,j);
scanf("%f", &a[i][j]);
}
}

/*Compute the Mean*/

b[0][0]= a[0][0]+a[0][1]+a[0][2];
b[1][0]= a[1][0]+a[1][1]+a[1][2];

for(i=0; i<2; i++)
{
for(j=0; j<1; j++)
{
mean[i][j]= b[i][j]*(1.0/3.0);
}
}

/*Subtract each vector by the mean vector*/

for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
for(k=0; k<1; k++)
{
after[i][j]=a[i][j]-mean[i][k];
}
}
}

/*Transpose after (afterT)*/

for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
{
afterT[j][i]=after[i][j];
}
}

/*Compute and Display Covariance*/

cov[0][0]= (after[0][0]*afterT[0][0])+(after[0][1]*afterT[1][0])+(after[0][2]*afterT[2][0]);
cov[0][1]= (after[0][0]*afterT[0][1])+(after[0][1]*afterT[1][1])+(after[0][2]*afterT[1][2]);
cov[1][0]= (after[1][0]*afterT[0][0])+(after[1][1]*afterT[1][0])+(after[1][2]*afterT[2][0]);
cov[1][1]= (after[1][0]*afterT[0][1])+(after[1][1]*afterT[1][1])+(after[1][2]*afterT[2][1]);

cov[0][0]=(.5)*cov[0][0];
cov[0][1]=(.5)*cov[0][1];
cov[1][0]=(.5)*cov[1][0];
cov[1][1]=(.5)*cov[1][1];

printf("\n Their product is :\n");

for(i=0; i<2; i++)
{
for(j=0; j<2; j++)
{
printf("%.1f ", cov[i][j]);
}
printf("\n");
}

/*Determinants*/

cov[0][0]=cov[0][0]-x;
cov[1][1]=cov[1][1]-x;

det=(cov[0][0]*cov[1][1])-(cov[1][0]*cov[0][1])

return(0);
}```

The thing I can't figure out is because the eigenvalues are the roots of the determinant, how to compute the eigenvalues...As seen above I have variable "det" as the determinant of the 2x2 covariance matrix....

can someone help? thanks

PS- I can only use the library functions sqrt, printf, scanf

2. Apply the elementary formula of:
r1 = (-b+sqrt(D))/(2*a)
r2 = (-b-sqrt(D))/(2*a)
Replace a,b,D for what you have as coefficients and determinant.
a*x^2+b*y+c=0

Call sqrt on det to find it's square root.

3. but from what I have as the determinant which is: (a-x)(b-x)-(c)(d) where a,b,c,d are the numbers from the matrix, how do I factor that out, or do I just apply the formula above as it is?

5. the coefficients will change because the user input can put whatever they want in the 2x3 matrix...so how would it I know what coefficients a and b to put into those formulas?

6. Originally Posted by dcwang3
the coefficients will change because the user input can put whatever they want in the 2x3 matrix...so how would it I know what coefficients a and b to put into those formulas?
You have to think for fifteen bloody seconds. Write out a 2x2 matrix using "alpha", "beta", "gamma", and "delta" as your four numbers. Put the -x in the diagonal entries, find the determinant, and expand. You'll get the coefficients of the quadratic polynomial in terms of the entries of the matrix.

Edit to add: And note that x is the variable we're trying to find -- it is the eigenvalue; it doesn't appear before or afterwards. (Traditionally, we use lambda, but I don't have a lambda key on my keyboard.) So subtracting x, as you have it in the code, won't work, since we can't have a polynomial stored in a float variable. We need to build our quadratic equation and find x.

7. yes, I understand that, but the thing that I have a problem with is how to set it up using the equations:

r1 = (-b+sqrt(D))/(2*a)
r2 = (-b-sqrt(D))/(2*a)

because I don't know how to expand the determinant "DET" in C. Do I just put it in the code like this...

Code:
```/*Determinants*/

cov[0][0]=cov[0][0]-x;
cov[1][1]=cov[1][1]-x;

det=(cov[0][0]*cov[1][1])-(cov[1][0]*cov[0][1])

r1 = (-b+sqrt(det))/(2*a) ??????
r2 = (-b-sqrt(det))/(2*a) ???????```
My problem is knowing what b and a are corresponding to in that quadratic question. If there is a way to factor it out in C, and then assign like a variable to it, then I would, but I don't know, that is why I am asking you....

I know how to find use the quadratic equation, it's just knowing what I can do in C to implement it....

8. also, write out a 4x4 matrix using "alpha", "beta", "gamma", and "delta" as your four numbers?? the last time I could recall is that a 4x4 has 16 entries, thus 16 numbers, unless you are talking about identity matrix...

9. Originally Posted by dcwang3
yes, I understand that, but the thing that I have a problem with is how to set it up using the equations:

r1 = (-b+sqrt(D))/(2*a)
r2 = (-b-sqrt(D))/(2*a)

because I don't know how to expand the determinant "DET" in C. Do I just put it in the code like this...

Code:
```/*Determinants*/

cov[0][0]=cov[0][0]-x;
cov[1][1]=cov[1][1]-x;

det=(cov[0][0]*cov[1][1])-(cov[1][0]*cov[0][1])

r1 = (-b+sqrt(det))/(2*a) ??????
r2 = (-b-sqrt(det))/(2*a) ???????```
My problem is knowing what b and a are corresponding to in that quadratic question. If there is a way to factor it out in C, and then assign like a variable to it, then I would, but I don't know, that is why I am asking you....

I know how to find use the quadratic equation, it's just knowing what I can do in C to implement it....
Do you really not know how to use +, -, /, (), and sqrt to express a formula?

Write down the matrix:
[alpha-x beta]
[gamma delta-x]

Write down its determinant: (alpha-x)(delta-x)-(beta)(gamma). Expand this as a quadratic formula in x. Note that the coefficients of the quadratic equation come from the values in the matrix (for instance, "c" = (alpha)(delta)-(beta)(gamma), which as pointed out is the determinant). Use the quadratic formula on this quadratic equation.

10. Originally Posted by dcwang3
also, write out a 4x4 matrix using "alpha", "beta", "gamma", and "delta" as your four numbers?? the last time I could recall is that a 4x4 has 16 entries, thus 16 numbers, unless you are talking about identity matrix...
Sorry, I fixed that -- 2 x 2.

11. kk np np...ok well then the way I expressed determinants shoudln't be like that, because I made:

Code:
```cov[0][0]=cov[0][0]-x;
cov[1][1]=cov[1][1]-x;```
also, because of the formulas the other guy gave me, does the coefficient c play a role in it, because sqrt(D), I suppose he meant sqrt(det) in my case?

12. Originally Posted by dcwang3
kk np np...ok well then the way I expressed determinants shoudln't be like that, because I made:

Code:
```cov[0][0]=cov[0][0]-x;
cov[1][1]=cov[1][1]-x;```
also, because of the formulas the other guy gave me, does the coefficient c play a role in it, because sqrt(D), I suppose he meant sqrt(det) in my case?
Read again: you can't subtract x because you don't know what it is yet. x is/are your eigenvalue(s), which you are trying to find. You cannot store "5-x" as a polynomial into a float variable in C, nor do you want to actually subtract anything 'cause you don't know what you're subtracting. Find eigenvalues in your linear algebra textbook, or look them up, or just sit down and do the example I posted above. But eigenvalues are the solutions of (in this case) a quadratic equation, whose coefficients are based on the matrix you started with.

You have det listed in your code (as I mentioned above, this will be the "c" in your quadratic equation/formula). You need to find "a" and "b", and then evaluate the quadratic formula using those "a", "b" and "c" coefficients in your quadratic equation.

13. ok so here it is with using the
[alpha-x beta]
[gamma delta-x]

x^2+(-alpha-delta)x+[(alpha*delta)-(beta*gamma)]

so

a=1
b=(-alpha-delta)
c=[(alpha*delta)-(beta*gamma)]

---------------

for the equations, would I use:

r1 = (-b+sqrt(D))/(2*a)
r2 = (-b-sqrt(D))/(2*a)

where a,b are the coefficients, and D is the actual det

or should just use

r1= -b+sqrt[(b^2 -4ac)]/ (2a)
r1= -b-sqrt[(b^2 -4ac)]/ (2a)

???? Are the equations the same?

14. Originally Posted by dcwang3
ok so here it is with using the
[alpha-x beta]
[gamma delta-x]

x^2+(-alpha-delta)x+[(alpha*delta)-(beta*gamma)]

so

a=1
b=(-alpha-delta)
c=[(alpha*delta)-(beta*gamma)]

---------------
Everything's right, here.
for the equations, would I use:

r1 = (-b+sqrt(D))/(2*a)
r2 = (-b-sqrt(D))/(2*a)

where a,b are the coefficients, and D is the actual det

or should just use

r1= -b+sqrt[(b^2 -4ac)]/ (2a)
r1= -b-sqrt[(b^2 -4ac)]/ (2a)

???? Are the equations the same?
D just stands for "discriminant", which is b^2-4ac. You can use your "det" above, for c; and a has to be -1, so that helps to simplify. I would probably set a temporary variable for b too. (You can get away with using -b, since it's not going to make a difference in the b^2 part, so I would set something like negb = cov[0][0]+cov[1][1].)

And then r1 and r2 are your eigenvalues.

15. Code:
```/*Determinants*/

/*90*/
B2=(-cov[0][0]-cov[1][1])*(-cov[0][0]-cov[1][1]);
c= (4*cov[0][0]*cov[1][1])-(4*cov[0][1]*cov[1][0]);

sq= sqrt(B2-c);

B=(cov[0][0])+(cov[1][1]);

r1=(B+sq)(.5);
r2=(B-sq)(.5);

printf("\nThe eigenvalues are &#37;f and %f", r1, r2);```
here is my final work, but the thing is my complier says:

100: error: called object is not a function
101: error: called object is not a function

Any idea?