首页 > 解决方案 > Java数学方程求解器[非正规方程]

问题描述

我需要一个java中的方法,它返回一个方程的解,这个没有代码的方程是这样的:

  1. 得到一个数字(Z)
  2. 和一个以弧度为单位的角度(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;
}

标签: javaalgorithmmathequation

解决方案


实际上 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 有无数种解。


推荐阅读