如何用java解方程?
我有三个方程,如下所示:
- 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?
}
您有任何可能的解决方案或其他通用框架吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用行列式来计算 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.
由于您正在编写 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.
您可以使用java矩阵包JAMA。请在此处查看此示例的完整页面
<代码> <代码>
>
you can use the java matrix package JAMA. See the full page of this example below here
您还可以使用Commons Math。他们的 用户指南 中有这一部分(参见 3.4)
You can also use Commons Math. They have a section of this in their userguide (see 3.4)
使用 ANTLR 创建解析器。然后使用 AST 评估 高斯消除。
Create a parser using ANTLR. Then evaluate the AST using Gaussian elimination.
使用 Gaussian_elimination 非常简单,但是有 一些值您可能很难计算。
代码示例
Use Gaussian_elimination it's incredibly easy, but there are some values you may have hard life calculating.
Code example
请尝试这个:
try this, please:
求解线性系统方程的方法有很多种。有一种最简单的方法可以执行此操作。在示例中,java 代码使用 Matrix 方法求解两个变量,但您可以修改以执行 3 个变量计算。
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.