我试图在pi/24的步骤中扫描pi/24到pi/2的值。目前我得到一个错误,与浮点对象不能被解释为一个整数。有什么解决办法吗?
我的代码示例:for theta in range(np.pi/24,np.pi/2,np.pi/24):
v_initial_x = np.cos(theta)*velocity
v_initial_y = np.sin(theta)*velocity
这是因为内置函数range
只接受整数。你可以像这样使用numpy:
for theta in np.arange(np.pi/24, np.pi/2, np.pi/24):
''' Do your stuff here '''
请记住,停止值被排除在范围之外,所以如果您想在计算中包括它,那么您必须将np.pi/24
添加到停止值中。
另一种选择:
import numpy as np
velocity = 4.0
for theta in range(1, 13):
v_initial_x = np.cos((np.pi*theta)/24) * velocity
v_initial_y = np.sin((np.pi*theta)/24) * velocity
print(f"vx - {round(v_initial_y, 2):<10} vy - {round(v_initial_x, 2)}")
vx - 0.52 vy - 3.97
vx - 1.04 vy - 3.86
vx - 1.53 vy - 3.7
vx - 2.0 vy - 3.46
vx - 2.44 vy - 3.17
vx - 2.83 vy - 2.83
vx - 3.17 vy - 2.44
vx - 3.46 vy - 2.0
vx - 3.7 vy - 1.53
vx - 3.86 vy - 1.04
vx - 3.97 vy - 0.52
vx - 4.0 vy - 0.0