Skip to main content
 首页 » 编程设计

python之numpy arctan2 错误或使用问题

2025年05月04日9over140

我正在将一些函数从 Excel 电子表格转换为 Python。我需要使用atan2 根据 numpy 文档,这是 arctan2: arctan2 .问题是这两个结果甚至不接近:

oc = 23.4384863405 
sal = 89.7814630647 
sra = np.arctan2(np.cos(np.deg2rad(sal)), 
         np.cos(np.deg2rad(oc))*np.sin(np.deg2rad(sal))) 
results: Excel = 1.566714757 Numpy = 0.00415720646  ?? 

我相信 Excel 结果是正确的。错误的是 numpy。

现在要么我没有正确使用 arctan2,要么 atan2 不是 numpy 中的 arctan2,要么 numpy 中有错误,要么我完全迷失在这里。

我使用的是 python 版本 2.7.2 和 numpy 1.6.2

有什么想法吗? 谢谢

请您参考如下方法:

来自Excel docs :

The syntax for the ATAN2 function is:

ATAN2( x-coordinate, y-coordinate )

来自numpy docs :

numpy.arctan2(x1, x2[, out])

Element-wise arc tangent of x1/x2 choosing the quadrant correctly.

The quadrant (i.e., branch) is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray ending at the origin and passing through the point (1,0), and the ray ending at the origin and passing through the point (x2, x1). (Note the role reversal: the “y-coordinate” is the first function parameter, the “x-coordinate” is the second.)

他们以相反的顺序进行论证。因此:

In [31]: arctan2(cos(deg2rad(sal)), cos(deg2rad(oc))*sin(deg2rad(sal))) 
Out[31]: 0.0041572064598812417 
 
In [32]: arctan2(cos(deg2rad(oc))*sin(deg2rad(sal)), cos(deg2rad(sal))) 
Out[32]: 1.5666391203350154