網(wǎng)站首頁 編程語言 正文
1.雙擺問題
所謂雙擺,就是兩個(gè)連在一起的擺。
接下來本來是要推公式的,考慮考慮到大家可能會(huì)有公式恐懼癥,同時(shí)又喜歡看圖,所以把公式挪到后面。
所以,只需知道角速度的微分方程,就可寫出對應(yīng)的代碼,其方程如下:
從而轉(zhuǎn)為代碼得到:
# 其中,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
接下來根據(jù)微分方程的解,便可進(jìn)行繪圖。
# 這段代碼用于設(shè)置初值,并調(diào)用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 ? ? ?#質(zhì)量比M2/M1 M = 1+mu # 生成時(shí)間 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]) # 微分方程組數(shù)值解 y = integrate.odeint(derivs, state, t) # 真實(shí)坐標(biāo) x1 = L1*sin(y[:, 0]) y1 = -L1*cos(y[:, 0]) x2 = L2*sin(y[:, 2]) + x1 y2 = -L2*cos(y[:, 2]) + y1
至此,就得到了所有位置處的坐標(biāo),從而可以觀察到雙擺的軌跡如圖所示
繪圖代碼為:
import matplotlib.pyplot as plt plt.scatter(x1,y1,marker='.') plt.scatter(x2,y2,marker='.') plt.show()
若將時(shí)間設(shè)置得長一點(diǎn),然后在畫圖的時(shí)候更改一下顏色,就會(huì)看到雙擺的運(yùn)動(dòng)區(qū)間,可見自然界還是挺有情懷的
其繪圖代碼為:
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()
當(dāng)然,也可以將其運(yùn)動(dòng)軌跡以一種三維的形式繪制出來
ax = plt.gca(projection='3d') ax.plot3D(t,x1,y1,linewidth=1) plt.show()
額……好吧,看來并沒有什么情懷。
但是,如果把這兩個(gè)小球分別當(dāng)作兩個(gè)星球,而我們又在一顆星球上,那么所觀測到的另一顆星球的運(yùn)動(dòng)大致如下,不出意外是個(gè)圓,畢竟圓形二者之間的距離是恒定的。
繪圖代碼為:
ax = plt.gca(projection='3d') ax.plot3D(t,x2-x1,y2-y1,linewidth=0.5) plt.show()
如果更改一下初值,則圖形將有如下變化
初值設(shè)為:
th1,th2 = 0, 0 ?#初始角度 om1,om2 = 120.0, 108.00 ? ? ? #初始角速度
2.運(yùn)動(dòng)過程
最后,還是傳統(tǒng)技能,繪制一下雙擺的運(yùn)動(dòng)過程如下:
代碼為:
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.公式推導(dǎo)過程
雙擺的動(dòng)能和勢能分別為:
根據(jù)拉格朗日方程
則有:
其中,
展開可得則:
原文鏈接:https://blog.csdn.net/m0_37816922/article/details/123850164
相關(guān)推薦
- 2022-10-05 Android開發(fā)Activity毛玻璃背景效果_Android
- 2023-12-15 Linux系統(tǒng)——退出vi編輯模式
- 2024-04-08 啟動(dòng)spring-boot出現(xiàn)Error creating bean with name ‘conf
- 2022-12-05 C++語義copy?and?swap示例詳解_C 語言
- 2023-03-26 Go語言通過WaitGroup實(shí)現(xiàn)控制并發(fā)的示例詳解_Golang
- 2022-12-09 python返回多個(gè)值與賦值多個(gè)值的示例代碼_python
- 2022-07-16 idea 編寫springmvc項(xiàng)目并部署到Tomcat
- 2022-06-18 C#使用LOCK實(shí)現(xiàn)線程同步_C#教程
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支