java - Java数学方程求解器[非正规方程]
问题描述
我需要一个java中的方法,它返回一个方程的解,这个没有代码的方程是这样的:
- 得到一个数字(Z)
- 和一个以弧度为单位的角度(C)
然后找到 X 的值,它是这个方程的解:
a = Integer( z*cos(c) ) // temp must be integer
//now we have the value of a
// we put it in b
b = a
//now we look for the value of x that solves this equation
b =? Integer( X/cos(C) ) // X also must be integer
X = ?? // we must get X the solves the equation above
示例:考虑
Z = 15
C = 140 // full angles will be casted ..it will be rooted to ~-0.0629*PI
temp = Integer( 15*cos(140) // -2.96 )
temp <-- -2 //after parsing to integer
-2 = Integer ( X )/cos(140)
what is X ?
我试图在java中实现这个方法,但大多数时候它卡在寻找结果这个代码没有找到直接的解决方案,就像我希望它测试数字直到它得到它但在很多时候它找不到结果并且一直循环到无穷大。找到结果的速度也很慢,我在程序中调用该函数超过 500,000 次
int Rounding(int z, int c){
int offset = 20 ;
int x;
int test = (int) ( z*Math.cos(c) - offset );
int solution;
while(true){
solution = (int) ( test/Math.cos(c) );
if(solution == z){
x = solution;
break;
}else{
test++;
}
/*
if(solution > z){
offset ++;
solution = (int) ( z*Math.cos(c) - offset );
}
*/
}
return x;
}
/*Note : the function will return x only when it solves this : */
int returned_Z = (int) ( x/Math.cos(c) )
// returned_Z must be equal to z
之后,该变量 x 将存储在一个文件中……然后当文件打开时,该变量 x 将使用此函数返回给 z:
int returning(int x, int c){
int z = (int) ( x/Math.cos(c) );
return z;
}
解决方案
实际上 eqn 有无数个解。说temp = 2
。你写:
2 = Integer ( ( X )/cos(140) )
如果我们取Integer()
范围内的所有实数2.0 <= num < 3.0
,则结果为2
。这就是为什么可能有无限数量的解决方案。例如,如果我们2.5
从范围中获取:
2 = Integer (2.5) is true
so we can write,
x / cos(140) = 2.5
=> x = 2.5 * cos(140)
= −1.915111108
如果我们2.3
从范围中取另一个:
x = −1.761902219
由于范围内有无限个实数,2.0 <= num < 3.0
因此解的数量也是无限的。
所以你不能只期望 x 有一个单一的解决方案。如果你这样做,那么使用:
int Rounding(int z, int c){
int test = ( z*Math.cos(c) );
int x = (int) ( test*Math.cos(c) );
return x;
}
这会给你一个正确的答案。但正如我之前所说,x 有无数种解。
推荐阅读
- visual-studio - 由于缺少可执行文件而导致 GDB 失败
- typescript - 如何使用 fp-ts Reader monad 制作单级管道/流?
- automation - 制定一个 Bamboo 计划会导致其他计划延迟安排
- node.js - 使用 Socket.io 从客户端向服务器发送消息数据是否可靠
- javascript - 动态组件内的自定义组件未初始化
- mysql - 选择关联表的所有行的数量并加入这些行。MySQL
- python - 检查 3D 点是否在基于正方形的金字塔中
- r - 如何在 r 中建立状态空间模型:将时变参数与 GARCH-M 模型相结合
- xslt - 有没有办法在 XSL 1.0 模板中使用 for 循环在以下场景中打印金额?
- blazor - 在自定义组件中包装 MudBlazor 组件 - @bind-Value 的问题