MATLAB: Solve the chain rule in lagrange equation

chain rule

Hai guys,
I would like to ask your opinion about this matter. I have a code as shown below which basically, is the Lagrange equation. I have reached to the Lagrange equation and next, I would like to differentiate the Lagrange equation.
Basically, the Lagrange eqaution is as :
L = KE - PE
As shown in the pic, now, I would like to differentiate the L to the r_dot. Could anyone advice me? I have try several ways, but it does not give me the correct answer. Thanks in advance
Regards,
Siti
syms x1(t) x2(t) x3(t) x4(t)
syms a la
syms m1 m2 m3 m4 I1 I2 I3 I4
syms g h1 h2 h3 h4
T0_1=[cos(x1(t)) 0 sin(x1(t)) 0 ;
sin(x1(t)) 0 -cos(x1(t)) 0;
0 1 0 0;
0 0 0 1;];
T1_2=[cos(x2(t)) 0 sin(x2(t)) 0;
sin(x2(t)) 0 -cos(x2(t)) 0;
0 1 0 0;
0 0 0 1;];
T2_3=[cos(x3(t)) 0 sin(x3(t)) 0;
sin(x3(t)) 0 -cos(x3(t)) 0;
0 1 0 -a;
0 0 0 1;];
T3_4=[cos(x4(t)) 0 -sin(x4(t)) -la*cos(x4(t));
sin(x4(t)) 0 cos(x4(t)) -la*sin(x4(t));
0 -1 0 0;
0 0 0 1;];
T1 = T0_1;
T2 = T1*T1_2;
T3 = T2*T2_3;
T4 = T3*T3_4;
xT1 = T1(1,4);
xT1_dot(t) = diff(xT1);
yT1 = T1(2,4);
yT1_dot(t) = diff(yT1);
xT2 = T2(1,4);
xT2_dot(t) = diff(xT2);
yT2 = T2(2,4);
yT2_dot(t) = diff(yT2);
xT3 = T3(1,4)
xT3_dot(t) = diff(xT3);
yT3 = T3(2,4);
yT3_dot(t) = diff(yT3);
xT4 = T4(1,4);
xT4_dot(t) = diff(xT4);
yT4 = T4(2,4);
yT4_dot(t) = diff(yT4);
% v1 = simplify((xT1_dot(t) + yT1_dot(t))^2);
% v2 = simplify((xT2_dot(t) + yT2_dot(t))^2);
% v3 = simplify((xT3_dot(t) + yT3_dot(t))^2);
% v4 = simplify((xT4_dot(t) + yT4_dot(t))^2);
v1 = ((xT1_dot(t) + yT1_dot(t))^2);
v2 = ((xT2_dot(t) + yT2_dot(t))^2);
v3 = ((xT3_dot(t) + yT3_dot(t))^2);
v4 = ((xT4_dot(t) + yT4_dot(t))^2);
KE = (1/2)*m1*v1 + (1/2)*m2*v2 + (1/2)*m3*v3 + (1/2)*m4*v4 + ...
(1/2)*I1*diff(x1(t), t) + (1/2)*I2*diff(x2(t), t) + (1/2)*I3*diff(x3(t), t) + (1/2)*I4*diff(x4(t), t) ;
PE = m1*g*h1 + m2*g*h2 + m3*g*h3 + m4*g*h4 ;
Lag = KE - PE;

Best Answer

Siti, the Euler-Lagrange tool package on File Exchange does exactly what you are looking for.