Java数学方程式求解器[不是普通方程式]

时间:2018-11-20 16:44:10

标签: java algorithm math equation

我需要Java中的一种方法来返回方程式的求解,而该方程式无需代码即可:

  1. 获取数字(Z)
  2. 弧度
  3. 中的角度(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;
}

2 个答案:

答案 0 :(得分:0)

实际上,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的解数是无限的。

答案 1 :(得分:0)

根据您的发帖,我们有

temp = Integer( 15*cos(140) // -2.96 )
Find X such that
temp = Integer ( X/cos(140) )

我们可以解决X的问题,而无需进行整数转换。

X = 15 / cos^2(140)

或者一般而言

X = Z / cos^2(C)

这将为您提供X的确切解决方案;您可以将整数中间要求用于其他目的。


每个OP评论的更新

您在XtempZ之间具有定义的数学关系。截断中间结果会破坏这种关系。简而言之,如果将X限制为整数,则无法保证在应用逆运算时得到的Z完全相同。

特别是,您具有先验功能cos;您不能说这将是整数tempXXZ的比率。对于cos,确实存在点解,它们是有理数,但很少。

如果我误解了这个问题-我意识到我们有翻译问题-那么请更新您的问题以指定正确的问题。