-2

I'm trying to solve for the eigenvectors of a 2x2 matrix. As of right now, I'm only considering real matrices whose eigenvectors and eigenvalues are also real. I'm currently having issues solving for the eigenvectors.

This is what I have so far:

public double[] getBasis(double[][] basis){

double a = basis[0][0];
double b = basis[0][1];
double c = basis[1][0];
double d = basis[1][1];

double eigenvalue1 = ((a+d) + Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
double eigenvalue2 = ((a+d) - Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
double tempx;
double tempy;

int counter = 1;
for (double y = -1000; y <= 1000; y++) {
    for (double x = -1000; x <= 1000; x++) {
        if (((a-eigenvalue1)*x + b*y == 0) && (c*x + (d-eigenvalue1)*y == 0)) {
            tempx = x;
            tempy = y;
            System.out.println("Eigenvector1: (" + x + "," + y + ")");
            System.out.println("Eigenvalue1: "+ eigenvalue1);

            }
         }
}   

for (double y = -10; y <= 10; y++) {
    for (double x = -10; x <= 10; x++) {
        if (((a-eigenvalue2)*x + b*y == 0) && (c*x + (d-eigenvalue2)*y == 0)) {
            tempx = x;
            tempy = y;                  
            System.out.println("Eigenvector2: (" + x + "," + y + ")");
            System.out.println("Eigenvalue2: " + eigenvalue2);
             }
            }
         }return eigenvector1;
}       

}

This method should have an input of a 2x2 array and i want it to output the two normalized eigenvectors. How would I be able to output both the eigenvectors?Additionally, I'm not allowed to use any packages that can solve for the eigenvectors or values. Basic math and arithmetic is perfectly acceptable.

Output:
Eigenvector1: (0.0,0.0)
Eigenvector1: (1.0,1.0)
Eigenvector1: (2.0,2.0)
Eigenvector1: (3.0,3.0)
Eigenvector1: (4.0,4.0)
Eigenvector1: (5.0,5.0)
Eigenvector1: (6.0,6.0)
Eigenvector1: (7.0,7.0)
Eigenvector1: (8.0,8.0)
Eigenvector1: (9.0,9.0)
Eigenvector1: (10.0,10.0)
Eigenvector2: (0.0,0.0)

How would I be able to choose only one vector for eigenvector 1 and eigenvector 2. Additionally, If the matrix had input (1,1,1,-1) such that it was a 2x2 matrix then the eigenvalues should be the square root of positive and negative 2. I'm able to get that far. However, once I attempt to calculate the eigenvectors I don't get a value for an eigenvector.

**Edited: I added the errors and took out the infinite loop I got stuck on before **

2 Answers2

1

There is no need to solve the linear system for the eigenvectors with a trial-and-error loop.

The equation

(a-e)*x+b*y == 0

has always the solution

x = b,  y = -(a-e)

which you then would have to normalize. You will need to check if the first equation has the coefficients all zero, then you have to use the second equation

c*x + (d-e)*y == 0

with solution

x = -(d-e), y = c

If also the second equation has all zero coefficients, then any vector is an eigenvector, as the matrix is the diagonal matrix diag([e, e]).


This should result in some code like

e = eigenvalue1;
x = b; y = e-a;
r = Math.sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
    x = e-d; y = c;
    r = sqrt(x*x+y*y)
    if( r > 0) { x /= r; y /= r; }
    else {
        x = 1; y = 0;
    }
}
System.out.println("Eigenvector1: (" + x + "," + y + ")");

e = eigenvalue2;
x = b; y = e-a;
r = Math.sqrt(x*x+y*y)
if( r > 0) { x /= r; y /= r; }
else {
    x = e-d; y = c;
    r = sqrt(x*x+y*y)
    if( r > 0) { x /= r; y /= r; }
    else {
        x = 0; y = 1;
    }
}
System.out.println("Eigenvector2: (" + x + "," + y + ")");
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51
0

According to your edit comments, I think this revised code should produce what you want. I've removed your temp variables and I'm returning your computed basis values as a 2 element array. Excuse me if I've misnamed anything, my maths is a little rusty.

public static void main(String[] args) {
    // the input matrix
    double[][] matrix = {
        {1.0, 1.0},
        {1.0, -1.0}
    };

    // compute the basis
    double[] basis = getBasis(matrix);

    System.out.println("Basis: (" + basis[0] + ", " + basis[1] + ")");
}

public double[] getBasis(double[][] matrix){

    double a = matrix[0][0];
    double b = matrix[0][1];
    double c = matrix[1][0];
    double d = matrix[1][1];

    double eigenvalue1 = ((a+d) + Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;
    double eigenvalue2 = ((a+d) - Math.sqrt( Math.pow(a-d,2) + 4*b*c))/2;

    // store the basis in a 2 element array
    double[] basis = new double[2];

    for (double y = -1000; y <= 1000; y++) {
        for (double x = -1000; x <= 1000; x++) {
            if (((a-eigenvalue1)*x + b*y == 0) && (c*x + (d-eigenvalue1)*y == 0)) {
                System.out.println("Eigenvector1: (" + x + "," + y + ")");
                basis[0] = eigenvalue1;
            }
        }
    }   

    for (double y = -10; y <= 10; y++) {
        for (double x = -10; x <= 10; x++) {
            if (((a-eigenvalue2)*x + b*y == 0) && (c*x + (d-eigenvalue2)*y == 0)) {
                System.out.println("Eigenvector2: (" + x + "," + y + ")");
                basis[1] = eigenvalue2;
            }
        }
    }

    return basis;
}

Output:

Basis: (1.4142135623730951, -1.4142135623730951)
Matt
  • 3,677
  • 1
  • 14
  • 24
  • This computes the eigenvalues, which is great! However, I was actually looking for a way to print out the eigenvectors of this matrix. – Diego De La Vega May 02 '17 at 04:13
  • You have an if() statement inside your nested for loops where you print out the eigenvectors. I took them out, but I've edited my post and added them back in. – Matt May 02 '17 at 04:34