views:

1384

answers:

6

Hi guys,

according to below 3 Equations

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

how to get the value of x,y,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 ?

}

=================================================

or do you hava other solutions or other common frameworks ? thanks .

+3  A: 

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%5FDeterminants.php

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

Xinus
The url that you recommended is very good.thanks again.
Cong De Peng
This is the mathematically valid, but not very good in terms of efficiency.see Transcript - Lecture 20 from video lectures of Professor Gilbert Strang teaching 18.06 "If you had to -- and Matlab would never, never do it.I mean, it would use elimination."http://ocw.mit.edu/OcwWeb/Mathematics/18-06Spring-2005/VideoLectures/detail/lecture20.htm
Liran Orevi
Computing a determinant is (I think) O(n!). Gaussian elimination is O(n^2).
erikkallen
NO NO NO ***NEVER*** use determinants. What the previous two commenters said.
Jason S
A: 

Another source, with sample code in various languages is given here

DaveJohnston
+1  A: 

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

erikkallen
waoh,the knowledge is so professional . I need some time to digest.
Cong De Peng
I'm not sure I get the connection between parsing and linear solving.
Liran Orevi
Not at all - this is a linear algebra problem. ANTLR does not apply.
duffymo
@duffymo: Added that last step to the explanation.
erikkallen
the parsing would be because the original post specified the equations as strings.
Martin DeMello
I've changed my mind and up-voted the answer. I wouldn't recommend inputting the equations as strings, but if that's what the OP really wants then erikkallen's answer is correct. Thanks for the clarification.
duffymo
+2  A: 

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

Code example

Liran Orevi
+2  A: 

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.

duffymo
What's the reason to prefer LU decomposition?
Liran Orevi
"LU decomposition is computationally efficient only when we have to solve a matrix equation multiple times for different b; it is faster in this case to do an LU decomposition of the matrix A once and then solve the triangular matrices for the different b, than to use Gaussian elimination each time." - common for finite element analysis with multiple load vectors. You're correct - it might not matter in this case.
duffymo
@duffymo, thanks for the explanation.
Liran Orevi
A: 

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

Valentin Rocher