How to Solve Equations with java?

前端 未结 8 674
花落未央
花落未央 2020-12-03 05:45

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

相关标签:
8条回答
  • 2020-12-03 06:26

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

    0 讨论(0)
  • 2020-12-03 06:27

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

    0 讨论(0)
提交回复
热议问题