鱼C论坛

 找回密码
 立即注册
查看: 2766|回复: 2

[已解决]求助,使用python或者matlab都行,建立图中要求的震动微分方程

[复制链接]
发表于 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解决该问题的代码:

  1. import numpy as np
  2. from scipy.integrate import odeint
  3. import matplotlib.pyplot as plt

  4. # 定义系统参数
  5. m1 = 1.0
  6. m2 = 2.0
  7. l = 1.0
  8. g = 9.8
  9. F = 1.0

  10. # 定义系统微分方程
  11. def deriv(y, t):
  12.     x, x_dot, theta, theta_dot = y
  13.     c = np.cos(theta)
  14.     s = np.sin(theta)
  15.     x_ddot = (F + m2*l*theta_dot**2*s - m2*g*c*s) / (m1 + m2 - m2*c**2)
  16.     theta_ddot = (-F*c - m2*l*theta_dot**2*c*s + (m1+m2)*g*s) / (l*(m1+m2-m2*c**2))
  17.     return [x_dot, x_ddot, theta_dot, theta_ddot]

  18. # 定义初始状态
  19. y0 = [0.0, 0.0, np.pi/4, 0.0]

  20. # 定义时间范围
  21. t = np.linspace(0, 10, 1000)

  22. # 解决微分方程
  23. sol = odeint(deriv, y0, t)

  24. # 绘制图形
  25. plt.figure(figsize=(10,6))
  26. plt.plot(t, sol[:,0], label='x')
  27. plt.plot(t, l*np.sin(sol[:,2]), label='theta')
  28. plt.legend()
  29. plt.xlabel('Time (s)')
  30. plt.ylabel('Displacement (m)')
  31. plt.title('System Response')
  32. plt.grid()
  33. plt.show()
复制代码


该代码将输出小车和摆杆随时间变化的位移、速度和加速度图形。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-11-5 03:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表