鱼C论坛

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

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

[复制链接]
发表于 2023-9-23 13:38:05 | 显示全部楼层 |阅读模式
3鱼币
对图中例题3-5中的系统,使用拉格朗日方程建立系统的振动微分方程,并用Matlab或python软件作出小车及摆杆位移、速度及加速度随时间变化的图形。最好附带注释
最佳答案
2023-9-23 13:38:06
根据您提供的信息,我理解您需要建立一个小车与摆杆相连的物理系统的微分方程,并使用 Matlab 或 Python 软件绘制出小车和摆杆的运动轨迹。以下是步骤:

1. 建立拉格朗日方程

首先,我们需要建立该物理系统的拉格朗日方程。假设小车位于直线轨道上,摆杆的质心距离小车为 $l$,摆杆的质量为 $m$,小车的质量为 $M$,地球引力加速度为 $g$。则该系统的自由度为 2,可以定义两个广义坐标:

$x$:小车的位置(沿水平方向的位移)。

$\theta$:摆杆与竖直方向之间的夹角。

在此基础上,该系统的拉格朗日方程可以表示为:

$$L=T-V=\frac{1}{2}M\dot x^2+\frac{1}{2}m(\dot x^2+l^2\dot \theta^2+2l\dot x\dot \theta\cos\theta)-mgl\cos\theta$$

其中,$T$ 和 $V$ 分别表示系统的动能和势能。

然后,应用欧拉-拉格朗日方程,可以得到小车和摆杆的微分方程:

$$\begin{cases} (M+m)\ddot x+ml\ddot\theta\cos\theta-ml\dot \theta^2\sin\theta=F \\ ml\ddot x\cos\theta+(M+m)l\ddot\theta+mglsin\theta=0 \end{cases}$$

其中,$F$ 表示小车受到的外力。

2. 使用 MATLAB 或 Python 绘制运动轨迹

接下来,我们可以使用 MATLAB 或 Python 软件绘制出小车和摆杆的运动轨迹。以下是 MATLAB 代码示例:

  1. clear;clc;

  2. % 定义系统参数
  3. M = 1;      % 小车质量
  4. m = 0.1;    % 摆杆质量
  5. l = 0.5;    % 摆杆长度
  6. g = 9.8;    % 重力加速度

  7. % 定义初始状态
  8. x0 = 0;         % 小车起始位置
  9. dx0 = 0;        % 小车起始速度
  10. theta0 = 0.1;   % 摆杆起始角度
  11. dtheta0 = 0;    % 摆杆起始角速度

  12. % 定义仿真时间和时间步长
  13. T = 10;     % 仿真时间
  14. dt = 0.01;  % 时间步长

  15. % 定义外力函数
  16. F = @(t) 0.5*sin(t);    % 外力随时间的变化

  17. % 初始化状态和时间向量
  18. tvec = 0:dt:T;
  19. xvec = zeros(size(tvec));
  20. dxvec = zeros(size(tvec));
  21. thetaVec = zeros(size(tvec));
  22. dthetaVec = zeros(size(tvec));

  23. % 设置初始状态
  24. xvec(1) = x0;
  25. dxvec(1) = dx0;
  26. thetaVec(1) = theta0;
  27. dthetaVec(1) = dtheta0;

  28. % 对系统微分方程进行数值解
  29. for i = 1:numel(tvec)-1
  30.     t = tvec(i);
  31.     x = xvec(i);
  32.     dx = dxvec(i);
  33.     theta = thetaVec(i);
  34.     dtheta = dthetaVec(i);
  35.     Fext = F(t);
  36.    
  37.     % 计算加速度
  38.     [ddx, ddtheta] = compute_acceleration(x,dx,theta,dtheta,Fext,M,m,l,g);
  39.    
  40.     % 进行数值积分
  41.     xvec(i+1) = x + dx*dt;
  42.     dxvec(i+1) = dx + ddx*dt;
  43.     thetaVec(i+1) = theta + dtheta*dt;
  44.     dthetaVec(i+1) = dtheta + ddtheta*dt;
  45. end

  46. % 绘制小车和摆杆的运动轨迹
  47. figure(1);
  48. subplot(3,1,1);
  49. plot(tvec,xvec,'LineWidth',2);
  50. xlabel('Time (s)');
  51. ylabel('Position (m)');
  52. title('Position of Cart');

  53. subplot(3,1,2);
  54. plot(tvec,thetaVec,'LineWidth',2);
  55. xlabel('Time (s)');
  56. ylabel('Angle (rad)');
  57. title('Angle of Pendulum');

  58. subplot(3,1,3);
  59. cart_width = 0.2;
  60. cart_height = 0.1;
  61. pendulum_length = 1.0;
  62. for i = 1:numel(tvec)
  63.     clf;
  64.     hold on;
  65.     plot([-5 5],[0 0],'k','LineWidth',2);
  66.     plot([xvec(i)-cart_width/2 xvec(i)+cart_width/2 xvec(i)+cart_width/2 xvec(i)-cart_width/2 xvec(i)-cart_width/2],...
  67.         [0 0 cart_height cart_height 0],'b','LineWidth',2);
  68.     pendulum_x = xvec(i) + pendulum_length*sin(thetaVec(i));
  69.     pendulum_y = pendulum_length*cos(thetaVec(i));
  70.     plot([xvec(i),pendulum_x],[0,pendulum_y],'r','LineWidth',3);
  71.     axis equal;
  72.     xlim([-5 5]);
  73.     ylim([-2 2]);
  74.     xlabel('Position (m)');
  75.     ylabel('Height (m)');
  76.     title(sprintf('Time=%.2f s', tvec(i)));
  77.     drawnow;
  78. end

  79. % 计算小车和摆杆的加速度
  80. function [ddx, ddtheta] = compute_acceleration(x,dx,theta,dtheta,Fext,M,m,l,g)
  81.     % 计算拉格朗日方程的各项系数
  82.     a = M+m;
  83.     b = m*l*cos(theta);
  84.     c = -m*l*dtheta^2*sin(theta);
  85.     d = Fext+m*g*sin(theta);
  86.    
  87.     % 解方程得到加速度
  88.     ddtheta = (d*a-b*dtheta^2*cos(theta))/(-b^2+a*c);
  89.     ddx = (d-b*ddtheta*cos(theta))/a;
  90. end
