Comment résoudre des équations avec java?


J'ai trois équations comme les suivantes:

  • x + y + z = 100;
  • x + y-z = 50;
  • x-y-z = 10;

Comment puis-je trouver les valeurs de x, y et z avec Java?

String equation1="x+y+z=100;";
String equation2="x+y-z=50;";
String equation3="x-y-z=10;";

int[] SolveEquations(equation1,equation2,equation3) {
   // to do
   // how to do?    
} 

Avez-vous des solutions possibles ou d'autres cadres communs?

Author: sth, 2009-09-16

8 answers

Vous pouvez utiliser le déterminant pour calculer les valeurs de x y et z. La logique peut être trouvée ici http://www.intmath.com/Matrices-determinants/1_Determinants.php

Et ensuite vous devez l'implémenter en java en utilisant des tableaux 3 dimensions.

 12
Author: Xinus, 2009-09-16 09:26:51

Puisque vous écrivez Java, vous pouvez utiliser le packageJAMA pour résoudre ce problème. Je recommanderais une bonne méthode de décomposition LU.

C'est un simple problème d'algèbre linéaire. Vous devriez être capable de le résoudre à la main ou en utilisant quelque chose comme Excel assez facilement. Une fois que vous avez cela, vous pouvez utiliser la solution pour tester votre programme.

Il n'y a aucune garantie, bien sûr, qu'il existe une solution. Si votre matrice est singulière, cela signifie qu'il n'y a pas d'intersection de ces trois lignes en 3D espace.

 11
Author: duffymo, 2016-06-04 10:13:00

Vous pouvez utiliser le paquet java matrix JAMA. Voir la page complète de cet exemple ci-dessous ici

/*
 *Solving three variable linear equation system
 * 3x + 2y -  z =  1 ---> Eqn(1)
 * 2x - 2y + 4z = -2 ---> Eqn(2)
 * -x + y/2-  z =  0 ---> Eqn(3)
 */
import Jama.Matrix;
import java.lang.Math.*;
public class Main {
    public Main() {
        //Creating  Arrays Representing Equations
        double[][] lhsArray = {{3, 2, -1}, {2, -2, 4}, {-1, 0.5, -1}};
        double[] rhsArray = {1, -2, 0};
        //Creating Matrix Objects with arrays
        Matrix lhs = new Matrix(lhsArray);
        Matrix rhs = new Matrix(rhsArray, 3);
        //Calculate Solved Matrix
        Matrix ans = lhs.solve(rhs);
        //Printing Answers
        System.out.println("x = " + Math.round(ans.get(0, 0)));
        System.out.println("y = " + Math.round(ans.get(1, 0)));
        System.out.println("z = " + Math.round(ans.get(2, 0)));
    }

    public static void main(String[] args) {
        new Main();
    }
}

 9
Author: sandi vorace, 2016-02-12 12:58:52

Vous pouvez aussi utiliser Communes Math. Ils ont une section de cela dans leurs userguide (voir 3.4)

 5
Author: Valentin Rocher, 2009-09-16 10:14:25

Créer un analyseur en utilisantANTLR . Ensuite, évaluez le AST en utilisant Élimination gaussienne .

 4
Author: erikkallen, 2009-09-16 10:19:20

UtilisezGaussian_elimination c'est incroyablement facile, mais il y acertaines valeurs vous pouvez avoir une vie difficile à calculer.

Exemple de Code

 2
Author: Liran Orevi, 2009-09-16 09:41:20

Il existe de nombreuses façons de résoudre les équations du système linéaire. Il existe un moyen le plus simple d'effectuer cela. Dans l'exemple, le code java résout deux variables EN UTILISANT la méthode Matrix mais vous pouvez modifier pour effectuer des calculs de 3 variables.

import java.util.Scanner; //OBJETO SCANNER

public class SYS2 {

