基于python plt的平面三体运动动态图

本文展示了使用Python中的matplotlib库和相关数学函数,模拟和动态绘制三体系统中星球之间的引力相互作用,生成了星体运动的动画效果。
摘要由CSDN通过智能技术生成
"""
三体运动动态图
"""
import math
import matplotlib
import matplotlib.patches as mpatches
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation

matplotlib.use('TkAgg')
fig, ax = plt.subplots(figsize=(6, 6))

G = 20  # 引力常数

# star恒星,planet行星
# 位置、速度
starPos = [[2, 2], [8, 20], [30, 9], [15, 15]]
starVel = [[0, 2], [3, 0], [-0.5, 1], [-4, -2]]
starMass = [6, 4, 5, 1]
# Circle(tuple(x,y), 半径radius, color, ...)
star0 = mpatches.Circle(starPos[0], starMass[0] / 5, color='red')
star1 = mpatches.Circle(starPos[1], starMass[1] / 5, color='green')
star2 = mpatches.Circle(starPos[2], starMass[2] / 5, color='blue')
planet = mpatches.Circle(starPos[3], starMass[3] / 5, color='black')

def planetCalculate():
    x1 = starPos[0][0] - starPos[3][0]
    y1 = starPos[0][1] - starPos[3][1]
    a1 = G * starMass[0] / (x1 ** 2 + y1 ** 2)
    angle1 = math.atan2(y1, x1)

    x2 = starPos[1][0] - starPos[3][0]
    y2 = starPos[1][1] - starPos[3][1]
    a2 = G * starMass[1] / (x2 ** 2 + y2 ** 2)
    angle2 = math.atan2(y2, x2)

    x3 = starPos[2][0] - starPos[3][0]
    y3 = starPos[2][1] - starPos[3][1]
    a3 = G * starMass[2] / (x3 ** 2 + y3 ** 2)
    angle3 = math.atan2(y3, x3)

    starPos[3][0] += starVel[3][0] * dt
    starPos[3][1] += starVel[3][1] * dt

    points.append(starPos[3])

    starVel[3][0] += (a1 * math.cos(angle1) + a2 * math.cos(angle2) + a3 * math.cos(angle3)) * dt
    starVel[3][1] += (a1 * math.sin(angle1) + a2 * math.sin(angle2) + a3 * math.sin(angle3)) * dt


def star0Calculate():
    x1 = starPos[1][0] - starPos[0][0]
    y1 = starPos[1][1] - starPos[0][1]
    a1 = G * starMass[1] / (x1 ** 2 + y1 ** 2)
    angle1 = math.atan2(y1, x1)

    x2 = starPos[2][0] - starPos[0][0]
    y2 = starPos[2][1] - starPos[0][1]
    a2 = G * starMass[2] / (x2 ** 2 + y2 ** 2)
    angle2 = math.atan2(y2, x2)

    starPos[0][0] += starVel[0][0] * dt
    starPos[0][1] += starVel[0][1] * dt

    starVel[0][0] += a1 * math.cos(angle1) * dt + a2 * math.cos(angle2) * dt
    starVel[0][1] += a1 * math.sin(angle1) * dt + a2 * math.sin(angle2) * dt


def star1Calculate():
    x1 = starPos[0][0] - starPos[1][0]
    y1 = starPos[0][1] - starPos[1][1]
    a1 = G * starMass[0] / (x1 ** 2 + y1 ** 2)
    angle1 = math.atan2(y1, x1)

    x2 = starPos[2][0] - starPos[1][0]
    y2 = starPos[2][1] - starPos[1][1]
    a2 = G * starMass[2] / (x2 ** 2 + y2 ** 2)
    angle2 = math.atan2(y2, x2)

    starPos[1][0] += starVel[1][0] * dt
    starPos[1][1] += starVel[1][1] * dt

    starVel[1][0] += a1 * math.cos(angle1) * dt + a2 * math.cos(angle2) * dt
    starVel[1][1] += a1 * math.sin(angle1) * dt + a2 * math.sin(angle2) * dt


