0
gr = 9.81;     %gravity
syms phi(t) m l
theta=1/3*m*l^2;
phidot=diff(phi,t);
U=m*gr*l/2*cos(phi);
T=1/2*theta*phidot^2+(1/2*phidot*l)^2*m;
L=T-U;
frst=diff(L,phidot);

The code is shown above. As you can see that phi(t) is symbolic time dependent function and phidot is derivation of it(also time dependent). L is obtained using these symbolic functions. So, The problem is I can't derive L in terms of phidot in Matlab. The error occurs as following:

Error using sym/diff (line 26)
All arguments, except for the first one, must not be **symbolic** functions.

Error in pndlm (line 11)
frst=diff(L,phidot)

Is there any way to derive symbolic function in terms of another symbolic function? If not, Can you suggest me another alternative for avoiding this kind of error?

jmoerdyk
  • 5,544
  • 7
  • 38
  • 49
  • 2
    Please don't vandalize your posts. By posting on the Stack Exchange network, you've granted a non-revocable right, under the [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0/) license, for Stack Exchange to distribute that content (i.e. regardless of your future choices). By Stack Exchange policy, the non-vandalized version of the post is the one which is distributed, and thus, any vandalism will be reverted. If you want to know more about deleting a post please see: [How does deleting work?](https://stackoverflow.com/help/what-to-do-instead-of-deleting-question). – jmoerdyk Feb 14 '23 at 17:40

1 Answers1

0

Possible duplicate of this

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 d/dt(dL/dq') later.

Remember those derivatives are partial. Hence just include explicitly the variable and obtain the expression.

% Variables
syms q qt m l g
theta=1/3*m*l^2;
% Lagrangian
U=m*g*l/2*cos(q);
T=1/2*theta*qt^2+(1/2*qt*l)^2*m;
L=T-U;
% Partial Derivatives
dLdq=diff(L,q)
dLdqt=diff(L,qt)
syms qf(t)
% Time Derivatives
qtf(t)=diff(qf,t)
dLdqf=subs(dLdqt,qt,qtf)
% Solution
m=1;l=1;g=9.81;
dsolve(diff(dLdqf,t)-dLdqf==0)
Brethlosze
  • 1,533
  • 1
  • 22
  • 41