    public static void main (String args[]) {

        //VARIABLE DECLARATION SPACE
        int i=0,j = 0;
        float x,y;
         Scanner S = new Scanner (System.in);

         int EC3[][]= new int [2][3]; //ARRAY TO STORE EQUATION 2X3

         float DET1=0;
        float DET2 =0;


        float DETA=0;
         float DETB=0;

        //END VARIABLE DECLARATIONS
       System.out.println("Enter Equation System : ");

for (i=0; i< 2; i++) {
                for (j=0; j< 3; j++) 
                        EC3[i][j] = S.nextInt();
                }    

                System.out.println("SISTEMA DE ECUACION LINEAL: "); //THIS SENTENCE ONLY PRINT THE CATCHED VALUES OF EQUATION

                for (i=0; i< 2; i++) {
                        for (j=0; j< 3; j++) 

                              System.out.print(EC3[i][j] + "  ");

                            System.out.println();


                }    




           //    System.out.print("Determinante A de la Matriz: ");

             //    System.out.print((EC3[0][2] * EC3[1][1]) - (EC3[0][1]*EC3[1][2]) );

                for (i=0;i<2;i++) {
                        for (j=0; j<2;j++)
                                DET1=  ((EC3[0][2] * EC3[1][1]) -( EC3[0][1]*EC3[1][2]));
                }

                           //    System.out.print(DET1 );
                            //       System.out.println();

                                 for (i=0;i<2;i++) {
                                        for (j=0; j<2;j++)
                                         DET2=  ((EC3[0][0] * EC3[1][1]) - (EC3[0][1]*EC3[1][0]));
                }

                               // System.out.print("Determinante B de la Matriz: ");
                               //  System.out.println(DET2 ); 

    x = (DET1 / DET2);

    System.out.println();
    System.out.println("X = " + x);
    System.out.print("=======================");
    //FIN PARA VALOR DE X



    //COMIENZO DE VALOR DE Y

  //  System.out.print("Determinante A de la Matriz Y: ");

                                    for (i=0;i<2;i++) {
                                            for (j=0; j<2;j++)
                                         DETA=  EC3[0][0] * EC3[1][2] - EC3[0][2]*EC3[1][0];


                                        //    System.out.print(DETA );
                                          //  System.out.println();
                }


                                     for (i=0;i<2;i++) {
                                            for (j=0; j<2;j++)
                                         DETB=  EC3[0][0] * EC3[1][1] - EC3[0][1]*EC3[1][0];

                }

                                   // System.out.print("Determinante B de la Matriz Y: ");
                                   //  System.out.println(DETB );                  
                                    y = DETA / DETB;

                                    System.out.print("=======================");
                                    System.out.println();

                                    System.out.println("Y = " + y);
                                    System.out.print("=======================");
    }
}




 1
Author: Keating Lopez, 2020-05-05 18:30:40

Essayez ceci, s'il vous plait:

import org.apache.commons.math3.linear.*;
import org.junit.Assert;
import org.junit.Test;

/**
 * Author: Andrea Ciccotta
 */
public class LinearSystemTest extends Assert {

    /**
     * Ax = B
     * 2x + 3y - 2z = 1
     * -x + 7y + 6x = -2
     * 4x - 3y - 5z = 1
     * <p>
     * it will use the LUDecomposition:
     * LU decomposition:
     * 1. find A = LU where LUx = B
     * 2. solve Ly = B
     * 4. solve Ux = y
     */
    @Test
    public void linearSystem3x3Test() {
        final RealMatrix coefficients = new Array2DRowRealMatrix(new double[][]{{2, 3, -2}, {-1, 7, 6}, {4, -3, -5}});
        final DecompositionSolver solver = new LUDecomposition(coefficients).getSolver();

        final RealVector constants = new ArrayRealVector(new double[]{1, -2, 1}, false);
        final RealVector solution = solver.solve(constants);
        final double[] arraySolution = solution.toArray();

        assertEquals(arraySolution[0], -0.36986301369863006, 0);
        assertEquals(arraySolution[1], 0.1780821917808219, 0);
        assertEquals(arraySolution[2], -0.6027397260273972, 0);
    }

}
 0
Author: Andrea Ciccotta, 2020-11-13 14:17:28