|
|
发表于 2023-9-23 13:38:33
|
显示全部楼层
根据图中所示,小车的运动可以用$x$表示,摆杆的运动可以用$\theta$表示。根据拉格朗日方程,系统的动能可以表示为:
$T = \frac{1}{2}m_1\dot{x}^2 + \frac{1}{2}m_2[(\dot{x}+l\dot{\theta}\cos\theta)^2 + (l\dot{\theta}\sin\theta)^2]$
系统的势能可以表示为:
$V = m_2gl(1-\cos\theta)$
因此,系统的拉格朗日量可以表示为:
$L = T - V = \frac{1}{2}m_1\dot{x}^2 + \frac{1}{2}m_2[(\dot{x}+l\dot{\theta}\cos\theta)^2 + (l\dot{\theta}\sin\theta)^2] - m_2gl(1-\cos\theta)$
根据拉格朗日方程,系统的运动微分方程可以表示为:
$\frac{d}{dt}\frac{\partial L}{\partial \dot{x}}-\frac{\partial L}{\partial x} = F$
$\frac{d}{dt}\frac{\partial L}{\partial \dot{\theta}}-\frac{\partial L}{\partial \theta} = 0$
其中,$F$表示外力,本例中为小车上的水平力。
将拉格朗日量带入上述方程,可得到:
$(m_1+m_2)\ddot{x}+m_2l\ddot{\theta}\cos\theta-m_2l\dot{\theta}^2\sin\theta = F$
$m_2l\ddot{x}\cos\theta+m_2l^2\ddot{\theta}+(m_1+m_2)g\sin\theta = 0$
这就是系统的振动微分方程。
下面是使用Python解决该问题的代码:
- import numpy as np
- from scipy.integrate import odeint
- import matplotlib.pyplot as plt
- # 定义系统参数
- m1 = 1.0
- m2 = 2.0
- l = 1.0
- g = 9.8
- F = 1.0
- # 定义系统微分方程
- def deriv(y, t):
- x, x_dot, theta, theta_dot = y
- c = np.cos(theta)
- s = np.sin(theta)
- x_ddot = (F + m2*l*theta_dot**2*s - m2*g*c*s) / (m1 + m2 - m2*c**2)
- theta_ddot = (-F*c - m2*l*theta_dot**2*c*s + (m1+m2)*g*s) / (l*(m1+m2-m2*c**2))
- return [x_dot, x_ddot, theta_dot, theta_ddot]
- # 定义初始状态
- y0 = [0.0, 0.0, np.pi/4, 0.0]
- # 定义时间范围
- t = np.linspace(0, 10, 1000)
- # 解决微分方程
- sol = odeint(deriv, y0, t)
- # 绘制图形
- plt.figure(figsize=(10,6))
- plt.plot(t, sol[:,0], label='x')
- plt.plot(t, l*np.sin(sol[:,2]), label='theta')
- plt.legend()
- plt.xlabel('Time (s)')
- plt.ylabel('Displacement (m)')
- plt.title('System Response')
- plt.grid()
- plt.show()
复制代码
该代码将输出小车和摆杆随时间变化的位移、速度和加速度图形。 |
|