Symbolic derivative of function using diff()

For those familiar, I am writing a function to find the EOMs using the lagrangian.
I have defined all variables as syms: x(t), y(t), z(t)...
Afterward, I define eqns KE and PE as functions of x,y, and z; and L = KE - PE.
When I call diff(L,x(t)) matlab returns the correct answer, but when attempting to take a second derivative with respect to time [diff(diff(L,x(t)),t)], Matlab calls the incorrect form of diff() and yields: "Error using sym/diff, [secondary] arguments must not be symbolic." Is there a way to force this command such that it yields a result? Happy to provide specific code on request, thank you.

回答 (2 件)

Mahdi
Mahdi 2014 年 5 月 23 日

1 投票

Why don't you just tell diff to take the second derivative immediately? Look under the Higher-Order Derivatives subsection for diff.
In this case, you can do:
diff(L,x(t),2) % Second derivative

6 件のコメント

Oscar Sandoval
Oscar Sandoval 2014 年 5 月 23 日
Mahdi,
Second derivative is wrt a different variable - i.e. d/dt(d/dx(f(x))), in accordance with first term of lagrangian formulation of equations of motion.
Mahdi
Mahdi 2014 年 5 月 23 日
Try
diff(L,x(t),t)
Also make sure that t is also defined as a symbol
syms t
Oscar Sandoval
Oscar Sandoval 2014 年 5 月 23 日
Mahdi,
diff(L,x_d(t),t): 'Error using sym/diff (line 69) The second and third arguments must either be variables or a variable and a nonnegative integer specifying the number of differentiations.'
It appears to interpret 't' as t number of differentiations.
Mahdi
Mahdi 2014 年 5 月 23 日
Sorry, I'm trying to understand the problem itself here. Is t a defined set of numbers? Or is it just a symbol? Can you show me the definitions of x(t) and y(t) (what you made them equal to?)?
Oscar Sandoval
Oscar Sandoval 2014 年 5 月 23 日
Mahdi,
x(t) and y(t) are two of six coordinates defining position and orientation of the Center of Mass of a dynamic system. These variables are measured inputs of the system. I am attempting to solve the function symbolically to arrive at the EoMs of the system, which will then be evaluated at the measured values of x and y, amongst other inputs, in order to design a real-time controller. Are you familiar with the Euler-Lagrange equations?
Mahdi
Mahdi 2014 年 5 月 26 日
Sorry for the late reply,
I am afraid that I am not. I would suggest posting the question again and adding a bit more detail. Try searching the forums because I have run into this problem into the past and found a solution online.

サインインしてコメントする。

Mischa Kim
Mischa Kim 2015 年 6 月 13 日

0 投票

Check out the Euler-Lagrange tool on File Exchange.

質問済み:

2014 年 5 月 23 日

編集済み:

2015 年 6 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by