lundi 27 novembre 2017

Solving Linear and Non-Linear Systems of Equations with No Solution (Java)

Both of the following pieces of code have something wrong with the math. One solves a linear system of equations in standard form and the other solves systems of quadratic equations. There seems to be something wrong with the last sections where it checks if the answer is no solution or not. Please help

Solving Systems of Linear Equations in Standard Form Program:

import javax.swing.JOptionPane;
public class StandardForm
{
     public static void StandardForm( String[] Args[] ) {   
       String inserta1;
       String insertb1;
       String insertc1;
       String inserta2;
       String insertb2;
       String insertc2;
       double a1;
       double b1;
       double c1;
       double a2;
       double b2;
       double c2;

       System.out.println("Here you can learn how to solve linear systems of equations in Standard Form:");
       System.out.println("ax+by=c");
       System.out.println("Here are some practice problems.");
       System.out.println("Once you have completed them, use the program to check you answer.");
       System.out.println("Once you are totally finished, type stop for any of the values to exit the program.");
       System.out.println("1. 2x+y=1 and x-2y=8");
       System.out.println("2. 3x+2y=4 and x+2y=-4");
       System.out.println("3. 4x+y=1 and x+y=-2");
       System.out.println("4. x+3y=-9 and 5x+3y=3");
       System.out.println("5. x+2y=4 and 3x+2y=8");
       System.out.println("6. 5x-3y=-12 and 2x+3y=-9");
       System.out.println("7. x-y=-1 and 2x+y=4");
       System.out.println("8. 2x-y=-2 and 2x-y=3");
       System.out.println("9. x+y=-6 and 4x+y=3");
       System.out.println("10. x-7y=-21 and 13x-7y=63");
       while ( true ) {
       inserta1 =
        JOptionPane.showInputDialog( "Enter the a value of the first equation." );
       if( inserta1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb1 =
        JOptionPane.showInputDialog( "Enter the b value of the first equation." );
       if( insertb1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc1 =
        JOptionPane.showInputDialog( "Enter the c value of the first equation." );
       if( insertc1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       inserta2 =
        JOptionPane.showInputDialog( "Enter the a value of the second equation." );
       if( inserta2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb2 =
        JOptionPane.showInputDialog( "Enter the b value of the second equation." );
       if( insertb2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc2 =
        JOptionPane.showInputDialog( "Enter the c value of the second equation." );
       if( insertc2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       a1 = Double.parseDouble( inserta1 );
       b1 = Double.parseDouble( insertb1 );
       c1 = Double.parseDouble( insertc1 );
       a2 = Double.parseDouble( inserta2 );
       b2 = Double.parseDouble( insertb2 );
       c2 = Double.parseDouble( insertc2 );

       double x = (b2 * a2 - b1 * c2)/(a1 * a2 - b1 * c1);
       double y = (a1 * c2 - b2 * c1)/(a1 * a2 - b1 * c1);
       double inf = Double.POSITIVE_INFINITY;
       if((a1*x)+(b1*y) != c1 || (a2*x)+(b2*y) != c2) {
           JOptionPane.showMessageDialog(
         null, "This system has no solution.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else if(x == inf && y == inf) {
            JOptionPane.showMessageDialog(
         null, "This system has infinately many solutions.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else {
           JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x + ", " + y + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
    }   
}
}

Solving Systems of Quadratic Equations Program:

import javax.swing.JOptionPane;
public class Quadratic
{
       public static void StandardForm( String[] Args[] ) {   
       String inserta1;
       String insertb1;
       String insertc1;
       String inserta2;
       String insertb2;
       String insertc2;
       double a1;
       double b1;
       double c1;
       double a2;
       double b2;
       double c2;

       System.out.println("Here you can learn how to solve systems of quadratic equations in standard form:");
       System.out.println("y=ax^2+bx+c");
       System.out.println("Here are some practice problems.");
       System.out.println("Once you have completed them, use the program to check you answer.");
       System.out.println("Once you are totally finished, type stop for any of the values to exit the program.");
       System.out.println("1. Problem");
       System.out.println("2. Problem");
       System.out.println("3. Problem");
       System.out.println("4. Problem");
       System.out.println("5. Problem");
       System.out.println("6. Problem");
       System.out.println("7. Problem");
       System.out.println("8. Problem");
       System.out.println("9. Problem");
       System.out.println("10. Problem");
       while ( true ) {
       inserta1 =
        JOptionPane.showInputDialog( "Enter the a value of the first equation." );
       if( inserta1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb1 =
        JOptionPane.showInputDialog( "Enter the b value of the first equation." );
       if( insertb1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc1 =
        JOptionPane.showInputDialog( "Enter the c value of the first equation." );
       if( insertc1.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       inserta2 =
        JOptionPane.showInputDialog( "Enter the a value of the second equation." );
       if( inserta2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertb2 =
        JOptionPane.showInputDialog( "Enter the b value of the second equation." );
       if( insertb2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       insertc2 =
        JOptionPane.showInputDialog( "Enter the c value of the second equation." );
       if( insertc2.equals("stop")) {
           System.out.println("");
           System.out.println("Thanks for using the program!");
           System.exit(0);
        }
       a1 = Double.parseDouble( inserta1 );
       b1 = Double.parseDouble( insertb1 );
       c1 = Double.parseDouble( insertc1 );
       a2 = Double.parseDouble( inserta2 );
       b2 = Double.parseDouble( insertb2 );
       c2 = Double.parseDouble( insertc2 );

       double finala = a2 - a1;
       double finalb = b2 - b1;
       double finalc = c2 - c1;
       double x1 =((Math.sqrt(Math.pow(finalb,2)-(4*finala*finalc))-finalb)/2);
       double x2 =(-finalb + Math.sqrt(Math.pow(finalb,2)-(4*finala*finalc)))/2;
       double y1 = (-a1*x1+c1)/b1;
       double y2 = (-a1*x2+c1)/b1;
       double inf = Double.POSITIVE_INFINITY;
       if((a2*x1)+(b2*y1) != c2 && (a2*x2)+(b2*y2) != c2) {
           JOptionPane.showMessageDialog(
         null, "This system has no solution.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else if((a2*x1)+(b2*y1) == c2 && (a2*x2)+(b2*y2) != c2) {
             JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x1 + ", " + y1 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );           
        }
       else if((a2*x1)+(b2*y1) != c2 && (a2*x2)+(b2*y2) == c2) {
             JOptionPane.showMessageDialog(
         null, "Your answer is ( " + x2 + ", " + y2 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );           
        }
       else if(x1 == inf && y1 == inf || x2 == inf && y2 == inf) {
            JOptionPane.showMessageDialog(
         null, "This system has infinately many solutions.", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
       else {
           JOptionPane.showMessageDialog(
         null, "Your answers are ( " + x1 + ", " + y1 + " ) and ( " + x2 + ", " + y2 + " ).", "Answer(s)", JOptionPane.PLAIN_MESSAGE
        );
        }
    }   
}
}

Aucun commentaire:

Enregistrer un commentaire