本文是对官方教程拉格朗日方法解转盘中代码的解读。
创建坐标系
from sympy import symbols, cos, sin, print_latex
from sympy.physics.mechanics import *
mechanics_printing(pretty_print=False)
q1, q2, q3 = dynamicsymbols('q1 q2 q3')
q1d, q2d, q3d = dynamicsymbols('q1 q2 q3', 1)
r, m, g = symbols('r m g')
【dynamicsymbols】自动生成的符号是时间的函数,其第二个参数为对时间的导数阶数。故而上述代码中, q i q_i qi表示位置, q i d q_i^d qid可表示速度。
N = ReferenceFrame('N')
Y = N.orientnew('Y', 'Axis', [q1, N.z])
L = Y.orientnew('L', 'Axis', [q2, Y.x])
R = L.orientnew('R', 'Axis', [q3, L.y])
其中, N N N是参考坐标系,随后用【orientnew】方法创建了三个关联坐标系,其中 Y Y Y绕 N N N的 z z z轴旋转角度 q 1 q_1 q1; L L L绕 Y Y Y的 x x x轴再旋转 q 2 q_2 q2角度,将用于描述刚体的惯性并矢; R R R绕 L L L的 y y y轴旋转角度 q 3 q_3 q3,将作为刚体内部的坐标框架。
创建刚体
C = Point('C')
C.set_vel(N, 0)
D = C.locatenew('D', r * L.z)
D.v2pt_theory(C, N, R)
I = inertia(L, m / 4 * r**2, m / 2 * r**2, m / 4 * r**2)
print_latex(I)
BodyD = RigidBody('BodyD', D, R, m, (I, D))
BodyD.potential_energy = - m * g * r * cos(q2)
Lag = Lagrangian(N, BodyD)
C C C是一个点,在坐标系 N N N中的速度为0。
以 C C C开始,沿着 L L L坐标系的 z z z轴方向移动距离 r r r,定义了点 D D D。接下来,通过【v2pt_theory】方法,计算了 D D D在 N N N中的速度。
【itertial】用于创建惯性并矢,其值为 m r 2 4 l ^ x ⊗ l ^ x + m r 2 2 l ^ y ⊗ l ^ y + m r 2 4 l ^ z ⊗ l ^ z \frac{m r^{2}}{4}\mathbf{\hat{l}_x}\otimes \mathbf{\hat{l}_x} + \frac{m r^{2}}{2}\mathbf{\hat{l}_y}\otimes \mathbf{\hat{l}_y} + \frac{m r^{2}}{4}\mathbf{\hat{l}_z}\otimes \mathbf{\hat{l}_z} 4mr2l^x⊗l^x+2mr2l^y⊗l^y+4mr2l^z⊗l^z。
【bodyD】即刚体,位于坐标系 R R R中,与 N N N在三个坐标轴方向分别有 q 1 , q 2 , q 3 q_1, q_2, q_3 q1,q2,q3的夹角。势能为 − m g r cos ( q 2 ) -mgr\cos(q_2) −mgrcos(q2)。【Lag】为其拉格朗日量。
拉格朗日方法
q = [q1, q2, q3]
l = LagrangesMethod(Lag, q)
le = l.form_lagranges_equations()
le.simplify()
print_latex(le)
lrhs = l.rhs()
lrhs.simplify()
print_latex(lrhs)
【LagrangesMethod】可以将拉格朗日量和广义坐标为参数,构建一个拉格朗日方法对象。该对象的【form_lagranges_equations】方法,可构造出一个规范的拉格朗日方程。
[ m r 2 ( 12 sin ( q 2 ( t ) ) d 2 d t 2 q 3 ( t ) + 10 sin ( 2 q 2 ( t ) ) d d t q 1 ( t ) d d t q 2 ( t ) + 12 cos ( q 2 ( t ) ) d d t q 2 ( t ) d d t q 3 ( t ) − 5 cos ( 2 q 2 ( t ) ) d 2 d t 2 q 1 ( t ) + 7 d 2 d t 2 q 1 ( t ) ) 8 m r ( 8 g sin ( q 2 ( t ) ) − 5 r sin ( 2 q 2 ( t ) ) ( d d t q 1 ( t ) ) 2 − 12 r cos ( q 2 ( t ) ) d d t q 1 ( t ) d d t q 3 ( t ) + 10 r d 2 d t 2 q 2 ( t ) ) 8 3 m r 2 ( sin ( q 2 ( t ) ) d 2 d t 2 q 1 ( t ) + cos ( q 2 ( t ) ) d d t q 1 ( t ) d d t q 2 ( t ) + d 2 d t 2 q 3 ( t ) ) 2 ] \left[\begin{matrix}\frac{m r^{2} \left(12 \sin{\left(q_{2}{\left(t \right)} \right)} \frac{d^{2}}{d t^{2}} q_{3}{\left(t \right)} + 10 \sin{\left(2 q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} \frac{d}{d t} q_{2}{\left(t \right)} + 12 \cos{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{2}{\left(t \right)} \frac{d}{d t} q_{3}{\left(t \right)} - 5 \cos{\left(2 q_{2}{\left(t \right)} \right)} \frac{d^{2}}{d t^{2}} q_{1}{\left(t \right)} + 7 \frac{d^{2}}{d t^{2}} q_{1}{\left(t \right)}\right)}{8}\\\frac{m r \left(8 g \sin{\left(q_{2}{\left(t \right)} \right)} - 5 r \sin{\left(2 q_{2}{\left(t \right)} \right)} \left(\frac{d}{d t} q_{1}{\left(t \right)}\right)^{2} - 12 r \cos{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} \frac{d}{d t} q_{3}{\left(t \right)} + 10 r \frac{d^{2}}{d t^{2}} q_{2}{\left(t \right)}\right)}{8}\\\frac{3 m r^{2} \left(\sin{\left(q_{2}{\left(t \right)} \right)} \frac{d^{2}}{d t^{2}} q_{1}{\left(t \right)} + \cos{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} \frac{d}{d t} q_{2}{\left(t \right)} + \frac{d^{2}}{d t^{2}} q_{3}{\left(t \right)}\right)}{2}\end{matrix}\right] 8mr2(12sin(q2(t))dt2d2q3(t)+10sin(2q2(t))dtdq1(t)dtdq2(t)+12cos(q2(t))dtdq2(t)dtdq3(t)−5cos(2q2(t))dt2d2q1(t)+7dt2d2q1(t))8mr(8gsin(q2(t))−5rsin(2q2(t))(dtdq1(t))2−12rcos(q2(t))dtdq1(t)dtdq3(t)+10rdt2d2q2(t))23mr2(sin(q2(t))dt2d2q1(t)+cos(q2(t))dtdq1(t)dtdq2(t)+dt2d2q3(t))
【lrhs】为拉格朗日方程的右侧
[ d d t q 1 ( t ) d d t q 2 ( t ) d d t q 3 ( t ) − 2 ( 2 tan ( q 2 ( t ) ) d d t q 1 ( t ) + 3 d d t q 3 ( t ) cos ( q 2 ( t ) ) ) d d t q 2 ( t ) − 4 g sin ( q 2 ( t ) ) 5 r + sin ( 2 q 2 ( t ) ) ( d d t q 1 ( t ) ) 2 2 + 6 cos ( q 2 ( t ) ) d d t q 1 ( t ) d d t q 3 ( t ) 5 ( − 5 cos ( q 2 ( t ) ) d d t q 1 ( t ) + 6 tan ( q 2 ( t ) ) d d t q 3 ( t ) + 4 d d t q 1 ( t ) cos ( q 2 ( t ) ) ) d d t q 2 ( t ) ] \left[\begin{matrix}\frac{d}{d t} q_{1}{\left(t \right)}\\\frac{d}{d t} q_{2}{\left(t \right)}\\\frac{d}{d t} q_{3}{\left(t \right)}\\- 2 \left(2 \tan{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} + \frac{3 \frac{d}{d t} q_{3}{\left(t \right)}}{\cos{\left(q_{2}{\left(t \right)} \right)}}\right) \frac{d}{d t} q_{2}{\left(t \right)}\\- \frac{4 g \sin{\left(q_{2}{\left(t \right)} \right)}}{5 r} + \frac{\sin{\left(2 q_{2}{\left(t \right)} \right)} \left(\frac{d}{d t} q_{1}{\left(t \right)}\right)^{2}}{2} + \frac{6 \cos{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} \frac{d}{d t} q_{3}{\left(t \right)}}{5}\\\left(- 5 \cos{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{1}{\left(t \right)} + 6 \tan{\left(q_{2}{\left(t \right)} \right)} \frac{d}{d t} q_{3}{\left(t \right)} + \frac{4 \frac{d}{d t} q_{1}{\left(t \right)}}{\cos{\left(q_{2}{\left(t \right)} \right)}}\right) \frac{d}{d t} q_{2}{\left(t \right)}\end{matrix}\right] dtdq1(t)dtdq2(t)dtdq3(t)−2(2tan(q2(t))dtdq1(t)+cos(q2(t))3dtdq3(t))dtdq2(t)−5r4gsin(q2(t))+2sin(2q2(t))(dtdq1(t))2+56cos(q2(t))dtdq1(t)dtdq3(t)(−5cos(q2(t))dtdq1(t)+6tan(q2(t))dtdq3(t)+cos(q2(t))4dtdq1(t))dtdq2(t)