/* Write a program (with one class and that too having only the "main" method) to find the eigen values and eigen vectors of a 2X2 matrix. You may assume that the input matrix is non-singular and has integer elements. */ import javabook.*; import java.math.*; public class MyClass { static int p,q,r,s; static int a,b,c; public static void main(String[] args) { MainWindow mainWindow = new MainWindow(); InputBox inputBox = new InputBox(mainWindow); OutputBox ob = new OutputBox(mainWindow); // Input the Matrix in row-major form p=inputBox.getInteger("Enter an integer : a11"); q=inputBox.getInteger("Enter an integer : a12"); r=inputBox.getInteger("Enter an integer : a21"); s=inputBox.getInteger("Enter an integer : a21"); // Display the matrix taken as input from user ob.print("Input Matrix [2 X 2] is : \n"); ob.print(p);ob.print("\t");ob.printLine(q); ob.print(r);ob.print("\t");ob.printLine(s); /* The eigen value equation is M.V = e.V, where V=(V1,V2) is the eigen vector and e is the eigen value of 2X2 matrix M. A 2x2 matrix will have up to two eigen values and two eigen vectors. To solve theequation we transform it to (M-eI).V = 0 where I stands for identity matrix. Expanding it we get two equations: M11.V1 - e.V1 + M12.V2 = 0 (1) and M21.V1 + M22.V2 - e.V2 = 0 Simplifying these equations leads to the equation e^2 - e.(M11 + M22) + (M11.M22 - M12.M21) = 0. Substituting the values of M11,M12,M21,M22, we get e^2 - e.(p+s) + (p*s - q*r) = 0 Since this is a quadratic eqn. of the form : a(x^2) + bx + c =0, we get 2 values of eigen values, lets store them in y1, y2 */ a=1; b=-(p+s); c=(p*s)-(q*r); double midterm= Math.sqrt( (b*b)-(4*a*c) ); double y1= (midterm-b)/(2*a); double y2= -(midterm+b)/(2*a); // Display the eigen values ob.print("\n First Eigen Value = "); ob.printLine(y1); ob.print("\n Second Eigen Value = "); ob.printLine(y2); // Substitute the values of eigen values in eqn. 1 to get eigen vectors double j2=1.0/( 1+ (((s-y1-q)/(p-y1-r))*((s-y1-q)/(p-y1-r)) )); j2=Math.sqrt(j2); double i2=j2*(s-y1-q)/(p-y1-r); ob.print("\n For first eigen value Unit Eigen vector first component is : "); ob.printLine(i2); ob.print("\n For first eigen value Unit Eigen vector second component is : "); ob.printLine(j2); j2=1.0/( 1+(((s-y2-q)/(p-y2-r))*((s-y2-q)/(p-y2-r)) )); j2=Math.sqrt(j2); i2=j2*(s-y2-q)/(p-y2-r); ob.print("\n For second eigen value Unit Eigen vector first component is : "); ob.printLine(i2); ob.print("\n For second eigen value Unit Eigen vector second component is : "); ob.printLine(j2); ob.waitUntilClose(); } }