利用matlab进行混沌摆仿真(双摆、三摆、多摆演示)

本文介绍了如何利用matlab通过拉格朗日方法建模混沌摆,包括双摆、三摆的数学模型和数值求解。通过非线性动力学分析,展示了混沌现象在多摆系统中的表现,强调了初始条件的敏感性。作者提供了用4阶龙格库塔方法实现的双摆和三摆的数值求解代码。
摘要由CSDN通过智能技术生成

本文首发于 matlab爱好者 微信公众号,欢迎关注。

惯例声明:本人没有相关的工程应用经验,只是纯粹对相关算法感兴趣才写此博客。所以如果有错误,欢迎在评论区指正,不胜感激。本文主要关注于算法的实现,对于实际应用等问题本人没有任何经验,所以也不再涉及。

本文为学习非线性动力学时,搜索资料时发现的一个小例子,感觉很有趣,试着自己尝试一下。本来想用计算多体动力学的方法求解,但时间不足,智商有限,没有学会。只能用丑陋的公式法,直接进行求解,实属遗憾。在此挖个小坑,日后有机会参悟出计算法后,必定在这里更新。

0 前言

非线性线性存在于物理与工程中的各个领域。在机械中,存在着大量的非线性现象。这里通过双摆和三摆的例子,来展示一个混沌现象。

什么是混沌呢,用俗话说,差别很小的输入,输入进混沌系统后,过一段时间总能将两者区分开。再换句话说,混沌系统对误差非常敏感,随着时间的推移,计算结果的可信度将会越来越低。反正现在科普文章还是很多的,有的文章把各种玄学都搬出来了。但系统就在那里,大家盲人摸象,不同的人会摸到不同的地方,产生自己的感悟。我这里也不啰嗦搞这一套了。

好了,不啰嗦了前言了。(话说这么啰嗦的前言会不会直接劝退读者 (っ°Д°;)っ )

本文的混沌摆模型都是二维平面内的运动的模型,也就是平面铰链连接的机构,不涉及三维运动以及球铰连接。本文的混沌摆没有考虑摩擦力。

1 拉格朗日方法建模

这里假设摆的长度和质量都一样。当然,不一样的话,也可以求,就是公式复杂一些,方法一样。

1.1 双摆模型

请添加图片描述
上图是一个随手画的双摆模型,由两个小棍组成。上面小棍顶端点被限制,小面小棍顶端点与上面小棍相连。现在只需要用两个变量就可以描述这个双摆模型的状态,一个是蓝色摆与垂线的夹角θ1,一个是黄色摆与垂线的夹角θ2。这两个摆的角速度,可以用 d θ 1 d t \frac{dθ1}{dt} dtdθ1 d θ 2 d t \frac{dθ2}{dt} dtdθ2来表示(或者简写为 θ 1 ˙ \dot{θ_1} θ1˙ θ 2 ˙ \dot{θ_2} θ2˙)。

之后根据这两个信息,我们就可以愉快的列方程了:

首先,每个小棍对质心求转动惯量:
I = 1 12 m l 2 I=\frac{1}{12} m l^2 I=121ml2
小棍1的质心位置为:
x 1 = l 2 sin ⁡ θ 1 y 1 = − l 2 cos ⁡ θ 1 x_1=\frac{l}{2} \sin{\theta_1} \\ y_1=-\frac{l}{2} \cos{\theta_1} x1=2lsinθ1y1=2lcosθ1
小棍2的质心位置比较复杂,为:
x 2 = l ( sin ⁡ θ 1 + 1 2 sin ⁡ θ 2 ) y 2 = − l ( cos ⁡ θ 1 + 1 2 cos ⁡ θ 2 ) x_2=l(\sin{\theta_1}+\frac{1}{2} \sin{\theta_2}) \\ y_2=-l(\cos{\theta_1}+\frac{1}{2} \cos{\theta_2}) x2=l(sinθ1+21sinθ2)y2=l(cosθ1+21cosθ2)
可以看做是小棍1的末端坐标,再加上小棍2的偏移量叠加得到的。
知道了质心坐标后,我们就可以求出质心的运动速度:
小棍1的质心速度为:
d x 1 = l 2 cos ⁡ θ 1 ⋅ θ 1 ˙ d y 1 = l 2 sin ⁡ θ 1 ⋅ θ 1 ˙ dx_1=\frac{l}{2} \cos{\theta_1}\cdot \dot{\theta_1}\\ dy_1=\frac{l}{2} \sin{\theta_1}\cdot \dot{\theta_1} dx1=2lcosθ1θ1˙dy1=2lsinθ1θ1˙
然后小棍2的质心速度为:
d x 2 = l ( cos ⁡ θ 1 ⋅ θ 1 ˙ + 1 / 2 ⋅ cos ⁡ θ 2 ⋅ θ 2 ˙ ) d x 2 = − l ( sin ⁡ θ 1 ⋅ θ 1 ˙ + 1 / 2 ⋅ sin ⁡ θ 2 ⋅ θ 2 ˙ ) dx_2=l ( \cos{\theta_1}\cdot \dot{\theta_1}+1/2\cdot \cos{\theta_2}\cdot\dot{\theta_2})\\ dx_2=-l ( \sin{\theta_1}\cdot \dot{\theta_1}+1/2\cdot \sin{\theta_2}\cdot\dot{\theta_2}) dx2=l(cosθ1θ1˙+1/2cosθ2θ2˙)dx2=l(sinθ1θ1˙+1/2sinθ2θ2˙)
然后求拉格朗日量(详情见任意理论力学课本):
L = T 平动 + T 转动 − U 势能 = 1 2 m ( d x 1 2 + d y 1 2 ) + 1 2 m ( d x 2 2 + d y 2 2 ) + 1 2 I θ 1 ˙ 2 + 1 2 I θ 2 ˙ 2 − m g ( y 1 + y 2 ) L=T_{平动}+T_{转动}-U_{势能} \\ =\frac{1}{2}m(dx_1^2+dy_1^2)+\frac{1}{2}m(dx_2^2+dy_2^2)+\frac{1}{2} I \dot{\theta_1}^2+ \frac{1}{2} I \dot{\theta_2}^2 - mg(y_1+y_2) L=T平动+T转动U势能=21m(dx12+dy12)+21m(dx22+dy22)+21Iθ1˙2+21Iθ2˙2mg(y1+y2)

