Differentiate with respect to a symbolic function

조회 수: 9 (최근 30일)
Leonardo Dipilato
Leonardo Dipilato 2020년 9월 24일
댓글: Leonardo Dipilato 2020년 9월 25일
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에서 Calculus에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by