Search code examples
javamathmatrixeigenvalueeigenvector

How to solve for eigenvectors of a 2x2 Matrix using Java


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 **


Solution

  • 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)