用余弦定律计算向量(Python)

2024-05-12 15:40:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着开发一个控制台应用程序,用余弦定律解决向量之间的加法:

sum = sqrt((s1 ** 2) + (s2 ** 2) + (2 * s1 * s2 * cos(angle)))
print(sum)

# Where s1 and s2 are the sizes of the vectors, respectively.

但是,等式中的cos返回了一个奇怪的值(角度是60,所以cos(angle)应该是1/2,对吧?)。你知道吗

另外,在阅读其他解决方案后,我尝试用acos更改cos,但它返回ValueError: math domain error。你知道吗

有人知道怎么解决这个问题吗?你知道吗


Tags: andthe应用程序sqrtcoswhere向量are
2条回答

cos()的参数应以弧度表示。你知道吗

Python的三角函数使用弧度,而不是度数。幸运的是,math模块包含一个函数来为您执行转换:

from math import cos, radians

sum = sqrt((s1 ** 2) + (s2 ** 2) + (2 * s1 * s2 * cos(radians(angle))))
print(sum)

相关问题 更多 >