复制代码


运行上述代码后将会绘制出模拟结果。代码中,我们首先定义了系统参数和初始状态,并设置了仿真时间和时间步长。然后,我们使用欧拉-前向差分法对微分方程进行数值解,并绘制出小车和摆杆的运动轨迹(在第三个子图中)。

希望这能帮助到您!
如果问题已经解决,请设置最佳答案

例题3-5

例题3-5

最佳答案

查看完整内容

根据您提供的信息,我理解您需要建立一个小车与摆杆相连的物理系统的微分方程,并使用 Matlab 或 Python 软件绘制出小车和摆杆的运动轨迹。以下是步骤: 1. 建立拉格朗日方程 首先,我们需要建立该物理系统的拉格朗日方程。假设小车位于直线轨道上,摆杆的质心距离小车为 $l$,摆杆的质量为 $m$,小车的质量为 $M$,地球引力加速度为 $g$。则该系统的自由度为 2,可以定义两个广义坐标: $x$:小车的位置(沿水平方向的位 ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-9-23 13:38:06 | 显示全部楼层    本楼为最佳答案   
根据您提供的信息,我理解您需要建立一个小车与摆杆相连的物理系统的微分方程,并使用 Matlab 或 Python 软件绘制出小车和摆杆的运动轨迹。以下是步骤:

1. 建立拉格朗日方程

首先,我们需要建立该物理系统的拉格朗日方程。假设小车位于直线轨道上,摆杆的质心距离小车为 $l$,摆杆的质量为 $m$,小车的质量为 $M$,地球引力加速度为 $g$。则该系统的自由度为 2,可以定义两个广义坐标:

$x$:小车的位置(沿水平方向的位移)。

$\theta$:摆杆与竖直方向之间的夹角。

在此基础上,该系统的拉格朗日方程可以表示为:

$$L=T-V=\frac{1}{2}M\dot x^2+\frac{1}{2}m(\dot x^2+l^2\dot \theta^2+2l\dot x\dot \theta\cos\theta)-mgl\cos\theta$$

其中,$T$ 和 $V$ 分别表示系统的动能和势能。

然后,应用欧拉-拉格朗日方程,可以得到小车和摆杆的微分方程:

$$\begin{cases} (M+m)\ddot x+ml\ddot\theta\cos\theta-ml\dot \theta^2\sin\theta=F \\ ml\ddot x\cos\theta+(M+m)l\ddot\theta+mglsin\theta=0 \end{cases}$$

其中,$F$ 表示小车受到的外力。

2. 使用 MATLAB 或 Python 绘制运动轨迹

接下来,我们可以使用 MATLAB 或 Python 软件绘制出小车和摆杆的运动轨迹。以下是 MATLAB 代码示例:

  1. clear;clc;

  2. % 定义系统参数
  3. M = 1;      % 小车质量
  4. m = 0.1;    % 摆杆质量
  5. l = 0.5;    % 摆杆长度
  6. g = 9.8;    % 重力加速度

  7. % 定义初始状态
  8. x0 = 0;         % 小车起始位置
  9. dx0 = 0;        % 小车起始速度
  10. theta0 = 0.1;   % 摆杆起始角度
  11. dtheta0 = 0;    % 摆杆起始角速度

  12. % 定义仿真时间和时间步长
  13. T = 10;     % 仿真时间
  14. dt = 0.01;  % 时间步长

  15. % 定义外力函数
  16. F = @(t) 0.5*sin(t);    % 外力随时间的变化

  17. % 初始化状态和时间向量
  18. tvec = 0:dt:T;
  19. xvec = zeros(size(tvec));
  20. dxvec = zeros(size(tvec));
  21. thetaVec = zeros(size(tvec));
  22. dthetaVec = zeros(size(tvec));

  23. % 设置初始状态
  24. xvec(1) = x0;
  25. dxvec(1) = dx0;
  26. thetaVec(1) = theta0;
  27. dthetaVec(1) = dtheta0;

  28. % 对系统微分方程进行数值解
  29. for i = 1:numel(tvec)-1
  30.     t = tvec(i);
  31.     x = xvec(i);
  32.     dx = dxvec(i);
  33.     theta = thetaVec(i);
  34.     dtheta = dthetaVec(i);
  35.     Fext = F(t);
  36.    
  37.     % 计算加速度
  38.     [ddx, ddtheta] = compute_acceleration(x,dx,theta,dtheta,Fext,M,m,l,g);
  39.    
  40.     % 进行数值积分
  41.     xvec(i+1) = x + dx*dt;
  42.     dxvec(i+1) = dx + ddx*dt;
  43.     thetaVec(i+1) = theta + dtheta*dt;
  44.     dthetaVec(i+1) = dtheta + ddtheta*dt;
  45. end

  46. % 绘制小车和摆杆的运动轨迹
  47. figure(1);
  48. subplot(3,1,1);
  49. plot(tvec,xvec,'LineWidth',2);
  50. xlabel('Time (s)');
  51. ylabel('Position (m)');
  52. title('Position of Cart');

  53. subplot(3,1,2);
  54. plot(tvec,thetaVec,'LineWidth',2);
  55. xlabel('Time (s)');
  56. ylabel('Angle (rad)');
  57. title('Angle of Pendulum');

  58. subplot(3,1,3);
  59. cart_width = 0.2;
  60. cart_height = 0.1;
  61. pendulum_length = 1.0;
  62. for i = 1:numel(tvec)
  63.     clf;
  64.     hold on;
  65.     plot([-5 5],[0 0],'k','LineWidth',2);
  66.     plot([xvec(i)-cart_width/2 xvec(i)+cart_width/2 xvec(i)+cart_width/2 xvec(i)-cart_width/2 xvec(i)-cart_width/2],...
  67.         [0 0 cart_height cart_height 0],'b','LineWidth',2);
  68.     pendulum_x = xvec(i) + pendulum_length*sin(thetaVec(i));
  69.     pendulum_y = pendulum_length*cos(thetaVec(i));
  70.     plot([xvec(i),pendulum_x],[0,pendulum_y],'r','LineWidth',3);
  71.     axis equal;
  72.     xlim([-5 5]);
  73.     ylim([-2 2]);
  74.     xlabel('Position (m)');
  75.     ylabel('Height (m)');
  76.     title(sprintf('Time=%.2f s', tvec(i)));
  77.     drawnow;
  78. end

  79. % 计算小车和摆杆的加速度
  80. function [ddx, ddtheta] = compute_acceleration(x,dx,theta,dtheta,Fext,M,m,l,g)
  81.     % 计算拉格朗日方程的各项系数
  82.     a = M+m;
  83.     b = m*l*cos(theta);
  84.     c = -m*l*dtheta^2*sin(theta);
  85.     d = Fext+m*g*sin(theta);
  86.    
  87.     % 解方程得到加速度
  88.     ddtheta = (d*a-b*dtheta^2*cos(theta))/(-b^2+a*c);
  89.     ddx = (d-b*ddtheta*cos(theta))/a;
  90. end
复制代码


运行上述代码后将会绘制出模拟结果。代码中,我们首先定义了系统参数和初始状态,并设置了仿真时间和时间步长。然后,我们使用欧拉-前向差分法对微分方程进行数值解,并绘制出小车和摆杆的运动轨迹(在第三个子图中)。

希望这能帮助到您!
如果问题已经解决,请设置最佳答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 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-4-22 22:51

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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