三体运动python
时间: 2023-11-12 21:58:06 浏览: 152
三体运动是指三个物体之间的相互作用和运动,可以使用Python进行模拟。其中,可以使用scipy库中的odeint函数求解微分方程组,来模拟三体运动。
以下是一个简单的三体运动模拟代码示例:
```python
import numpy as np
from scipy.integrate import odeint
# 定义微分方程组
def three_body_equations(w, t, G, m1, m2, m3):
x1, y1, vx1, vy1, x2, y2, vx2, vy2, x3, y3, vx3, vy3 = w
r12 = np.sqrt((x2 - x1)**2 + (y2 - y1)**2)
r13 = np.sqrt((x3 - x1)**2 + (y3 - y1)**2)
r23 = np.sqrt((x3 - x2)**2 + (y3 - y2)**2)
dx1dt = vx1
dy1dt = vy1
dvx1dt = G * m2 * (x2 - x1) / r12**3 + G * m3 * (x3 - x1) / r13**3
dvy1dt = G * m2 * (y2 - y1) / r12**3 + G * m3 * (y3 - y1) / r13**3
dx2dt = vx2
dy2dt = vy2
dvx2dt = G * m1 * (x1 - x2) / r12**3 + G * m3 * (x3 - x2) / r23**3
dvy2dt = G * m1 * (y1 - y2) / r12**3 + G * m3 * (y3 - y2) / r23**3
dx3dt = vx3
dy3dt = vy3
dvx3dt = G * m1 * (x1 - x3) / r13**3 + G * m2 * (x2 - x3) / r23**3
dvy3dt = G * m1 * (y1 - y3) / r13**3 + G * m2 * (y2 - y3) / r23**3
return dx1dt, dy1dt, dvx1dt, dvy1dt, dx2dt, dy2dt, dvx2dt, dvy2dt, dx3dt, dy3dt, dvx3dt, dvy3dt
# 定义初始状态和参数
w0 = [1, 0, 0, 6, -1, 0, 0, -6, 0, 0, 0, 0]
t = np.linspace(0, 10, 1000)
G = 1
m1 = 1
m2 = 1
m3 = 1
# 求解微分方程组
wsol = odeint(three_body_equations, w0, t, args=(G, m1, m2, m3))
# 绘制轨迹图
import matplotlib.pyplot as plt
plt.plot(wsol[:,0], wsol[:,1], label='Body 1')
plt.plot(wsol[:,4], wsol[:,5], label='Body 2')
plt.plot(wsol[:,8], wsol[:,9], label='Body 3')
plt.legend()
plt.show()
```
阅读全文