python圆周运动
时间: 2023-12-01 09:43:42 浏览: 36
以下是一个简单的Python圆周运动的例子,使用matplotlib.animation库绘制动态图形:
```python
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
# 定义粒子类
class Particle:
def __init__(self, x, y, ang_vel):
self.x = x
self.y = y
self.ang_vel = ang_vel
# 定义圆周运动函数
def circular_motion(particle, dt):
r = 1.0 # 半径
theta = particle.ang_vel * dt # 角度
particle.x = r * np.cos(theta) # 计算x坐标
particle.y = r * np.sin(theta) # 计算y坐标
particle.ang_vel += 0.1 # 角速度增加
# 初始化粒子
particle = Particle(0, 0, 0)
# 绘制动态图
fig, ax = plt.subplots()
line, = ax.plot([], [], 'o')
def init():
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
return line,
def update(frame):
circular_motion(particle, 0.1)
line.set_data([particle.x], [particle.y])
return line,
ani = animation.FuncAnimation(fig, update, frames=100, init_func=init, blit=True)
plt.show()
```