網站首頁 編程語言 正文
1.雙擺問題
所謂雙擺,就是兩個連在一起的擺。
接下來本來是要推公式的,考慮考慮到大家可能會有公式恐懼癥,同時又喜歡看圖,所以把公式挪到后面。
所以,只需知道角速度的微分方程,就可寫出對應的代碼,其方程如下:
從而轉為代碼得到:
# 其中,lam,mu,G_L1,M為全局變量 def derivs(state, t): ? ? dydx = np.zeros_like(state) ? ? th1,om1,th2,om2 = state ? ? dydx[0] = state[1] ? ? delta = state[2] - state[0] ? ? cDelta, sDelta = np.cos(delta), np.sin(delta) ? ? sTh1,_,sTh2,_ = np.sin(state) ? ? den1 = M - mu*cDelta**2 ? ? dydx[1] = (mu * om1**2 * sDelta * cDelta ? ? ? ? ? ? ? ? + mu * G_L1 * sTh2 * cDelta ? ? ? ? ? ? ? ? + mu * lam * om2**2 * sDelta ? ? ? ? ? ? ? ? - M * G_L1 * sTh1)/ den1 ? ? dydx[2] = state[3] ? ? den2 = lam * den1 ? ? dydx[3] = (- mu * lam * om2**2 * sDelta * cDelta ? ? ? ? ? ? ? ? + M * G_L1 * sTh1 * cDelta ? ? ? ? ? ? ? ? - M * om1**2 * sDelta ? ? ? ? ? ? ? ? - M * G_L1 * sTh2)/ den2 ? ? return dydx
接下來根據微分方程的解,便可進行繪圖。
# 這段代碼用于設置初值,并調用integrate求解微分方程組 import numpy as np import scipy.integrate as integrate G = 9.8 L1,L2 = 1.0, 1.0 G_L1 = G/L1 lam = L2/L1 ? #桿長度比L2/L1 mu = 1.0 ? ? ?#質量比M2/M1 M = 1+mu # 生成時間 dt = 0.01 t = np.arange(0, 20, dt) th1,th2 = 120.0, -10.0 ?#初始角度 om1,om2 = 0.0, 0.00 ? ? ? #初始角速度 state = np.radians([th1, om1, th2, om2]) # 微分方程組數值解 y = integrate.odeint(derivs, state, t) # 真實坐標 x1 = L1*sin(y[:, 0]) y1 = -L1*cos(y[:, 0]) x2 = L2*sin(y[:, 2]) + x1 y2 = -L2*cos(y[:, 2]) + y1
至此,就得到了所有位置處的坐標,從而可以觀察到雙擺的軌跡如圖所示
繪圖代碼為:
import matplotlib.pyplot as plt plt.scatter(x1,y1,marker='.') plt.scatter(x2,y2,marker='.') plt.show()
若將時間設置得長一點,然后在畫圖的時候更改一下顏色,就會看到雙擺的運動區間,可見自然界還是挺有情懷的
其繪圖代碼為:
plt.plot(x1,y1,marker='.',alpha=0.2, linewidth=0.2) plt.plot(x2,y2,marker='.',alpha=0.2, linewidth=2, c='r') plt.axis('off') plt.show()
當然,也可以將其運動軌跡以一種三維的形式繪制出來
ax = plt.gca(projection='3d') ax.plot3D(t,x1,y1,linewidth=1) plt.show()
額……好吧,看來并沒有什么情懷。
但是,如果把這兩個小球分別當作兩個星球,而我們又在一顆星球上,那么所觀測到的另一顆星球的運動大致如下,不出意外是個圓,畢竟圓形二者之間的距離是恒定的。
繪圖代碼為:
ax = plt.gca(projection='3d') ax.plot3D(t,x2-x1,y2-y1,linewidth=0.5) plt.show()
如果更改一下初值,則圖形將有如下變化
初值設為:
th1,th2 = 0, 0 ?#初始角度 om1,om2 = 120.0, 108.00 ? ? ? #初始角速度
2.運動過程
最后,還是傳統技能,繪制一下雙擺的運動過程如下:
代碼為:
import matplotlib.animation as animation # 下面為繪圖過程 fig = plt.figure(figsize=(12,12)) ax = fig.add_subplot(111, autoscale_on=False, xlim=(-2, 2), ylim=(-2, 2)) ax.set_aspect('equal') ax.grid() line, = ax.plot([], [], 'o-', lw=2) time_template = 'time = %.1fs' time_text = ax.text(0.05, 0.9, '', transform=ax.transAxes) # 初始化圖形 def init(): ? ? line.set_data([], []) ? ? time_text.set_text('') ? ? return line, time_text def animate(i): ? ? thisx = [0, x1[i], x2[i]] ? ? thisy = [0, y1[i], y2[i]] ? ? line.set_data(thisx, thisy) ? ? time_text.set_text(time_template % (i*dt)) ? ? return line, time_text ani = animation.FuncAnimation(fig, animate, range(1, len(y)), ?? ? ? ? ? interval=dt*1000, blit=True, init_func=init) ani.save("dua_1.gif",writer='imagemagick') plt.show()
3.公式推導過程
雙擺的動能和勢能分別為:
根據拉格朗日方程
則有:
其中,
展開可得則:
原文鏈接:https://blog.csdn.net/m0_37816922/article/details/123850164
相關推薦
- 2022-11-13 Python?fileinput模塊應用詳解_python
- 2022-03-17 正確使用dotnet-*工具的方法_實用技巧
- 2023-10-16 nginx啟動與配置
- 2022-03-06 C語言之快速排序算法(遞歸Hoare版)介紹_C 語言
- 2022-03-31 聊聊Python?String型列表求最值的問題_python
- 2022-08-20 Python中range()與np.arange()的具體使用_python
- 2022-05-01 使用SQL實現車流量的計算的示例代碼_MsSql
- 2023-01-08 基于C#實現屏幕取色器的示例詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支