/* ESc101N Laboratory Assignment Solution Wednesday 18/8/04 PROBLEM: Write a program (with one class and that too having only the "main" method) to compute the solution of system of linear eqautions with 3 variables using Gaussian Elimination. You may assume that a unique solution exists */ import javabook.*; import java.awt.*; import java.lang.*; class gaussian { public static void main(String[] args) { int a11,a12,a13,a21,a22,a23,a31,a32,a33,c1,c2,c3; MainWindow mainwindow=new MainWindow(); InputBox inputbox=new InputBox(mainwindow); OutputBox outputbox=new OutputBox(mainwindow); // Take each coefficient as input one by one from the user // by invoking the getInteger() method of InputBox class. // Error message will be displayed if the value entered // cannot be converted to an int. a11=inputbox.getInteger("Please Enter a11"); a12=inputbox.getInteger("Please Enter a12"); a13=inputbox.getInteger("Please Enter a13"); c1=inputbox.getInteger("Please Enter c1"); a21=inputbox.getInteger("Please Enter a21"); a22=inputbox.getInteger("Please Enter a22"); a23=inputbox.getInteger("Please Enter a23"); c2=inputbox.getInteger("Please Enter c2"); a31=inputbox.getInteger("Please Enter a31"); a32=inputbox.getInteger("Please Enter a32"); a33=inputbox.getInteger("Please Enter a33"); c3=inputbox.getInteger("Please Enter c3"); /*The three equations in x,y and z are (a11)x + (a12)y + (a13)z = c1 ... (1) (a21)x + (a22)y + (a23)z = c2 ... (2) (a31)x + (a32)y + (a33)z = c3 ... (3) Reduce eqns (1)and (2)to linear equations in 2 variables using the following steps :- a) Multipy eqn(1) by (a21) (a21) * [(a11)x + (a12)y + (a13)z] = (a21) * c1 ... (4) b) Multipy eqn(2) by (a21) (a11) * [(a21)x + (a22)y + (a23)z] = (a11) * c2 ... (5) c) Subtract eqn(5) from eqn(4) [(a21)*(a12) - (a11)*(a22)]y + [(a21)*(a13)-(a11)*(a23)]z = (a21)*c1 - (a11)*c2 ... (6) Similarly reduce, eqns (2) and (3) to linear equations in 2 variables to get the following eqn. : [(a31)*(a22) - (a21)*(a32)]y + [(a31)*(a23)-(a21)*(a33)]z = (a31)*c2 - (a21)*c2 ... (7) Equation (6) can be written as: (l1)y + (m1)z = n1 .....(8) where l1=(a21)*(a12) - (a11)*(a22) m1=(a21)*(a13)-(a11)*(a23) n1=(a21)*c1 - (a11)*c2 Similiarly (7) can be written as: (l2)y + (m2)z = n2 .....(9) where l2=(a31)*(a22) - (a21)*(a32) m2=(a31)*(a23)-(a21)*(a33) n2=(a31)*c2 - (a21)*c3 The variables l1,l2,m1,m2,n1,n2 can be taken as float to avoid truncation while division. */ float l1,l2,m1,m2,n1,n2; l1=(a21)*(a12) - (a11)*(a22); l2=(a31)*(a22) - (a21)*(a32); m1=(a21)*(a13)-(a11)*(a23); m2=(a31)*(a23)-(a21)*(a33); n1=(a21)*c1 - (a11)*c2; n2=(a31)*c2 - (a21)*c3; /* z can be calculated by reducing equations (8) and (9) to linear equations in one variable z by following steps: a) Multiplying equation (8) by l2 and equation (9) by l1 l2[(l1)y + (m1)z] = n1*l2 .... (10) l1[(l2)y + (m2)z] = n2*l1 .... (11) b) Substracting (10) from (11) and solving for z: z[l2*m1-l1*m2] = n1*l2 - n2*l1 So, z= (n1*l2 - n2*l1)/(l2*m1-l1*m2) */ float x,y,z; z=(n1*l2-n2*l1)/(l2*m1-l1*m2); //by putting the value of z in equation (8) y can be calculated. y=(n1-m1*z)/l1; //by putting the values of y and z in equation (1) x can be calculated. x=(c1-a12*y-a13*z)/a11; //To show the output invoke setVisible() method of OutputBox with an argument true. outputbox.setVisible(true); //Print the equations entered by the user outputbox.printLine("You have entered the following equations:\n"); outputbox.printLine(a11+"x + "+a12+"y + "+a13+"z = "+c1); outputbox.printLine(a21+"x + "+a22+"y + "+a23+"z = "+c2); outputbox.printLine(a31+"x + "+a32+"y + "+a33+"z = "+c3); //print the solution i.e. values of x,y and z outputbox.printLine("\nThe solution of the equations is:\n"); outputbox.printLine("x = "+x); outputbox.printLine("y = "+y); outputbox.printLine("z = "+z); } }