我是python的新手,所以请原谅我在代码中的胡言乱语。我正在尝试使用我的经典力学教科书(约翰·R·泰勒的经典力学)中的方程,在python中编写行星的圆形轨道(我刚刚使用了天王星和太阳的质量)。我想我可以使用方程,并绘制一个函数,y,它等于一个圆的方程,c_squared是半径,x是用来绘制圆的值的数组。让我知道我可以如何改进代码,或者我正在朝着正确的方向前进。
..。
import matplotlib.pyplot as plt
import numpy as np
import matplotlib
import math
fig = plt.figure()
ax = fig.add_subplot()
m_uranus = 8.681 * 10**(25)
m_sun = 1.989 * 10 **(30)
G = 6.67430 * 10**(-11)
mu = (m_uranus * m_sun)/(m_uranus + m_sun)
l = (1.7 * 10**(42)) * 1000 * 24 * 60 * 60
ang_squared = l ** 2
c = (ang_squared)/(G * m_uranus * m_sun * mu)
c_squared = c**2
print(m_sun, mu, m_uranus, ang_squared, c)
x = np.arange(-100, 100, 1)
y = math.sqrt(c_squared - x)
plt.plot(x, y)
plt.show()..。
发布于 2021-04-19 06:09:07
正如@JohanC所提到的,使用numpy np.sqrt()而不是math.sqrt()将修复您的错误,这里的修复是(删除不必要的库):
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot()
m_uranus = 8.681 * 10 ** 25
m_sun = 1.989 * 10 ** 30
G = 6.67430 * 10 ** (-11)
mu = (m_uranus * m_sun) / (m_uranus + m_sun)
l = (1.7 * 10 ** 42) * 1000 * 24 * 60 * 60
ang_squared = l ** 2
c = ang_squared / (G * m_uranus * m_sun * mu)
c_squared = c ** 2
print(m_sun, mu, m_uranus, ang_squared, c)
x = np.arange(-100, 100, 1)
y = np.sqrt(c_squared - x)
plt.plot(x, y)
plt.show()
希望,这会对你有所帮助!
https://stackoverflow.com/questions/67153502
复制相似问题