1

我正在尝试将一些 Octave 代码移植到 Java。我正在尝试 atan2 所以我有以下尝试

octave3.2:53> m1 = [1,2;3,4]
m1 =

   1   2
   3   4

octave3.2:54> m2=[5,6;7,8]
m2 =

   5   6
   7   8

octave3.2:55> atan2(m1,m2)
ans =

   0.19740   0.32175
   0.40489   0.46365

octave3.2:56> atan(1/5)
ans =  0.19740
octave3.2:57> atan(2/6)
ans =  0.32175
octave3.2:58> atan(3/7)
ans =  0.40489
octave3.2:59> atan(4/8)
ans =  0.46365

所以在我看来, atan2 将 atan 单元格应用于第一个参数除以第二个参数的商。我在 Java 中做得很好。但是,下面的代码给出了一些奇怪的东西......

y
x
theta = atan2(y,x) 

我得到了类似的痕迹,

y =

  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000  -0.50000
  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000  -0.40000
  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000  -0.30000
  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000  -0.20000
  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000  -0.10000
   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000   0.00000
   0.10000   0.10000   0.10000   0.10000   0.10000   0.10000   0.10000   0.10000   0.10000   0.10000
   0.20000   0.20000   0.20000   0.20000   0.20000   0.20000   0.20000   0.20000   0.20000   0.20000
   0.30000   0.30000   0.30000   0.30000   0.30000   0.30000   0.30000   0.30000   0.30000   0.30000
   0.40000   0.40000   0.40000   0.40000   0.40000   0.40000   0.40000   0.40000   0.40000   0.40000

x =

  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000
  -0.50000  -0.40000  -0.30000  -0.20000  -0.10000   0.00000   0.10000   0.20000   0.30000   0.40000

theta =

  -2.35619  -2.24554  -2.11122  -1.95130  -1.76819  -1.57080  -1.37340  -1.19029  -1.03038  -0.89606
  -2.46685  -2.35619  -2.21430  -2.03444  -1.81577  -1.57080  -1.32582  -1.10715  -0.92730  -0.78540
  -2.60117  -2.49809  -2.35619  -2.15880  -1.89255  -1.57080  -1.24905  -0.98279  -0.78540  -0.64350
  -2.76109  -2.67795  -2.55359  -2.35619  -2.03444  -1.57080  -1.10715  -0.78540  -0.58800  -0.46365
  -2.94420  -2.89661  -2.81984  -2.67795  -2.35619  -1.57080  -0.78540  -0.46365  -0.32175  -0.24498
   3.14159   3.14159   3.14159   3.14159   3.14159   0.00000   0.00000   0.00000   0.00000   0.00000
   2.94420   2.89661   2.81984   2.67795   2.35619   1.57080   0.78540   0.46365   0.32175   0.24498
   2.76109   2.67795   2.55359   2.35619   2.03444   1.57080   1.10715   0.78540   0.58800   0.46365
   2.60117   2.49809   2.35619   2.15880   1.89255   1.57080   1.24905   0.98279   0.78540   0.64350
   2.46685   2.35619   2.21430   2.03444   1.81577   1.57080   1.32582   1.10715   0.92730   0.78540

那是怎么回事?我认为 theta 的第一个单元格应该〜0.785398。其他细胞似乎也错了。有什么我错过的吗?

4

2 回答 2

0

Atan2 不是安静的 atan,因为它考虑了象限。

见: http ://en.wikipedia.org/wiki/Atan2

你的第一个案例是atan(x/y) - Pi

于 2012-03-18T10:27:24.953 回答
0

我自己找到了答案。atan2不是简单atan地按单元应用。操作数的符号也很重要。

欲了解更多信息,请参阅:http ://sunsite.univie.ac.at/textbooks/octave/octave_17.html

然后如何atan2自己实现(正如我正在做的那样)请参阅:http ://en.wikipedia.org/wiki/Atan2

于 2012-03-18T10:23:47.670 回答