如何在MATLAB中根据另一个符号函数导出符号函数?

如何在MATLAB中根据另一个符号函数导出符号函数?,matlab,symbolic-math,differentiation,Matlab,Symbolic Math,Differentiation,代码如上所示。正如你们所看到的,phi(t)是符号时间相关函数,phidot是它的导数(也是时间相关的)。L是使用这些符号函数得到的。 所以,问题是我不能用Matlab中的phidot来推导L。出现以下错误: 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;

代码如上所示。正如你们所看到的,phi(t)是符号时间相关函数,phidot是它的导数(也是时间相关的)。L是使用这些符号函数得到的。 所以,问题是我不能用Matlab中的phidot来推导L。出现以下错误:

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);
有没有办法从另一个符号函数中导出符号函数?如果没有,您能为我推荐另一种避免此类错误的方法吗?

可能重复的

如果你想区分L和q,q必须是a 变量可以使用subs将其替换为函数并计算 d/dt(dL/dq')之后

记住那些导数是偏导数。因此,只需显式地包含变量并获取表达式

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)
% 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)