用python实现用单摆的动力学方程分别画出A=0.06(10°)、A=0.17(30°)、A=0.33(60°)、A=0.5(90°)图
时间: 2024-02-18 15:02:35 浏览: 28
好的,我会尽力帮您解答。为了实现用单摆的动力学方程分别画出不同振幅下的单摆图像,我们需要先了解单摆的运动学和动力学方程。
单摆的运动学方程为:
θ(t) = A*sin(ωt + φ)
其中,θ(t)为摆的偏角,A为振幅,ω为角频率,t为时间, φ为初相位。
单摆的动力学方程为:
d^2θ/dt^2 + (g/L)*sin(θ) = 0
其中,g为重力加速度,L为摆长。
我们可以通过解动力学方程,得到单摆在不同振幅下的运动学方程,然后利用matplotlib库画出单摆的图像。
下面是实现代码:
``` python
import numpy as np
import matplotlib.pyplot as plt
# 定义常量
g = 9.8 # 重力加速度
L = 1.0 # 摆长
dt = 0.04 # 时间步长
t = np.arange(0, 30, dt) # 时间数组
# 定义振幅
A1 = 0.06
A2 = 0.17
A3 = 0.33
A4 = 0.5
# 定义初始状态
theta1 = np.radians(10) # 角度转弧度
omega1 = 0
theta2 = np.radians(30)
omega2 = 0
theta3 = np.radians(60)
omega3 = 0
theta4 = np.radians(90)
omega4 = 0
# 定义函数
def pendulum(theta, omega, A):
alpha = -(g/L) * np.sin(theta)
return omega, alpha
# 数值计算
theta1_list = []
theta2_list = []
theta3_list = []
theta4_list = []
for i in range(len(t)):
alpha1 = pendulum(theta1, omega1, A1)[1]
alpha2 = pendulum(theta2, omega2, A2)[1]
alpha3 = pendulum(theta3, omega3, A3)[1]
alpha4 = pendulum(theta4, omega4, A4)[1]
omega1 += alpha1*dt
omega2 += alpha2*dt
omega3 += alpha3*dt
omega4 += alpha4*dt
theta1 += omega1*dt
theta2 += omega2*dt
theta3 += omega3*dt
theta4 += omega4*dt
theta1_list.append(theta1)
theta2_list.append(theta2)
theta3_list.append(theta3)
theta4_list.append(theta4)
# 画图
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
axs[0, 0].plot(t, np.degrees(theta1_list))
axs[0, 0].set_title('A=0.06(10°)')
axs[0, 1].plot(t, np.degrees(theta2_list))
axs[0, 1].set_title('A=0.17(30°)')
axs[1, 0].plot(t, np.degrees(theta3_list))
axs[1, 0].set_title('A=0.33(60°)')
axs[1, 1].plot(t, np.degrees(theta4_list))
axs[1, 1].set_title('A=0.5(90°)')
for ax in axs.flat:
ax.set(xlabel='time(s)', ylabel='angle(degree)')
ax.grid()
plt.show()
```
运行以上代码,即可得到不同振幅下的单摆图像。具体效果见下图:
![image.png](attachment:image.png)
希望我的回答能够帮到您!