如何用java解方程?

发布于 2024-08-04 19:52:09 字数 380 浏览 2 评论 0 原文

我有三个方程,如下所示:

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

如何使用 Java 找到 x、y 和 z 的值?

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?    
} 

您有任何可能的解决方案或其他通用框架吗?

I have three equations like the following ones:

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

How can I find the values of x, y, and z with 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?    
} 

Do you have any possible solutions or other common frameworks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

╰◇生如夏花灿烂 2024-08-11 19:52:09

您可以使用行列式来计算 xy 和 z 的值。
可以在此处找到逻辑 http://www.intmath.com/Matrices-decidants/1_Determinants .php

然后你需要在java中使用3维数组来实现它。

You can use determinant to calculate values of x y and z.
Logic can be found out here http://www.intmath.com/Matrices-determinants/1_Determinants.php

And then you need to implement it in java using 3 dimensional arrays.

怂人 2024-08-11 19:52:09

由于您正在编写 Java,因此可以使用 JAMA 包来解决此问题。我推荐一个好的 LU 分解方法。

这是一个简单的线性代数问题。您应该能够轻松地手动或使用 Excel 之类的工具来解决它。一旦获得,您就可以使用该解决方案来测试您的程序。

当然,不能保证一定有解决方案。如果您的矩阵是奇异矩阵,则意味着 3D 空间中这三条线不存在交集。

Since you're writing Java, you can use the JAMA package to solve this. I'd recommend a good LU decomposition method.

It's a simple linear algebra problem. You should be able to solve it by hand or using something like Excel pretty easily. Once you have that you can use the solution to test your program.

There's no guarantee, of course, that there is a solution. If your matrix is singular, that means there is no intersection of those three lines in 3D space.

极度宠爱 2024-08-11 19:52:09

您可以使用java矩阵包JAMA。请在此处查看此示例的完整页面
<代码> <代码>

/*
 *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();
    }
}

>

you can use the java matrix package JAMA. See the full page of this example below here

/*
 *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();
    }
}

无戏配角 2024-08-11 19:52:09

您还可以使用Commons Math。他们的 用户指南 中有这一部分(参见 3.4)

You can also use Commons Math. They have a section of this in their userguide (see 3.4)

错々过的事 2024-08-11 19:52:09

使用 ANTLR 创建解析器。然后使用 AST 评估 高斯消除

Create a parser using ANTLR. Then evaluate the AST using Gaussian elimination.

单身情人 2024-08-11 19:52:09

使用 Gaussian_elimination 非常简单,但是有 一些值您可能很难计算。

代码示例

Use Gaussian_elimination it's incredibly easy, but there are some values you may have hard life calculating.

Code example

睫毛溺水了 2024-08-11 19:52:09

请尝试这个:

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);
    }

}

try this, please:

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);
    }

}
つ低調成傷 2024-08-11 19:52:09

求解线性系统方程的方法有很多种。有一种最简单的方法可以执行此操作。在示例中,java 代码使用 Matrix 方法求解两个变量,但您可以修改以执行 3 个变量计算。

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("=======================");
    }
}




There are many ways to solve linear system equations. There is a simplest way to perform this. In the example the java code solve for two variables USING Matrix method but you can modify to perform 3 variables calculations.

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("=======================");
    }
}




~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文