Getting error: Second argument must be a variable or a nonnegative integer specifying the number of differentiations.

86 views (last 30 days)
I am trying to symbolically obtain the Euler-Lagrange equations, but I keep getting the "Second argument must be a variable or a nonnegative integer specifying the number of differentiations" error. It occurs with the first term of the equation (dL/dx_dot). I don't what I am doing wrong. Any help is appreciated. Thanks.
syms x(t) theta1(t) theta2(t) F(t) m0 m1 m2 I1 I2 L1 L2 g
T0 = 0.5*m0*diff(x(t),t)^2; %Kinetic energy of mass
T1 = 0.5*m1*((diff(x(t),t)+0.5*L1*diff(theta1(t),t)*cos(theta1(t)))^2+(L1*diff(theta1(t),t)*sin(theta1(t)))^2)+0.5*I1*diff(theta1(t),t)^2; %Kinetic energy of pendulum 1
T2 = 0.5*m2*((diff(x(t),t)+L1*diff(theta1(t),t)*cos(theta1(t))+0.5*L2*diff(theta2(t),t)*cos(theta2(t)))^2+(L1*diff(theta1(t),t)*sin(theta1(t))+0.5*L2*diff(theta2(t),t)*sin(theta2(t)))^2)+0.5*I2*diff(theta2(t),t)^2; %Kinetic energy of pendulum 2
T = T0+T1+T2; % Total kinetic energy
U0 = 0; %Potential energy of mass
U1 = m1*g*0.5*L1*cos(theta1(t)); %Potential energy of pendulum 1
U2 = m2*g*(L1*cos(theta2(t))+0.5*L2*cos(theta2(t))); %Potential energy of pendulum 2
U = U0+U1+U2; %Total potential energy
L = T-U; %Lagrangian of system
dL_dx_dot = diff(L,diff(x(t),t));
dL_dx = diff(L,x);
diff(dL_dx_dot,t) - dL_dx == F(t)
  3 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 19 Oct 2020
It is not possible to differentiate with respect to a function.
You have to make a substitution and hope that the system remains valid.
syms Dx;
dL_dx_dot = subs(diff( subs(L,diff(x(t),t),Dx), Dx), Dx, diff(x(t),t))
This is not guaranteed to be valid in general! In the past I have posted systems that this kind of manipulation is not valid for. It depends upon the rest of the equation being independent of the function whose derivative is being taken, and that is not always the case.
The situation is sort of like making a substitution assuming that you are not dividing by zero -- the substitution would often work, but there are cases where you accidentally introduce nonsense answers when you make such a substitution.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!