Differentiate with respect to a symbolic function

14 次查看(过去 30 天)
I'm modeling the effects of applying vertical vibrations unto the base of an inverted pendulum. I'm approaching that with lagrangian mechanics. I derived the final equations by hand once, but I want to be able to do that with matlab, too.
Thus far, I wrote this code:
syms theta(t) A omega l m g
assume(theta(t), 'real');
assume(A, 'positive');
assume(omega, 'positive');
assume(l, 'positive');
assume(m, 'positive');
assume(g, 'positive');
x0 = [0; A*sin(omega*t)];
x = x0 + [l*sin(theta); l*cos(theta)]
T = 0.5*m*(diff(x, t)'*diff(x, t))
V = m*g*([0 1] * x);
L = T - V;
Unless I made some mistakes, this checks out until now.
The next step to apply Lagrange's equation, requiring me to compute dL/dθ and d/dt(dL/dθ'), where θ' = dθ/dt.
However, Matlab forbids me from computing diff(L, theta) since it says I can't differentiate with respect to a symbolic function. I tried to work around that by subbing theta out with La=subs(L, theta(t), a) (where a is a symbolic variable), but differentiating with respect to a using diff(La, a) returns an incorrect result.
Do you have any pointers on how to proceed?
(Also, as an added question, is there any better way to write diff(x, t)'*diff(x, t) and [0 1] * x?)
  1 个评论
Walter Roberson
Walter Roberson 2020-9-25
Substituting a variable for the function, and differentiating with respect to the variable, and substituting back the function for the variable, is the typical attempt at a work-around.
However, I have demonstrated in the past that there are circumstances under which the approach can fail. The approach depends upon the other parts of the expression being "independent" of the function being swapped out, and while that is often the case, it is not always the case. In calculus terms, you would need to use the chain rule and have lingering d<expression>/d<function> terms hanging around continuing to cause problems because they are still derivatives with respect to the unknown function. When you swap out a variable and the result of the expression appears to be independent of what is represented by the variable, then diff() is going to assume the derivative d<expression>/d<variable> is 0 which is not robust.

请先登录,再进行评论。

回答(1 个)

Bjorn Gustavsson
Bjorn Gustavsson 2020-9-25
When I've used the Euler-Lagrange method I've used this tool: Euler-Lagrange tool (that handles this). If you need to solve a problem I suggest using that package, if you intend to develop your own Euler-Lagrange-tools then I suggest you check how they've solve this problem of differentiation.
HTH
  1 个评论
Leonardo Dipilato
Leonardo Dipilato 2020-9-25
I had already solved the problem, so this was more of an exercise to me, but thanks for pointing the tool out: I'll definitely look into that.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Symbolic Math Toolbox 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by