Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Solve Equations with java?

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?

like image 563
Cong De Peng Avatar asked Sep 16 '09 09:09

Cong De Peng


People also ask

How to solve system of linear equations in Java?

This is a java program to solve the system of linear equations. This can be done by first representing equations (vectors) to matrix form, then finding the inverse of the matrix formed by the coefficients of variable and multiplying it with constants. How To Import And Run The Project?

How can I solve for multiple variables in Java?

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. Thanks for contributing an answer to Stack Overflow!

How do you do math in Java?

This line will start doing math using the a, b, and c variables that were defined at the beginning of the program. Math.sqrt () is a Java command that takes the square root of everything within the parenthesis.

How to solve math expressions in JavaScript?

Since we are using the JavaScript engine, we can directly add variables to the expressions as we do in JavaScript. Note – JavaScript doesn't have direct methods to perform math operations and requires access to the Math object. Thus, we cannot solve math expressions using the Java Scripting API.


2 Answers

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

}
like image 107
Andrea Ciccotta Avatar answered Sep 29 '22 13:09

Andrea Ciccotta


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

like image 33
Valentin Rocher Avatar answered Sep 29 '22 12:09

Valentin Rocher