Euler-Lagrange方程的符号微分 [英] Symbolic differentiation with Euler-Lagrange equation
问题描述
我正在尝试为机器人结构计算Euler-Lagrange方程.
我将使用q
指示关节变量的向量.
I'm trying to calculate Euler-Lagrange equations for a robotic structure.
I'll use q
to indicate the vector of the joint variables.
在我的代码中,我使用
syms t;
q1 = sym('q1(t)');
q2 = sym('q2(t)');
q = [q1, q2];
声明q1
和q2
取决于时间t
.
在我计算了拉格朗日L
之后(在这种情况下,这是一个与旋转关节的简单链接)
to declare that q1
and q2
depend on time t
.
After I calculate the Lagrangian L
(in this case it is a simple link with a rotoidal joint)
L = (I1z*diff(q1(t), t)^2)/2 + (L1^2*M1*diff(q1(t), t)^2)/8
问题是,当我尝试使用diff(L, q)
区分L
与q
的方面时,出现此错误
The problem is that when I try to differentiate L
respect to q
using diff(L, q)
, I get this error
使用sym/diff时出错(第69行)
第二个参数必须是一个变量或一个非负整数,用于指定微分的数量.
Error using sym/diff (line 69)
The second argument must be a variable or a nonnegative integer specifying the number of differentiations.
如何区分L
与q
的关系,以得到Euler-Lagrange方程的第一项?
How can I differentiate L
respect to q
to have the first term of the Euler-Lagrange equation?
我也试图简单地写q
为
syms q1 q2
q = [q1 q2]
没有时间依赖性,但微分将不起作用,即显然会给我[0, 0]
without the time dependency but differentiation will not work, i.e. will obviously give me [0, 0]
这就是我在工作区中得到的(I1z是链接相对于z轴的惯性,M1是链接的质量,L1是链接的长度)
That's what I've got in the workspace (I1z is the inertia of the link respect to z-axis, M1 is the mass of the link, L1 is the length of the link)
q = [q1(t), q2(t)]
diff(q, t) = [diff(q1(t), t), diff(q2(t), t)]
L = (I1z*diff(q1(t), t)^2)/2 + (L1^2*M1*diff(q1(t), t)^2)/8
If you want to run the full code, you have to download all the .m files from here and then use
[t, q, L, M, I] = initiate();
L = lagrangian(odof(q, L), q, M, I, t, 1)
否则下面的代码应该相同.
otherwise the following code should be the same.
syms t I1z L1 M1
q1 = sym('q1(t)');
q2 = sym('q2(t)');
q = [q1, q2];
qp = diff(q, t);
L = (I1z*qp(1)^2)/2 + (L1^2*M1*qp(1)^2)/8;
编辑
感谢 AVK的答案我意识到了这个问题.
EDIT
Thanks to AVK's answer I realized the problem.
syms t q1 q2 q1t q2t I1z L1 M1 % variables
L = (I1z*q1t^2)/2 + (L1^2*M1*q1t^2)/8
dLdqt = [diff(L,q1t), diff(L,q2t)]
这将起作用,其结果将是
This will work and its result will be
dLdqt = [(M1*q1t*L1^2)/4 + I1z*q1t, 0]
syms t q1 q2 q1t q2t I1z L1 M1
L = (I1z*q1t^2)/2 + (L1^2*M1*q1t^2)/8;
qt = [q1t q2t];
dLdqt = diff(L, qt)
这将无效有效,因为diff
期望单个差异变量
This will not work, because diff
expects a single variable of differentiation
syms t q1 q2 q1t q2t I1z L1 M1
L = (I1z*q1t^2)/2 + (L1^2*M1*q1t^2)/8;
qt = [q1t q2t];
dLdqt = jacobian(L, qt)
此将有效,因为jacobian
期望至少个差异变量
This will work, because jacobian
expects at least a variable of differentiation
似乎MATLAB的Symbolit Toolbox无法处理关于q(t)
的差异,因此您必须使用变量 q
.
Seems that MATLAB's Symbolit Toolbox can't handle differentiation with respect to q(t)
, so you have to use the variable q
.
因此将它们用作功能
q = [q1(t), q2(t), q3(t), q4(t), q5(t), q6(t)]
qp = [diff(q1(t), t), diff(q2(t), t), diff(q3(t), t), diff(q4(t), t), diff(q5(t), t), diff(q6(t), t)]
和这些作为变量
qv = [q1, q2, q3, q4, q5, q6];
qvp = [q1p, q2p, q3p, q4p, q5p, q6p];
解决了问题.
整个代码如下:
syms q1 q2 q3 q4 q5 q6;
syms q1p q2p q3p q4p q5p q6p;
qv = [q1, q2, q3, q4, q5, q6];
qvp = [q1p, q2p, q3p, q4p, q5p, q6p];
Lagv = subs(Lag, [q, qp], [qv, qvp]);
dLdq = jacobian(Lagv, qv);
dLdqp = jacobian(Lagv, qvp);
dLdq = subs(dLdq, [qv, qvp], [q, qp]);
dLdqp = subs(dLdqp, [qv, qvp], [q, qp]);
m_eq = diff(dLdqp, t) - dLdq;
推荐答案
如果要针对q区分L,则q必须是变量.您可以使用subs
将其替换为函数并进行计算
以后:
If you want to differentiate L with respect to q, q must be a variable. You can use subs
to replace it with a function and calculate
later:
syms t q1 q2 q1t q2t I1z L1 M1 % variables
L = (I1z*q1t^2)/2 + (L1^2*M1*q1t^2)/8
dLdqt= [diff(L,q1t), diff(L,q2t)]
dLdq = [diff(L,q1), diff(L,q2)]
syms q1_f(t) q2_f(t) % functions
q1t_f(t)= diff(q1_f,t)
q2t_f(t)= diff(q2_f,t)
% replace the variables with the functions
dLdq_f= subs(dLdq,{q1 q2 q1t q2t},{q1_f q2_f q1t_f q2t_f})
dLdqt_f= subs(dLdqt,{q1 q2 q1t q2t},{q1_f q2_f q1t_f q2t_f})
% now we can solve the equation
dsolve(diff(dLdqt_f,t)-dLdq_f==0)
这篇关于Euler-Lagrange方程的符号微分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!