def star2Calculate():
    x1 = starPos[0][0] - starPos[2][0]
    y1 = starPos[0][1] - starPos[2][1]
    a1 = G * starMass[0] / (x1 ** 2 + y1 ** 2)
    angle1 = math.atan2(y1, x1)

    x2 = starPos[1][0] - starPos[2][0]
    y2 = starPos[1][1] - starPos[2][1]
    a2 = G * starMass[1] / (x2 ** 2 + y2 ** 2)
    angle2 = math.atan2(y2, x2)

    starPos[2][0] += starVel[2][0] * dt
    starPos[2][1] += starVel[2][1] * dt

    starVel[2][0] += a1 * math.cos(angle1) * dt + a2 * math.cos(angle2) * dt
    starVel[2][1] += a1 * math.sin(angle1) * dt + a2 * math.sin(angle2) * dt


def calculatePosition():
    planetCalculate()
    star0Calculate()
    star1Calculate()
    star2Calculate()


def limit(i):
    l = min(starPos[0][i], starPos[1][i], starPos[2][i], starPos[3][i])
    r = max(starPos[0][i], starPos[1][i], starPos[2][i], starPos[3][i])
    return l - (r - l) * 0.2, r + (r - l) * 0.2


# 定义每帧更新图形的方法
def animate(i):
    calculatePosition()
    # 设置star0的位置
    star0.center = starPos[0]
    star1.center = starPos[1]
    star2.center = starPos[2]
    planet.center = starPos[3]
    ax.add_patch(star0)
    ax.add_patch(star1)
    ax.add_patch(star2)
    ax.add_patch(planet)

    ax.set_xlim(limit(0))
    ax.set_ylim(limit(1))
    return star0, star1, star2, planet


ax.axis('equal')

dt = 0.01  # 时间间隔
frames = int(5 / dt)  # 总帧数

ani = FuncAnimation(fig, animate, frames=frames, interval=20)
plt.grid()
plt.show()

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python中可以使用科学计算库NumPy来模拟三体运动。以下是一个简单的示例代码: ```python import numpy as np import matplotlib.pyplot as plt # 定义常数 G = 6.67430e-11 # 万有引力常数 # 定义初始条件 m1 = 5.972e24 # 地球质量 m2 = 7.348e22 # 月球质量 m3 = 1.989e30 # 太阳质量 r1 = np.array([0, 0]) # 地球位置 r2 = np.array([384400000, 0]) # 月球位置 r3 = np.array([0, 0]) # 太阳位置 v1 = np.array([0, 0]) # 地球速度 v2 = np.array([0, np.sqrt(G * m3 / np.linalg.norm(r2 - r3))]) # 月球速度 v3 = np.array([0, -np.sqrt(G * m2 / np.linalg.norm(r1 - r2))]) # 太阳速度 # 定义时间步长和总时长 dt = 3600 # 时间步长(秒) total_time = 86400 * 30 # 总时长(秒) # 初始化轨迹数组 r1_track = [r1] r2_track = [r2] r3_track = [r3] # 进行模拟 for t in range(0, total_time, dt): # 计算加速度 a1 = G * (m2 * (r2 - r1) / np.linalg.norm(r2 - r1)**3 + m3 * (r3 - r1) / np.linalg.norm(r3 - r1)**3) a2 = G * (m1 * (r1 - r2) / np.linalg.norm(r1 - r2)**3 + m3 * (r3 - r2) / np.linalg.norm(r3 - r2)**3) a3 = G * (m1 * (r1 - r3) / np.linalg.norm(r1 - r3)**3 + m2 * (r2 - r3) / np.linalg.norm(r2 - r3)**3) # 更新位置和速度 r1 += v1 * dt v1 += a1 * dt r2 += v2 * dt v2 += a2 * dt r3 += v3 * dt v3 += a3 * dt # 记录轨迹 r1_track.append(r1.copy()) r2_track.append(r2.copy()) r3_track.append(r3.copy()) # 绘制轨迹 r1_track = np.array(r1_track) r2_track = np.array(r2_track) r3_track = np.array(r3_track) plt.plot(r1_track[:, 0], r1_track[:, 1], label='Earth') plt.plot(r2_track[:, 0], r2_track[:, 1], label='Moon') plt.plot(r3_track[:, 0], r3_track[:, 1], label='Sun') plt.xlabel('x') plt.ylabel('y') plt.title('Three-Body Motion') plt.legend() plt.show() ``` 该代码使用欧拉方法进行数值模拟,通过计算每个物体的加速度并更新位置和速度。最后使用Matplotlib库将轨迹绘制出来。 请注意,这只是一个简单的示例,并且在实际的三体运动中可能存在更多复杂性和数值稳定性问题。如果需要更精确的模拟,可以考虑使用更高阶的数值积分方法,例如四阶龙格-库塔法(RK4)等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值