我的代码抛出了一个TypeError:只有size-1数组才能转换为Python标量



我的代码抛出了这个错误,但我将其与其他人的代码进行了比较,结果基本相同,有人知道为什么吗?

def solar_system(T, x_e, x_m, omega_e, omega_m):
"""Plot the trajectories of the earth and moon over the time interval [0,T]
assuming the initial position of the earth is (x_e,0) and the initial
position of the moon is (x_m,0).
Parameters:
T (int): The final time.
x_e (float): The earth's initial x coordinate.
x_m (float): The moon's initial x coordinate.
omega_e (float): The earth's angular velocity.
omega_m (float): The moon's angular velocity.
"""
#raise NotImplementedError("Problem 2 Incomplete")

t = np.linspace(0, T, 1000)
earth_pos = []
moon_pos = []
for i in t:
new_pos_e = [[math.cos(t*omega_e), -math.sin(t*omega_e)], [math.sin(t*omega_e), math.cos(t*omega_e)]] @ np.array([x_e,0]).T
earth_pos.append(new_pos_e)
earth_pos = np.array(earth_pos)
plt.plot(earth_pos[:,0], earth_pos[:,1], 'b-', linewidth=3)

for i in t:
new_pos_m = [[math.cos(t*omega_m), -math.sin(t*omega_m)], [math.sin(t*omega_m), math.cos(t*omega_m)]] @ np.array([(x_m - x_e), 0]).T
moon_pos.append(new_pos_m)
moon_pos = np.array(moon_pos)
plt.plot(moon_pos[:,0]+earth_pos[:,0], moon_pos[:,1]+earth_pos[:,1], "r-", linewidth=3)

plt.axis("equal")
plt.xlabel("x axis co-ordinates")
plt.ylabel("y axis co-ordinates")
plt.title("Earth and Moon Rotation")
plt.legend("Earth", "Moon", loc="lower right")
plt.show()

它抛出的错误如下:

TypeError: only size-1 arrays can be converted to Python scalars

上面写着错误在这一行:

new_pos_e = [[math.cos(t*omega_e), -math.sin(t*omega_e)], [math.sin(t*omega_e), math.cos(t*omega_e)]] @ np.array([x_e,0]).T
enter code here

我用这个来运行函数:

solar_system(3*np.pi/2, 10, 11, 1, 13)

在两个循环中,应该使用i而不是t,修复了以下完整代码:

import matplotlib.pyplot as plt, numpy as np, math
def solar_system(T, x_e, x_m, omega_e, omega_m):
"""Plot the trajectories of the earth and moon over the time interval [0,T]
assuming the initial position of the earth is (x_e,0) and the initial
position of the moon is (x_m,0).
Parameters:
T (int): The final time.
x_e (float): The earth's initial x coordinate.
x_m (float): The moon's initial x coordinate.
omega_e (float): The earth's angular velocity.
omega_m (float): The moon's angular velocity.
"""
#raise NotImplementedError("Problem 2 Incomplete")
t = np.linspace(0, T, 1000)
earth_pos = []
moon_pos = []
for i in t:
new_pos_e = [[math.cos(i * omega_e), -math.sin(i * omega_e)], [math.sin(i * omega_e), math.cos(i * omega_e)]] @ np.array([x_e, 0]).T
earth_pos.append(new_pos_e)
earth_pos = np.array(earth_pos)
plt.plot(earth_pos[:,0], earth_pos[:,1], 'b-', linewidth=3)
for i in t:
new_pos_m = [[math.cos(i * omega_m), -math.sin(i * omega_m)], [math.sin(i * omega_m), math.cos(i * omega_m)]] @ np.array([(x_m - x_e), 0]).T
moon_pos.append(new_pos_m)
moon_pos = np.array(moon_pos)
plt.plot(moon_pos[:,0]+earth_pos[:,0], moon_pos[:,1]+earth_pos[:,1], "r-", linewidth=3)

plt.axis("equal")
plt.xlabel("x axis co-ordinates")
plt.ylabel("y axis co-ordinates")
plt.title("Earth and Moon Rotation")
plt.legend("Earth", "Moon", loc="lower right")
plt.show()
solar_system(3*np.pi/2, 10, 11, 1, 13)

最新更新