鱼C论坛

 找回密码
 立即注册
查看: 894|回复: 3

[已解决]添加完善代码,解决图中问题

[复制链接]
发表于 2023-9-26 15:08:37 | 显示全部楼层 |阅读模式
1鱼币

                               
登录/注册后可看大图


为解决图中问题:描述小车与杆的运动状态。

提供以下代码

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

  5. mpl.rcParams['font.sans-serif'] = ['SimHei']  # 显示中文
  6. mpl.rcParams['axes.unicode_minus'] = False  # 显示负号

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

  9. # 定义系统参数
  10. m1 = 1.0
  11. m = 2.0
  12. l = 1.0
  13. g = 10
  14. F = 1.0
  15. Dx = 0.0
  16. Dtheta = 0.0

  17. # 定义系统微分方程
  18. def deriv(y, t):
  19.     x,v,theta,w,x_ddot,theta_ddot = y
  20.     theta_dot = w
  21.     x_dot = v
  22.     theta_ddot = ((m*g*l*np.sin(theta)-Dtheta*theta_dot)/(m*l*np.cos(theta)) - (F-m*l*(theta_dot**2)*np.sin(theta)-Dx*x_dot)/(m1+m)) / (4*l/(3*np.cos(theta))-m*l*np.cos(theta)/(m1+m))
  23.     x_ddot = (F - Dx*x_dot + m*l*(theta_dot**2)*np.sin(theta) - m*l*theta_ddot*np.cos(theta))/(m+m1)

  24.     return [x_dot,x_ddot,theta_dot,theta_ddot,x,theta]

  25. # 定义初始状态
  26. y0 = [0.0, 0.0, np.pi/2, 0.0, F/m1, 0.0]

  27. # 解决微分方程
  28. sol = odeint(deriv, y0, t)  # func为待求解函数;y0为初值;t为自变量列表

  29. fig,axes = plt.subplots(1,2, figsize=(12, 6))  # 添加画布

  30. axes_1 = plt.subplot(131)
  31. line_1, = axes_1.plot(t, sol[:,0], label='小车速度',color='red')
  32. axes_1.set(title='F=1时,小车运动状态',xlabel='时间 (s)',ylabel='速度/V')
  33. axes_1_2 = axes_1.twinx()
  34. line_2, = axes_1_2.plot(t, (sol[:,1])-F/(m1+m)*(t-1), label='小车加速度',color='green')
  35. axes_1_2.set(ylabel='加速度/a')
  36. plt.grid()  # 显示网格
  37. axes_1.legend(handles=[line_1, line_2])

  38. axes_2 = plt.subplot(132)
  39. line_3, = axes_2.plot(t, sol[:,2], label='杆角速度',color='red')
  40. axes_2.set(title='F=1时,杆运动状态',xlabel='时间 (s)',ylabel='角速度/w')
  41. axes_2_2 = axes_2.twinx()
  42. line_4, = axes_2_2.plot(t, sol[:,3], label='杆角加速度',color='green')
  43. axes_2_2.set(ylabel='角加速度/dwdt')
  44. plt.grid()  # 显示网格
  45. axes_2.legend(handles=[line_3, line_4])

  46. axes_3 = plt.subplot(133)
  47. line_5, = axes_3.plot(t,sol[:,5], label='杆角度变化',color='red')
  48. axes_3.set(title='F=1时,杆与车运动状态',xlabel='时间 (s)',ylabel='角度')
  49. axes_3_2 = axes_3.twinx()
  50. line_6, = axes_3_2.plot(t, sol[:,4], label='小车位移',color='green')
  51. axes_3_2.set(ylabel='位移/m')
  52. plt.grid()  # 显示网格
  53. axes_3.legend(handles=[line_5, line_6])

  54. fig.tight_layout()  #自动调整布局,使标题不干涉
  55. plt.show()
复制代码


代码中定义函数deriv()输出小车速度x_dot,杆角速度theta_dot,为标量只有大小,没有方向,修改代码使输出能正确反应小车与杆的运动,小车向两侧运动时速度分别为正或负。

