我在JupyterLab
中有一个很长的符号表达式,我试图用它替换值。该表达式很长,因此除非有必要,否则我不会详细说明它的含义。输出的符号表达式(使用SymPy
)。我正在尝试使用如下所示的.subs
命令,但这不是替换我拥有的任何变量,而是输出相同的符号。我以前使用过.subs
,但是容量非常有限,因此不确定是否存在类型问题或类似问题。所有这些变量均以符号表示法表示。
replacements=[(t1,np.pi/2), (t1d,1), (t1dd,1), (l1,1), (l2,1), (m1,1), (m2,1), (m3,1), (g,-10)]
Lagrangian = Lagrangian.subs(replacements)
display(Lagrangian)
使用SymPy
命令symbols
通过以下方式定义这些变量,然后对其进行操作以获取以下表达式。并不是所有定义的符号都在最终表达式中,而是中间符号。
l1, l2= sp.symbols('l_1, l_2')
y1, y2, y3, y1d, y2d, y3d = sp.symbols('y_1, y_2, y_3, ydot_1, ydot_2, ydot_3')
t1, t2, t1d, t2d= sp.symbols('theta_1, theta_1, thetadot_1, thetadot_2')
t1dd, t2dd = sp.symbols('thetaddot_1, thetaddot_2')
m1, m2, m3 = sp.symbols('m_1, m_2, m_3')
I1, I2 = sp.symbols('I_1, I_2')
g = sp.symbols('g')
对于Lagrangian
,这只是对所有这些变量的操纵。我将在下面显示以下一些操作,但是我认为并不是造成问题的必要条件。
I1 = 1/3*m1*l1**2
I2 = 1/3*m2*l2**2
t2 = sp.acos(l1/l2*sp.sin(t1))
t2d = sp.diff(t2, t1) * t1d
y1 = 0.5*l1*sp.cos(t1)
y1d = sp.diff(y1,t1) * t1d
y2 = l1*sp.cos(t1) + 0.5*l2*sp.cos(t2)
y2d = 2*y1d - 0.5*l2*sp.sin(t2)*t2d
y3 = l1*sp.cos(t1) + l2*sp.cos(t2)
y3d = 2*y1d + 2*y2d
T = 0.5*m1*y1d**2 + 0.5*m2*y2d**2 + 0.5*m3*y3d**2 + 0.5*I1*t1d**2 + 0.5*I2*t2d**2
U = m1*g*y1 + m2*g*y2 + m3*g*y3
print('T=')
display(sp.simplify(T))
print('U=')
display(sp.simplify(U))
dTdTheta_dot = sp.diff(T,t1d)
dTdTheta = sp.diff(T,t1)
dUdTheta = sp.diff(U,t1)
#d_dt =
print('dT/dtheta_dot=')
display(sp.simplify(dTdTheta_dot))
print('dT/dTheta=')
display(sp.simplify(dTdTheta))
print('dU/dTheta=')
display(sp.simplify(dUdTheta))
A = (1/3)*l1**4*l2**2*m2*t1d**2*sp.sin(t1)*sp.cos(t1)**3
dA = (1/3)*l1**4*l2**4*m2*(2*t1d*t1dd*sp.sin(t1)*sp.cos(t1)**3+t1d**2*sp.cos(t1)*t1d*sp.cos(t1)**3+
t1d**2*sp.sin(t1)*3*sp.cos(t1)**2*(-sp.sin(t1)*t1d))
B = l1**2*((1/3)*l2**2*m2*l1**2*t1d*sp.sin(t1)**3*sp.cos(t1)-(1/3)*l2**4*m2*t1d*sp.cos(t1)*sp.cos(t1))
dB = l1**2*(((1/3)*l1**2*l2**2*m2*t1dd*sp.sin(t1)**3*sp.cos(t1) - (1/3)*l1**2*l2**2*m2*t1d*3*sp.sin(t1)**2*sp.cos(t1)*t1d*sp.cos(t1) +
(1/3)*l1**2/l2**2*m2*t1d*sp.sin(t1)**3*(-sp.sin(t1))*t1d) - ((1/3)*l1**4*m2*t1dd*sp.sin(t1)*sp.cos(t1)+
(1/3)*l2**4*m2*t1d*sp.cos(t1)*t1d*sp.cos(t1) + (1/3)*l2**4*m2*t1d*sp.sin(t1)*(-sp.sin(t1))*t1d))
C = l1**2*(l1**4*t1d*sp.sin(t1)**4 - 2*l1**2*l2**2*t1d*sp.sin(t1)**2 + l2**4*t1d)*((1/8)*m1*sp.sin(2*t1) + (3/8)*m2*sp.sin(2*t1) - (1/2)*m2)
dC = l1**2 * ((l1**4*(t1dd*sp.sin(t1)**4 + t1d*4*sp.sin(t1)**3*sp.cos(t1)*t1d) - 2*l1**2*l2**2*(t1dd*sp.sin(t1)**2 + t1d*2*sp.sin(t1)*sp.cos(t1)*t1d)+l2**4*t1dd) *
((1/8)*m1*sp.sin(2*t1) + (3/8)*m2*sp.sin(2*t1) - (1/2)*m2*sp.cos(2*t1)) + (l1**4*t1d*sp.sin(t1)**4 - 2*l1**2*l2**2*t1d*sp.sin(t1) + l2**4*t1d) *
((1/8)*m1*sp.cos(2*t1)*2*t1d + (3/8)*m2*sp.cos(2*t1)*2*t1d - (1/2)*m2*(-sp.sin(2*t1)*2*t1d)))
D = (4*m2*sp.sin(2*t1)-3*m3*sp.cos(2*t1)) * (l1**2*sp.sin(t1)**2-l2**2)**2
dD = (4*m2*sp.cos(2*t1)*2*t1d-3*m2*(-sp.sin(2*t1))*2*t1d)*((l1**2*sp.sin(t1)**2-l2**2)**2) + (4*m2*sp.sin(2*t1)-3*m3*sp.cos(2*t1))*(2*(l1**2*sp.sin(t1)-l2**2)*(2*l1**2*sp.sin(t1)*sp.cos(t1)*t1d))
E = (l1**2*sp.sin(t1)**2-l2**2)**-2
dE = -2*(l1**2*sp.sin(t1)-l2**2)**-3*(2*l1**2*sp.sin(t1)*sp.cos(t1)*t1d)
F = (dA+dB+dC+dD)*E + (A+B+C+D)*dE
display(sp.simplify(F))
Lagrangian = F - dTdTheta + dUdTheta
display(sp.simplify(Lagrangian))
有人知道为什么.subs
不起作用吗?任何帮助将非常感激。预先感谢!