然后我们需要求解出角度与角动量p相关的方程组,方便后续求解。首先是角度变化方程,这个需要方程求解一下:
p 1 = ∂ L ∂ θ 1 ˙ p 2 = ∂ L ∂ θ 2 ˙ p_1=\frac{\partial L}{\partial \dot{\theta_1}} \\ p_2=\frac{\partial L}{\partial \dot{\theta_2}} p1=θ1˙Lp2=θ2˙L
得到:
θ 1 ˙ = 6 ( 2 p 1 − 3 p 2 cos ⁡ ( θ 1 − θ 2 ) ) m l 2 ( 9 cos ⁡ 2 ( θ 1 − θ 2 ) − 16 ) θ 2 ˙ = 6 ( 8 p 2 − 3 p 1 cos ⁡ ( θ 1 − θ 2 ) ) m l 2 ( 9 cos ⁡ 2 ( θ 1 − θ 2 ) − 16 ) \dot{\theta_1}= \frac{6 (2 p_1 - 3 p_2 \cos (\theta_1-\theta_2))}{ml^2 \left(9 \cos ^2(\theta_1-\theta_2)-16\right)} \\ \dot{\theta_2}= \frac{6 (8 p_2 - 3 p_1 \cos (\theta_1-\theta_2))}{ml^2 \left(9 \cos ^2(\theta_1-\theta_2)-16\right)} θ1˙=ml2(9cos2(θ1θ2)16)6(2p13p2cos(θ1θ2))θ2˙=ml2(9cos2(θ1θ2)16)6(8p23p1cos(θ1θ2))
角动量变化方程比较简单,不需要联立求解:
p 1 ˙ = ∂ L ∂ θ 1 p 2 ˙ = ∂ L ∂ θ 2 \dot{p_1}=\frac{\partial L}{\partial \theta_1} \\ \dot{p_2}=\frac{\partial L}{\partial \theta_2} p1˙=θ1Lp2˙=θ2L
得到:
p 1 ˙ = 1 2 m l ( 3 g sin ⁡ θ 1 + θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) ) p 2 ˙ = 1 2 m l ( − g sin ⁡ θ 2 + θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) ) \dot{p_1}=\frac{1}{2} ml (3g\sin{\theta_1} + \dot{\theta_1}\dot{\theta_2}l \sin( \theta_1-\theta_2 ) ) \\ \dot{p_2}=\frac{1}{2} ml ( -g\sin{\theta_2} + \dot{\theta_1}\dot{\theta_2}l \sin( \theta_1-\theta_2 ) ) p1˙=21ml(3gsinθ1+θ1˙θ2˙lsin(θ1θ2))p2˙=21ml(gsinθ2+θ1˙θ2˙lsin(θ1θ2))
然后就是用龙格库塔方法,求解下面的方程组就行了:
{ θ 1 ˙ = 6 ( 2 p 1 − 3 p 2 cos ⁡ ( θ 1 − θ 2 ) ) m l 2 ( 9 cos ⁡ 2 ( θ 1 − θ 2 ) − 16 ) θ 2 ˙ = 6 ( 8 p 2 − 3 p 1 cos ⁡ ( θ 1 − θ 2 ) ) m l 2 ( 9 cos ⁡ 2 ( θ 1 − θ 2 ) − 16 ) p 1 ˙ = 1 2 m l ( 3 g sin ⁡ θ 1 + θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) ) p 2 ˙ = 1 2 m l ( − g sin ⁡ θ 2 + θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) ) \left\{\begin{matrix} \dot{\theta_1}= \frac{6 (2 p_1 - 3 p_2 \cos (\theta_1-\theta_2))}{ml^2 \left(9 \cos ^2(\theta_1-\theta_2)-16\right)} \\ \\ \dot{\theta_2}= \frac{6 (8 p_2 - 3 p_1 \cos (\theta_1-\theta_2))}{ml^2 \left(9 \cos ^2(\theta_1-\theta_2)-16\right)} \\ \\ \dot{p_1}=\frac{1}{2} ml (3g\sin{\theta_1} + \dot{\theta_1}\dot{\theta_2}l \sin( \theta_1-\theta_2 ) ) \\ \\ \dot{p_2}=\frac{1}{2} ml ( -g\sin{\theta_2} + \dot{\theta_1}\dot{\theta_2}l \sin( \theta_1-\theta_2 ) ) \end{matrix}\right. θ1˙=ml2(9cos2(θ1θ2)16)6(2p13p2cos(θ1θ2))θ2˙=ml2(9cos2(θ1θ2)16)6(8p23p1cos(θ1θ2))p1˙=21ml(3gsinθ1+θ1˙θ2˙lsin(θ1θ2))p2˙=21ml(gsinθ2+θ1˙θ2˙lsin(θ1θ2))
当然以上过程,全部可以用matlab的符号求解功能,或者用mathematica进行求解,而且事实上强烈建议用软件进行求解,以免造成手算导致的失误。

1.2 三摆以及多摆模型

知道了双摆模型,三摆模型或者多摆模型就可以迎刃而解了。

如果采用程序法计算的话:
不同小棍质心点的坐标,用循环的方式可以有规律的生成。拉格朗日量也可以用循环的方式去构建。
之后求偏导以及解方程,也都可以自动化完成,非常的方便。

不过这里还是给出三摆的运动方程,方便各位读者校对:
各个小棍重心的位置方程如下,小棍1和小棍2同双摆:
x 1 = l 2 sin ⁡ θ 1 y 1 = − l 2 cos ⁡ θ 1 x 2 = l ( sin ⁡ θ 1 + 1 2 sin ⁡ θ 2 ) y 2 = − l ( cos ⁡ θ 1 + 1 2 cos ⁡ θ 2 ) x 3 = l ( sin ⁡ θ 1 + sin ⁡ θ 2 + 1 2 sin ⁡ θ 3 ) y 3 = − l ( cos ⁡ θ 1 + cos ⁡ θ 2 + 1 2 cos ⁡ θ 3 ) x_1=\frac{l}{2} \sin{\theta_1} \\ y_1=-\frac{l}{2} \cos{\theta_1} \\ x_2=l(\sin{\theta_1}+\frac{1}{2} \sin{\theta_2}) \\ y_2=-l(\cos{\theta_1}+\frac{1}{2} \cos{\theta_2}) \\ x_3=l(\sin{\theta_1}+\sin{\theta_2}+\frac{1}{2} \sin{\theta_3}) \\ y_3=-l(\cos{\theta_1}+\cos{\theta_2}+\frac{1}{2} \cos{\theta_3}) x1=2lsinθ1y1=2lcosθ1x2=l(sinθ1+21sinθ2)y2=l(cosθ1+21cosθ2)x3=l(sinθ1+sinθ2+21sinθ3)y3=l(cosθ1+cosθ2+21cosθ3)
小棍重心的变化为:
d x 1 = l 2 cos ⁡ θ 1 ⋅ θ 1 ˙ d y 1 = l 2 sin ⁡ θ 1 ⋅ θ 1 ˙ d x 2 = l ( cos ⁡ θ 1 ⋅ θ 1 ˙ + 1 / 2 ⋅ cos ⁡ θ 2 ⋅ θ 2 ˙ ) d x 2 = − l ( sin ⁡ θ 1 ⋅ θ 1 ˙ + 1 / 2 ⋅ sin ⁡ θ 2 ⋅ θ 2 ˙ ) d x 3 = l ( cos ⁡ θ 1 ⋅ θ 1 ˙ + cos ⁡ θ 2 ⋅ θ 2 ˙ + 1 / 2 ⋅ cos ⁡ θ 3 ⋅ θ 3 ˙ ) d x 3 = − l ( sin ⁡ θ 1 ⋅ θ 1 ˙ + sin ⁡ θ 2 ⋅ θ 2 ˙ + 1 / 2 ⋅ sin ⁡ θ 3 ⋅ θ 3 ˙ ) dx_1=\frac{l}{2} \cos{\theta_1}\cdot \dot{\theta_1}\\ dy_1=\frac{l}{2} \sin{\theta_1}\cdot \dot{\theta_1} \\ dx_2=l ( \cos{\theta_1}\cdot \dot{\theta_1}+1/2\cdot \cos{\theta_2}\cdot\dot{\theta_2})\\ dx_2=-l ( \sin{\theta_1}\cdot \dot{\theta_1}+1/2\cdot \sin{\theta_2}\cdot\dot{\theta_2}) \\ dx_3=l ( \cos{\theta_1}\cdot \dot{\theta_1}+\cos{\theta_2}\cdot \dot{\theta_2}+1/2\cdot \cos{\theta_3}\cdot\dot{\theta_3})\\ dx_3=-l ( \sin{\theta_1}\cdot \dot{\theta_1}+\sin{\theta_2}\cdot \dot{\theta_2}+1/2\cdot \sin{\theta_3}\cdot\dot{\theta_3}) \\ dx1=2lcosθ1θ1˙dy1=2lsinθ1θ1˙dx2=l(cosθ1θ1˙+1/2cosθ2θ2˙)dx2=l(sinθ1θ1˙+1/2sinθ2θ2˙)dx3=l(cosθ1θ1˙+cosθ2θ2˙+1/2cosθ3θ3˙)dx3=l(sinθ1θ1˙+sinθ2θ2˙+1/2sinθ3θ3˙)
这个三摆系统的拉格朗日量L为:
L = 1 2 m ( d x 1 2 + d y 1 2 + d x 2 2 + d y 2 2 + d x 3 2 + d y 3 2 ) + 1 2 I ( θ 1 ˙ 2 + θ 2 ˙ 2 + θ 3 ˙ 2 ) − m g ( y 1 + y 2 + y 3 ) = 1 6 m l ( 9 θ 2 ˙ θ 1 ˙ l cos ⁡ ( θ 1 − θ 2 ) + 3 θ 3 ˙ θ 1 ˙ l cos ⁡ ( θ 1 − θ 3 ) + 3 θ 2 ˙ θ 3 ˙ l cos ⁡ ( θ 2 − θ 3 ) + 7 θ 1 ˙ 2 l + 4 θ 2 ˙ 2 l + θ 3 ˙ 2 l + 15 g cos ⁡ ( θ 1 ) + 9 g cos ⁡ ( θ 2 ) + 3 g cos ⁡ ( θ 3 ) ) L=\frac{1}{2}m(dx_1^2+dy_1^2+dx_2^2+dy_2^2+dx_3^2+dy_3^2)+\frac{1}{2} I (\dot{\theta_1}^2+\dot{\theta_2}^2+\dot{\theta_3}^2) - mg(y_1+y_2+y_3)\\ =\frac{1}{6} ml \left(9 \dot{\theta_2} \dot{\theta_1} l \cos (\theta_1-\theta_2)+3 \dot{\theta_3} \dot{\theta_1} l \cos (\theta_1-\theta_3)+3 \dot{\theta_2} \dot{\theta_3} l \cos (\theta_2-\theta_3) +7 \dot{\theta_1}^2 l+4 \dot{\theta_2}^2 l+\dot{\theta_3}^2 l +15 g \cos (\theta_1)+9 g \cos (\theta_2)+3 g \cos (\theta_3)\right) L=21m(dx12+dy12+dx22+dy22+dx32+dy32)+21I(θ1˙2+θ2˙2+θ3˙2)mg(y1+y2+y3)=61ml(9θ2˙θ1˙lcos(θ1θ2)+3θ3˙θ1˙lcos(θ1θ3)+3θ2˙θ3˙lcos(θ2θ3)+7θ1˙2l+4θ2˙2l+θ3˙2l+15gcos(θ1)+9gcos(θ2)+3gcos(θ3))
同样,我们联立求解出角度的变化
p 1 = ∂ L ∂ θ 1 ˙ p 2 = ∂ L ∂ θ 2 ˙ p 3 = ∂ L ∂ θ 3 ˙ p_1=\frac{\partial L}{\partial \dot{\theta_1}} \\ p_2=\frac{\partial L}{\partial \dot{\theta_2}} \\ p_3=\frac{\partial L}{\partial \dot{\theta_3}} p1=θ1˙Lp2=θ2˙Lp3=θ3˙L
得到
θ 1 ˙ = 6 ( 9 p 1 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) + 27 p 2 cos ⁡ ( θ 1 − θ 2 ) − 9 p 2 cos ⁡ ( θ 1 + θ 2 − 2 θ 3 ) + 21 p 3 cos ⁡ ( θ 1 − θ 3 ) − 27 p 3 cos ⁡ ( θ 1 − 2 θ 2 + θ 3 ) − 23 p 1 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) θ 2 ˙ = 6 ( 27 p 1 cos ⁡ ( θ 1 − θ 2 ) − 9 p 1 cos ⁡ ( θ 1 + θ 2 − 2 θ 3 ) + 9 p 2 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) − 27 p 3 cos ⁡ ( 2 θ 1 − θ 2 − θ 3 ) + 57 p 3 cos ⁡ ( θ 2 − θ 3 ) − 47 p 2 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) θ 3 ˙ = 6 ( 21 p 1 cos ⁡ ( θ 1 − θ 3 ) − 27 p 1 cos ⁡ ( θ 1 − 2 θ 2 + θ 3 ) − 27 p 2 cos ⁡ ( 2 θ 1 − θ 2 − θ 3 ) + 57 p 2 cos ⁡ ( θ 2 − θ 3 ) + 81 p 3 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 143 p 3 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) \dot{\theta_1}= \frac{6 (9 p_1 \cos (2 (\theta_2-\theta_3))+27 p_2 \cos (\theta_1-\theta_2)-9 p_2 \cos (\theta_1+\theta_2-2 \theta_3)+21 p_3 \cos (\theta_1-\theta_3)-27 p_3 \cos (\theta_1-2 \theta_2+\theta_3)-23 p_1)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} \\ \dot{\theta_2}= \frac{6 (27 p_1 \cos (\theta_1-\theta_2)-9 p_1 \cos (\theta_1+\theta_2-2 \theta_3)+9 p_2 \cos (2 (\theta_1-\theta_3))-27 p_3 \cos (2 \theta_1-\theta_2-\theta_3)+57 p_3 \cos (\theta_2-\theta_3)-47 p_2)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} \\ \dot{\theta_3}= \frac{6 (21 p_1 \cos (\theta_1-\theta_3)-27 p_1 \cos (\theta_1-2 \theta_2+\theta_3)-27 p_2 \cos (2 \theta_1-\theta_2-\theta_3)+57 p_2 \cos (\theta_2-\theta_3)+81 p_3 \cos (2 (\theta_1-\theta_2))-143 p_3)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} θ1˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(9p1cos(2(θ2θ3))+27p2cos(θ1θ2)9p2cos(θ1+θ22θ3)+21p3cos(θ1θ3)27p3cos(θ12θ2+θ3)23p1)θ2˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(27p1cos(θ1θ2)9p1cos(θ1+θ22θ3)+9p2cos(2(θ1θ3))27p3cos(2θ1θ2θ3)+57p3cos(θ2θ3)47p2)θ3˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(21p1cos(θ1θ3)27p1cos(θ12θ2+θ3)27p2cos(2θ1θ2θ3)+57p2cos(θ2θ3)+81p3cos(2(θ1θ2))143p3)
另外三个角动量方程为:
p 1 ˙ = ∂ L ∂ θ 1 p 2 ˙ = ∂ L ∂ θ 2 p 3 ˙ = ∂ L ∂ θ 3 \dot{p_1}=\frac{\partial L}{\partial \theta_1} \\ \dot{p_2}=\frac{\partial L}{\partial \theta_2} \\ \dot{p_3}=\frac{\partial L}{\partial \theta_3} p1˙=θ1Lp2˙=θ2Lp3˙=θ3L
计算得到:
p 1 ˙ = − 1 2 m l ( 3 θ 2 ˙ θ 1 ˙ l sin ⁡ ( θ 1 − θ 2 ) + θ 1 ˙ θ 3 ˙ l sin ⁡ ( θ 1 − θ 3 ) + 5 g sin ⁡ ( θ 1 ) ) p 1 ˙ = − 1 2 m l ( − 3 θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) + θ 2 ˙ θ 3 ˙ l sin ⁡ ( θ 2 − θ 3 ) + 3 g sin ⁡ ( θ 2 ) ) p 1 ˙ = − 1 2 m l ( θ 1 ˙ θ 3 ˙ l sin ⁡ ( θ 1 − θ 3 ) + θ 2 ˙ θ 3 ˙ l sin ⁡ ( θ 2 − θ 3 ) − g sin ⁡ ( θ 3 ) ) \dot{p_1}= -\frac{1}{2} ml (3 \dot{\theta_2} \dot{\theta_1} l \sin (\theta_1-\theta_2)+\dot{\theta_1} \dot{\theta_3} l \sin (\theta_1-\theta_3)+5 g \sin (\theta_1))\\ \dot{p_1}= -\frac{1}{2} ml (-3 \dot{\theta_1} \dot{\theta_2} l \sin (\theta_1-\theta_2)+\dot{\theta_2} \dot{\theta_3} l \sin (\theta_2-\theta_3)+3 g \sin (\theta_2))\\ \dot{p_1}= -\frac{1}{2} ml (\dot{\theta_1} \dot{\theta_3} l \sin (\theta_1-\theta_3)+\dot{\theta_2} \dot{\theta_3} l \sin (\theta_2-\theta_3)-g \sin (\theta_3)) p1˙=21ml(3θ2˙θ1˙lsin(θ1θ2)+θ1˙θ3˙lsin(θ1θ3)+5gsin(θ1))p1˙=21ml(3θ1˙θ2˙lsin(θ1θ2)+θ2˙θ3˙lsin(θ2θ3)+3gsin(θ2))p1˙=21ml(θ1˙θ3˙lsin(θ1θ3)+θ2˙θ3˙lsin(θ2θ3)gsin(θ3))
因此,我们得到了下面这个6维一阶常微分方程组:
{ θ 1 ˙ = 6 ( 9 p 1 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) + 27 p 2 cos ⁡ ( θ 1 − θ 2 ) − 9 p 2 cos ⁡ ( θ 1 + θ 2 − 2 θ 3 ) + 21 p 3 cos ⁡ ( θ 1 − θ 3 ) − 27 p 3 cos ⁡ ( θ 1 − 2 θ 2 + θ 3 ) − 23 p 1 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) θ 2 ˙ = 6 ( 27 p 1 cos ⁡ ( θ 1 − θ 2 ) − 9 p 1 cos ⁡ ( θ 1 + θ 2 − 2 θ 3 ) + 9 p 2 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) − 27 p 3 cos ⁡ ( 2 θ 1 − θ 2 − θ 3 ) + 57 p 3 cos ⁡ ( θ 2 − θ 3 ) − 47 p 2 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) θ 3 ˙ = 6 ( 21 p 1 cos ⁡ ( θ 1 − θ 3 ) − 27 p 1 cos ⁡ ( θ 1 − 2 θ 2 + θ 3 ) − 27 p 2 cos ⁡ ( 2 θ 1 − θ 2 − θ 3 ) + 57 p 2 cos ⁡ ( θ 2 − θ 3 ) + 81 p 3 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 143 p 3 ) m l 2 ( 81 cos ⁡ ( 2 ( θ 1 − θ 2 ) ) − 9 cos ⁡ ( 2 ( θ 1 − θ 3 ) ) + 45 cos ⁡ ( 2 ( θ 2 − θ 3 ) ) − 169 ) p 1 ˙ = − 1 2 m l ( 3 θ 2 ˙ θ 1 ˙ l sin ⁡ ( θ 1 − θ 2 ) + θ 1 ˙ θ 3 ˙ l sin ⁡ ( θ 1 − θ 3 ) + 5 g sin ⁡ ( θ 1 ) ) p 1 ˙ = − 1 2 m l ( − 3 θ 1 ˙ θ 2 ˙ l sin ⁡ ( θ 1 − θ 2 ) + θ 2 ˙ θ 3 ˙ l sin ⁡ ( θ 2 − θ 3 ) + 3 g sin ⁡ ( θ 2 ) ) p 1 ˙ = − 1 2 m l ( θ 1 ˙ θ 3 ˙ l sin ⁡ ( θ 1 − θ 3 ) + θ 2 ˙ θ 3 ˙ l sin ⁡ ( θ 2 − θ 3 ) − g sin ⁡ ( θ 3 ) ) \left\{\begin{matrix} \dot{\theta_1}= \frac{6 (9 p_1 \cos (2 (\theta_2-\theta_3))+27 p_2 \cos (\theta_1-\theta_2)-9 p_2 \cos (\theta_1+\theta_2-2 \theta_3)+21 p_3 \cos (\theta_1-\theta_3)-27 p_3 \cos (\theta_1-2 \theta_2+\theta_3)-23 p_1)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} \\ \\ \dot{\theta_2}= \frac{6 (27 p_1 \cos (\theta_1-\theta_2)-9 p_1 \cos (\theta_1+\theta_2-2 \theta_3)+9 p_2 \cos (2 (\theta_1-\theta_3))-27 p_3 \cos (2 \theta_1-\theta_2-\theta_3)+57 p_3 \cos (\theta_2-\theta_3)-47 p_2)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} \\ \\ \dot{\theta_3}= \frac{6 (21 p_1 \cos (\theta_1-\theta_3)-27 p_1 \cos (\theta_1-2 \theta_2+\theta_3)-27 p_2 \cos (2 \theta_1-\theta_2-\theta_3)+57 p_2 \cos (\theta_2-\theta_3)+81 p_3 \cos (2 (\theta_1-\theta_2))-143 p_3)} {ml^2 (81 \cos (2 (\theta_1-\theta_2))-9 \cos (2 (\theta_1-\theta_3))+45 \cos (2 (\theta_2-\theta_3))-169)} \\ \\ \dot{p_1}= -\frac{1}{2} ml (3 \dot{\theta_2} \dot{\theta_1} l \sin (\theta_1-\theta_2)+\dot{\theta_1} \dot{\theta_3} l \sin (\theta_1-\theta_3)+5 g \sin (\theta_1))\\ \\ \dot{p_1}= -\frac{1}{2} ml (-3 \dot{\theta_1} \dot{\theta_2} l \sin (\theta_1-\theta_2)+\dot{\theta_2} \dot{\theta_3} l \sin (\theta_2-\theta_3)+3 g \sin (\theta_2))\\ \\ \dot{p_1}= -\frac{1}{2} ml (\dot{\theta_1} \dot{\theta_3} l \sin (\theta_1-\theta_3)+\dot{\theta_2} \dot{\theta_3} l \sin (\theta_2-\theta_3)-g \sin (\theta_3)) \end{matrix}\right. θ1˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(9p1cos(2(θ2θ3))+27p2cos(θ1θ2)9p2cos(θ1+θ22θ3)+21p3cos(θ1θ3)27p3cos(θ12θ2+θ3)23p1)θ2˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(27p1cos(θ1θ2)9p1cos(θ1+θ22θ3)+9p2cos(2(θ1θ3))27p3cos(2θ1θ2θ3)+57p3cos(θ2θ3)47p2)θ3˙=ml2(81cos(2(θ1θ2))9cos(2(θ1θ3))+45cos(2(θ2θ3))169)6(21p1cos(θ1θ3)27p1cos(θ12θ2+θ3)27p2cos(2θ1θ2θ3)+57p2cos(θ2θ3)+81p3cos(2(θ1θ2))143p3)p1˙=21ml(3θ2˙θ1˙lsin(θ1θ2)+θ1˙θ3˙lsin(θ1θ3)+5gsin(θ1))p1˙=21ml(3θ1˙θ2˙lsin(θ1θ2)+θ2˙θ3˙lsin(θ2θ3)+3gsin(θ2))p1˙=21ml(θ1˙θ3˙lsin(θ1θ3)+θ2˙θ3˙lsin(θ2θ3)gsin(θ3))

我这里提供一个简单的mathematica小程序,用来计算这个多摆方程用的(感觉matlab的兼容性应该会更好,毕竟之后我是用matlab求解,但是我个人不太习惯matlab的符号求解):

下面是求解拉格朗日量的程序:

num = 3
II = 1/12*m*l^2;
Do[x[i] =
  Sum[l*Sin[th[k]], {k, 1, i}] - l/2*Sin[th[i]]
 , {i, 1, num}]
Do[y[i] =
  -Sum[l*Cos[th[k]], {k, 1, i}] + l/2*Cos[th[i]]
 , {i, 1, num}]
Do[dx[i] =
  Sum[l*Cos[th[k]]*dth[k], {k, 1, i}] - l/2*Cos[th[i]]*dth[i]
 , {i, 1, num}]
Do[dy[i] =
  Sum[l*Sin[th[k]]*dth[k], {k, 1, i}] - l/2*Sin[th[i]]*dth[i]
 , {i, 1, num}]
L = TrigReduce[
   1/2*m*(Sum[dx[i]^2, {i, 1, num}] + Sum[dy[i]^2, {i, 1, num}]) +
    1/2*II*(Sum[dth[i]^2, {i, 1, num}]) -
    m*g*(Sum[y[i], {i, 1, num}])];

然后求解三个角度

Solve[pth[1] == D[L, dth[1]] &&
  pth[2] == D[L, dth[2]] &&
  pth[3] == D[L, dth[3]], {dth[1], dth[2], dth[3]}]

最后计算三个动量

Do[dpth[i] = Simplify[D[L, th[i]]], {i, 1, num}]
dpth[1]
dpth[2]
dpth[3]

但是这个方程的复杂程度,感觉是和小棍的数量平方一个量级的(只是感觉,没有证明)。所以如果小棍的数量比较多,这种方法计算的方程量就会非常大。到那个时候,就不建议用理论计算出方程的方式去求解,建议用计算多体动力学之类的方式,进行数值求解。

2 多摆的数值求解

2.1 双摆

我这里没用ode45,用的是自己编的一个4阶龙格库塔方法进行求解的。

肉眼上看动图,看起来是没有什么问题的,运动连贯,混沌现象满足,能量、位置之类的也没有发散。
请添加图片描述

程序如下:

%双摆
clear
clc
close all
%输入
N=2;%双摆
m=1;
l=1;
g=9.8;
Input=[N,m,l,g];
%初始条件和时间设置
y0=[pi/2;pi/2;0;0];%这里全部是弧度值。分别代表[摆1与垂面夹角,摆2与垂面夹角,摆1角动量,摆2角动量]
h=1e-2;
x0=0:h:20;
%代入到ODE求解器中
[y1,Output]=ODE_RK4_hyh(x0,h,y0,Input);

%提取出角度
tN=size(y1,2);
th1=y1(1,:);
th2=y1(2,:);

%计算出关节坐标
CX1_A=zeros(1,tN);
CX1_B=CX1_A+l*sin(th1);
CY1_A=zeros(1,tN);
CY1_B=CY1_A-l*cos(th1);

CX2_A=CX1_B;
CX2_B=CX2_A+l*sin(th2);
CY2_A=CY1_B;
CY2_B=CY2_A-l*cos(th2);

%绘图
n=1;
figure()
set(gcf,'position',[488   342   400   300])
for k=1:4:length(x0) %这里4步一显示时间帧
    clf
    xlim([-2,2])
    ylim([-2,2])
    hold on
    %绘制摆
    plot([CX1_A(k),CX1_B(k)],[CY1_A(k),CY1_B(k)],'color','k','LineWidth',1.5)
    plot([CX2_A(k),CX2_B(k)],[CY2_A(k),CY2_B(k)],'color','k','LineWidth',1.5)
    %绘制轨线
    if k>200
        n=n+1;
    end
    Nm=k-n+1;
    %轨迹1
    F_color=[1,0,0];
    F_color=F_color*0.6+[1,1,1]*0.4*0.999;
    cdata=[linspace(1,F_color(1),Nm+1)',linspace(1,F_color(2),Nm+1)',linspace(1,F_color(3),Nm+1)'];
    cdata=reshape(cdata,Nm+1,1,3);
    if k>3
        patch([CX1_B(n:k),NaN],[CY1_B(n:k),NaN],1:Nm+1,'EdgeColor','interp','Marker','none',...
          'MarkerFaceColor','flat','CData',cdata,'LineWidth',1.5);
    end
    %轨迹2
    F_color=[0,0,1];
    F_color=F_color*0.6+[1,1,1]*0.4*0.999;
    cdata=[linspace(1,F_color(1),Nm+1)',linspace(1,F_color(2),Nm+1)',linspace(1,F_color(3),Nm+1)'];
    cdata=reshape(cdata,Nm+1,1,3);
    if k>3
        patch([CX2_B(n:k),NaN],[CY2_B(n:k),NaN],1:Nm+1,'EdgeColor','interp','Marker','none',...
          'MarkerFaceColor','flat','CData',cdata,'LineWidth',1.5);
    end
    hold off
    pause(0.05)
    %可以在这里添加输出动图的程序
end

function [y,Output]=ODE_RK4_hyh(x,h,y0,Input)
%4阶RK方法
%h间隔为常数的算法
y=zeros(size(y0,1),size(x,2));
y(:,1)=y0;
for ii=1:length(x)-1
    yn=y(:,ii);
    xn=x(ii);
    K1=Fdydx(xn    ,yn       ,Input);
    K2=Fdydx(xn+h/2,yn+h/2*K1,Input);
    K3=Fdydx(xn+h/2,yn+h/2*K2,Input);
    K4=Fdydx(xn+h  ,yn+h*K3  ,Input);
    y(:,ii+1)=yn+h/6*(K1+2*K2+2*K3+K4);
end
Output=[];
end

function dydx=Fdydx(x,y,Input)
%将原方程整理为dy/dx=F(y,x)的形式
%输入Input整理
m=Input(2);
l=Input(3);
g=Input(4);
%输入
th1=y(1);%角度1
th2=y(2);%角度2
pth1=y(3);%角动量1
pth2=y(4);%角动量2
%利用拉格朗日法得到的方程
M=l^2*m*(-16 + 9*cos(th1 - th2)^2);
dth1 = -6*(2*pth1 - 3*pth2*cos(th1 - th2))/M; 
dth2 = -6*(8*pth2 - 3*pth1*cos(th1 - th2))/M;
dpth1=-0.5*l*m*(3*g*sin(th1)+dth1*dth2*l*sin(th1-th2));
dpth2=0.5*l*m*(dth1*dth2*l*sin(th1-th2)-g*sin(th2));
%整理输出
dydx=zeros(4,1);
dydx(1)=dth1;
dydx(2)=dth2;
dydx(3)=dpth1;
dydx(4)=dpth2;
end

2.2 三摆

这是三摆模型,其实和双摆大同小异。
请添加图片描述
代码也和双摆差不多:

%三摆
clear
clc
close all
%输入
N=3;%双摆
m=1;
l=1;
g=9.8;
Input=[N,m,l,g];
%初始条件和时间设置
y0=[1/2*pi;1/2*pi;1/2*pi;0;0;0];%这里全部是弧度值。分别代表[摆1与垂面夹角,摆2与垂面夹角,摆1角动量,摆2角动量]
h=1e-2;
x0=0:h:20;
%代入到ODE求解器中
[y1,Output]=ODE_RK4_hyh(x0,h,y0,Input);

%提取出角度
tN=size(y1,2);
th1=y1(1,:);
th2=y1(2,:);
th3=y1(3,:);

%计算出关节坐标
CX1_A=zeros(1,tN);
CX1_B=CX1_A+l*sin(th1);
CY1_A=zeros(1,tN);
CY1_B=CY1_A-l*cos(th1);

CX2_A=CX1_B;
CX2_B=CX2_A+l*sin(th2);
CY2_A=CY1_B;
CY2_B=CY2_A-l*cos(th2);

CX3_A=CX2_B;
CX3_B=CX3_A+l*sin(th3);
CY3_A=CY2_B;
CY3_B=CY3_A-l*cos(th3);
%绘图
n=1;
figure()
set(gcf,'position',[488   342   400   300])
for k=1:4:1100
    clf
    xlim([-3,3])
    ylim([-3,3])
    hold on
    plot([CX1_A(k),CX1_B(k)],[CY1_A(k),CY1_B(k)],'color','k','linewidth',1)
    plot([CX2_A(k),CX2_B(k)],[CY2_A(k),CY2_B(k)],'color','k','linewidth',1)
    plot([CX3_A(k),CX3_B(k)],[CY3_A(k),CY3_B(k)],'color','k','linewidth',1)
    hold off
    
    %绘制轨线
    if k>200
        n=n+1;
    end
    Nm=k-n+1;
    %轨迹1
    F_color=[1,0,0];
    F_color=F_color*0.6+[1,1,1]*0.4*0.999;
    cdata=[linspace(1,F_color(1),Nm+1)',linspace(1,F_color(2),Nm+1)',linspace(1,F_color(3),Nm+1)'];
    cdata=reshape(cdata,Nm+1,1,3);
    if k>3
        patch([CX1_B(n:k),NaN],[CY1_B(n:k),NaN],1:Nm+1,'EdgeColor','interp','Marker','none',...
          'MarkerFaceColor','flat','CData',cdata,'LineWidth',1);
    end
    %轨迹2
    F_color=[0,0,1];
    F_color=F_color*0.6+[1,1,1]*0.4*0.999;
    cdata=[linspace(1,F_color(1),Nm+1)',linspace(1,F_color(2),Nm+1)',linspace(1,F_color(3),Nm+1)'];
    cdata=reshape(cdata,Nm+1,1,3);
    if k>3
        patch([CX2_B(n:k),NaN],[CY2_B(n:k),NaN],1:Nm+1,'EdgeColor','interp','Marker','none',...
          'MarkerFaceColor','flat','CData',cdata,'LineWidth',1);
    end
    %轨迹3
    F_color=[0,1,0];
    F_color=F_color*0.6+[1,1,1]*0.4*0.999;
    cdata=[linspace(1,F_color(1),Nm+1)',linspace(1,F_color(2),Nm+1)',linspace(1,F_color(3),Nm+1)'];
    cdata=reshape(cdata,Nm+1,1,3);
    if k>3
        patch([CX3_B(n:k),NaN],[CY3_B(n:k),NaN],1:Nm+1,'EdgeColor','interp','Marker','none',...
          'MarkerFaceColor','flat','CData',cdata,'LineWidth',1);
    end
    pause(0.02)
    %这里可以插入gif输出程序
end

function [y,Output]=ODE_RK4_hyh(x,h,y0,Input)
%4阶RK方法
%h间隔为常数的算法
y=zeros(size(y0,1),size(x,2));
y(:,1)=y0;
for ii=1:length(x)-1
    yn=y(:,ii);
    xn=x(ii);
    K1=Fdydx(xn    ,yn       ,Input);
    K2=Fdydx(xn+h/2,yn+h/2*K1,Input);
    K3=Fdydx(xn+h/2,yn+h/2*K2,Input);
    K4=Fdydx(xn+h  ,yn+h*K3  ,Input);
    y(:,ii+1)=yn+h/6*(K1+2*K2+2*K3+K4);
end
Output=[];
end

function dydx=Fdydx(x,y,Input)
%将原方程整理为dy/dx=F(y,x)的形式
%三摆方程
%输入Input整理
m=Input(2);
l=Input(3);
g=Input(4);
%输入
th=y(1:3);%角度1
pth=y(4:6);%角动量1
%利用拉格朗日法得到的方程
M=l^2*m*(-169+81*cos(2*(th(1)-th(2)))-9*cos(2*(th(1)-th(3)))+45*cos(2*(th(2)-th(3))));
dth1=(6*(-23*pth(1)+9*cos(2*(th(2)-th(3)))*pth(1)+27*cos(th(1)-th(2))*pth(2)-9*cos(th(1)+th(2)-2*th(3))*pth(2)+21*cos(th(1)-th(3))*pth(3)-27*cos(th(1)-2*th(2)+th(3))*pth(3)))/M;
dth2=(6*(27*cos(th(1)-th(2))*pth(1)-9*cos(th(1)+th(2)-2*th(3))*pth(1)-47*pth(2)+9*cos(2*(th(1)-th(3)))*pth(2)-27*cos(2*th(1)-th(2)-th(3))*pth(3)+57*cos(th(2)-th(3))*pth(3)))/M;
dth3=(6*(21*cos(th(1)-th(3))*pth(1)-27*cos(th(1)-2*th(2)+th(3))*pth(1)-27*cos(2*th(1)-th(2)-th(3))*pth(2)+57*cos(th(2)-th(3))*pth(2)-143*pth(3)+81*cos(2*(th(1)-th(2)))*pth(3)))/M;
dth=[dth1;dth2;dth3];
dpth1=-0.5*l*m*(5*g*sin(th(1))+l*dth(1)*(3*dth(2)*sin(th(1)-th(2))+dth(3)*sin(th(1)-th(3))));
dpth2=-0.5*l*m*(-3*l*dth(1)*dth(2)*sin(th(1)-th(2))+3*g*sin(th(2))+l*dth(2)*dth(3)*sin(th(2)-th(3)));
dpth3=0.5*l*m*(l*dth(1)*dth(3)*sin(th(1)-th(3))+l*dth(2)*dth(3)*sin(th(2)-th(3))-g*sin(th(3)));
%整理输出
dydx=zeros(6,1);
dydx(1)=dth1;
dydx(2)=dth2;
dydx(3)=dth3;
dydx(4)=dpth1;
dydx(5)=dpth2;
dydx(6)=dpth3;
end

3 多摆的混沌性

对于多摆来说,初始角度越小,其线性越好,非线性影响越小。比如下图为不同角度自由释放的双摆模型,随着时间推进,角度越大的摆,其与周边摆的差异性越大。在最下面的几个摆基本保持着左右摆动的周期运动。而靠上的摆早早的在第一个周期内,便出现了运动的混乱性(定性来看,就是率先的失去了和周围摆运动的一致性,代表误差经系统传递被放大到失去控制的程度)。
请添加图片描述
对于三摆来说,也是一样,越靠下线性度越好:
请添加图片描述

下面是三摆绘图的程序。双摆大同小异,稍微在基础上改一下就行,就不放出来了。

%三摆,做出很多摆的动画
%4阶RK求解
clear
clc
close all

%输入
N=3;%三摆
m=1;
l=1;
g=9.8;
Input=[N,m,l,g];
h=5e-3;
x0=0:h:20;
angle_list=1:2:359;
N_angle=numel(angle_list);
tN=numel(x0);
%构建储存单元
CX1_A_Sum=zeros(N_angle,tN);
CX1_B_Sum=CX1_A_Sum;
CX2_A_Sum=CX1_A_Sum;
CX2_B_Sum=CX1_A_Sum;
CX3_A_Sum=CX1_A_Sum;
CX3_B_Sum=CX1_A_Sum;

CY1_A_Sum=CX1_A_Sum;
CY1_B_Sum=CX1_A_Sum;
CY2_A_Sum=CX1_A_Sum;
CY2_B_Sum=CX1_A_Sum;
CY3_A_Sum=CX1_A_Sum;
CY3_B_Sum=CX1_A_Sum;
for k=1:N_angle
    angle_k=deg2rad(angle_list(k));
    %初始条件和时间设置
    y0=[angle_k;angle_k;angle_k;0;0;0];%这里全部是弧度值。分别代表[摆1与垂面夹角,摆2与垂面夹角,摆1角动量,摆2角动量]
    %代入到ODE求解器中
    [y1,Output]=ODE_RK4_hyh(x0,h,y0,Input);
    
    %提取出角度
    th1=y1(1,:);
    th2=y1(2,:);
    th3=y1(3,:);
    
    %计算出关节坐标
    CX1_A=zeros(1,tN);
    CX1_B=CX1_A+l*sin(th1);
    CY1_A=zeros(1,tN);
    CY1_B=CY1_A-l*cos(th1);
    
    CX2_A=CX1_B;
    CX2_B=CX2_A+l*sin(th2);
    CY2_A=CY1_B;
    CY2_B=CY2_A-l*cos(th2);
    
    CX3_A=CX2_B;
    CX3_B=CX3_A+l*sin(th3);
    CY3_A=CY2_B;
    CY3_B=CY3_A-l*cos(th3);
    
    CX1_A_Sum(k,:)=CX1_A;
    CX1_B_Sum(k,:)=CX1_B;
    CX2_A_Sum(k,:)=CX2_A;
    CX2_B_Sum(k,:)=CX2_B;
    CX3_A_Sum(k,:)=CX3_A;
    CX3_B_Sum(k,:)=CX3_B;
    
    CY1_A_Sum(k,:)=CY1_A;
    CY1_B_Sum(k,:)=CY1_B;
    CY2_A_Sum(k,:)=CY2_A;
    CY2_B_Sum(k,:)=CY2_B;
    CY3_A_Sum(k,:)=CY3_A;
    CY3_B_Sum(k,:)=CY3_B;
    
end

%绘图
%第t时刻的单摆图
t_k=100;
figure('Color','white')
set(gcf,'position',[488   342   400   300])
for t_k=1:4:800%720
clf
xlim([-N,N])
ylim([-N,N])
axis off equal
hold on
for k=1:N_angle
    %绘制摆
    plot([CX1_A_Sum(k,t_k),CX1_B_Sum(k,t_k)],[CY1_A_Sum(k,t_k),CY1_B_Sum(k,t_k)],'color','k')
    plot([CX2_A_Sum(k,t_k),CX2_B_Sum(k,t_k)],[CY2_A_Sum(k,t_k),CY2_B_Sum(k,t_k)],'color','k')
    plot([CX3_A_Sum(k,t_k),CX3_B_Sum(k,t_k)],[CY3_A_Sum(k,t_k),CY3_B_Sum(k,t_k)],'color','k')
end
hold off
pause(0.01)
end

function [y,Output]=ODE_RK4_hyh(x,h,y0,Input)
%4阶RK方法
%h间隔为常数的算法
y=zeros(size(y0,1),size(x,2));
y(:,1)=y0;
for ii=1:length(x)-1
    yn=y(:,ii);
    xn=x(ii);
    K1=Fdydx(xn    ,yn       ,Input);
    K2=Fdydx(xn+h/2,yn+h/2*K1,Input);
    K3=Fdydx(xn+h/2,yn+h/2*K2,Input);
    K4=Fdydx(xn+h  ,yn+h*K3  ,Input);
    y(:,ii+1)=yn+h/6*(K1+2*K2+2*K3+K4);
end
Output=[];
end

function dydx=Fdydx(x,y,Input)
%将原方程整理为dy/dx=F(y,x)的形式
%输入Input整理
m=Input(2);
l=Input(3);
g=Input(4);
%输入
th=y(1:3);%角度1
pth=y(4:6);%角动量1
%利用拉格朗日法得到的方程
M=l^2*m*(-169+81*cos(2*(th(1)-th(2)))-9*cos(2*(th(1)-th(3)))+45*cos(2*(th(2)-th(3))));
dth1=(6*(-23*pth(1)+9*cos(2*(th(2)-th(3)))*pth(1)+27*cos(th(1)-th(2))*pth(2)-9*cos(th(1)+th(2)-2*th(3))*pth(2)+21*cos(th(1)-th(3))*pth(3)-27*cos(th(1)-2*th(2)+th(3))*pth(3)))/M;
dth2=(6*(27*cos(th(1)-th(2))*pth(1)-9*cos(th(1)+th(2)-2*th(3))*pth(1)-47*pth(2)+9*cos(2*(th(1)-th(3)))*pth(2)-27*cos(2*th(1)-th(2)-th(3))*pth(3)+57*cos(th(2)-th(3))*pth(3)))/M;
dth3=(6*(21*cos(th(1)-th(3))*pth(1)-27*cos(th(1)-2*th(2)+th(3))*pth(1)-27*cos(2*th(1)-th(2)-th(3))*pth(2)+57*cos(th(2)-th(3))*pth(2)-143*pth(3)+81*cos(2*(th(1)-th(2)))*pth(3)))/M;
dth=[dth1;dth2;dth3];
dpth1=-0.5*l*m*(5*g*sin(th(1))+l*dth(1)*(3*dth(2)*sin(th(1)-th(2))+dth(3)*sin(th(1)-th(3))));
dpth2=-0.5*l*m*(-3*l*dth(1)*dth(2)*sin(th(1)-th(2))+3*g*sin(th(2))+l*dth(2)*dth(3)*sin(th(2)-th(3)));
dpth3=0.5*l*m*(l*dth(1)*dth(3)*sin(th(1)-th(3))+l*dth(2)*dth(3)*sin(th(2)-th(3))-g*sin(th(3)));
%整理输出
dydx=zeros(6,1);
dydx(1)=dth1;
dydx(2)=dth2;
dydx(3)=dth3;
dydx(4)=dpth1;
dydx(5)=dpth2;
dydx(6)=dpth3;
end
含阻尼混沌双摆可以用 MATLAB 进行仿真。下面是一个简单的 MATLAB 代码示例,用于模拟含阻尼混沌双摆的运动: ```matlab % 定义常数 g = 9.81; % 重力加速度 l1 = 1; % 第一根杆子的长度 l2 = 1; % 第二根杆子的长度 m1 = 1; % 第一根杆子的质量 m2 = 1; % 第二根杆子的质量 b1 = 0.1; % 第一根杆子的阻尼系数 b2 = 0.1; % 第二根杆子的阻尼系数 % 定义初始条件 theta1 = pi/4; % 第一根杆子的初始角度 theta2 = pi/2; % 第二根杆子的初始角度 omega1 = 0; % 第一根杆子的初始角速度 omega2 = 0; % 第二根杆子的初始角速度 % 定义时间步长和时间范围 dt = 0.01; % 时间步长 t = 0:dt:100; % 时间范围 % 初始化数组 theta1_array = zeros(size(t)); theta2_array = zeros(size(t)); % 循环求解含阻尼混沌双摆的运动 for i = 1:length(t) % 计算角加速度 alpha1 = (-b1*omega1 - g*(2*m1+m2)*sin(theta1)-m2*g*sin(theta1-2*theta2)-2*sin(theta1-theta2)*m2*(omega2^2*l2+omega1^2*l1*cos(theta1-theta2)))/(l1*(2*m1+m2-m2*cos(2*theta1-2*theta2))); alpha2 = (-b2*omega2 + 2*sin(theta1-theta2)*(omega1^2*l1*(m1+m2)+g*(m1+m2)*cos(theta1)+omega2^2*l2*m2*cos(theta1-theta2)))/(l2*(2*m1+m2-m2*cos(2*theta1-2*theta2))); % 计算角速度 omega1 = omega1 + alpha1*dt; omega2 = omega2 + alpha2*dt; % 计算角度 theta1 = theta1 + omega1*dt; theta2 = theta2 + omega2*dt; % 保存角度到数组中 theta1_array(i) = theta1; theta2_array(i) = theta2; end % 绘制含阻尼混沌双摆的运动轨迹 x1 = l1*sin(theta1_array); y1 = -l1*cos(theta1_array); x2 = x1 + l2*sin(theta2_array); y2 = y1 - l2*cos(theta2_array); plot(x1,y1,'b',x2,y2,'r'); axis equal; xlabel('x'); ylabel('y'); title('含阻尼混沌双摆的运动轨迹'); legend('第一根杆子','第二根杆子'); ``` 这个代码将会模拟含阻尼混沌双摆的运动,并且绘制出双摆的运动轨迹。你可以根据自己的需要调整代码中的参数和初始条件,以便更好地理解含阻尼混沌双摆的运动。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值