并说明输出各值的物理单位,比如速度为米每秒
最佳答案
2023-9-26 15:08:38
将函数deriv()中的x_dot和theta_dot修改为以下代码即可使输出能正确反应小车与杆的运动,小车向左运动时速度为负,向右运动时速度为正,杆逆时针旋转时角速度为正,顺时针旋转时角速度为负。

  1. def deriv(y, t):
  2.     x,v,theta,w,x_ddot,theta_ddot = y
  3.     theta_dot = w
  4.     x_dot = v
  5.     theta_ddot = ((m*g*l*np.sin(theta)-Dtheta*theta_dot)/(m*l*np.cos(theta)) - (F-m*l*(theta_dot**2)*np.sin(theta)-Dx*x_dot)/(m1+m)) / (4*l/(3*np.cos(theta))-m*l*np.cos(theta)/(m1+m))
  6.     x_ddot = (F - Dx*x_dot + m*l*(theta_dot**2)*np.sin(theta) - m*l*theta_ddot*np.cos(theta))/(m+m1)

  7.     # 小车向左运动时速度为负,向右运动时速度为正
  8.     if x_dot < 0:
  9.         x_dot = -x_dot
  10.         theta_dot = -theta_dot

  11.     return [x_dot,x_ddot,theta_dot,theta_ddot,x,theta]
复制代码


输出各值的物理单位:

· 速度:米每秒(m/s)
· 加速度:米每秒平方(m/s^2)
· 角速度:弧度每秒(rad/s)
· 角加速度:弧度每秒平方(rad/s^2)
· 位移:米(m)
· 角度:弧度(rad)

最佳答案

查看完整内容

将函数deriv()中的x_dot和theta_dot修改为以下代码即可使输出能正确反应小车与杆的运动,小车向左运动时速度为负,向右运动时速度为正,杆逆时针旋转时角速度为正,顺时针旋转时角速度为负。 输出各值的物理单位: · 速度:米每秒(m/s) · 加速度:米每秒平方(m/s^2) · 角速度:弧度每秒(rad/s) · 角加速度:弧度每秒平方(rad/s^2) · 位移:米(m) · 角度:弧度(rad)

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-9-26 15:08:38 | 显示全部楼层    本楼为最佳答案   
将函数deriv()中的x_dot和theta_dot修改为以下代码即可使输出能正确反应小车与杆的运动,小车向左运动时速度为负,向右运动时速度为正,杆逆时针旋转时角速度为正,顺时针旋转时角速度为负。

  1. def deriv(y, t):
  2.     x,v,theta,w,x_ddot,theta_ddot = y
  3.     theta_dot = w
  4.     x_dot = v
  5.     theta_ddot = ((m*g*l*np.sin(theta)-Dtheta*theta_dot)/(m*l*np.cos(theta)) - (F-m*l*(theta_dot**2)*np.sin(theta)-Dx*x_dot)/(m1+m)) / (4*l/(3*np.cos(theta))-m*l*np.cos(theta)/(m1+m))
  6.     x_ddot = (F - Dx*x_dot + m*l*(theta_dot**2)*np.sin(theta) - m*l*theta_ddot*np.cos(theta))/(m+m1)

  7.     # 小车向左运动时速度为负,向右运动时速度为正
  8.     if x_dot < 0:
  9.         x_dot = -x_dot
  10.         theta_dot = -theta_dot

  11.     return [x_dot,x_ddot,theta_dot,theta_ddot,x,theta]
复制代码


输出各值的物理单位:

· 速度:米每秒(m/s)
· 加速度:米每秒平方(m/s^2)
· 角速度:弧度每秒(rad/s)
· 角加速度:弧度每秒平方(rad/s^2)
· 位移:米(m)
· 角度:弧度(rad)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-1 16:12:59 | 显示全部楼层
看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-1 16:13:20 | 显示全部楼层
isdkz 发表于 2023-9-26 15:08
将函数deriv()中的x_dot和theta_dot修改为以下代码即可使输出能正确反应小车与杆的运动,小车向左运动时速 ...

可以解释一下吗?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-2 07:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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