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

조회 수: 15 (최근 30일)
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
Walter Roberson
Walter Roberson 2020년 10월 19일
Interesting, I did not notice that in R2020b they improved this.
My answer below applies up to R2020a.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 19일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Assembly에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by