# Revision history [back]

### How to define more complicated differential expressions in Sage?

Dear Community,

i have a very hard time doing something pretty straight foreward in Sage. In mechanics, there is a Lagrange-formalism that yields the equation of motion of a system of $n$ degrees of freedom in the following way:

$$0=\frac{d}{dt}\left(\frac{\partial L}{\partial x'_i(t)}\right) - \frac{\partial L}{\partial x_i(t)}$$

where $L$ is the Lagrange function

$$L=E_{Kin} (x'_{1}(t),x'_2(t)...x'_n(t) ) - E _{Pot}(x_1(t),x_2(t),...,x_n(t))$$

is the kinetic energy minus the potential energy. The kinetic energy depends on the speeds of the coordinates $x'_i(t)$, while the potential energy depends on the coordinates $x_i(t)$.

In Mathematica, the hack is

L = Ekin[x'[t]] - Epot[x[t]];
eq = 0 == D[D[L, x'[t]], t] - D[L, x[t]];


which gives

0 == Epot'[x[t]] + Ekin''[x'[t]] x''[t]


I can easily specify something for $E_{Kin}$ and $E_{Pot}$, solve the latter equation for $x''$, and give it as a right handside to some time integration scheme.

How can this be done in Sage? Assume you have complicated expressions for $E_{Pot}$ and $E_{Kin}$ which you do not want to differentiate by hand.

### How to define more complicated differential expressions in Sage?

Dear Community,

i have a very hard time doing something pretty straight foreward in Sage. In mechanics, there is a Lagrange-formalism that yields the equation of motion of a system of $n$ degrees of freedom in the following way:

$$0=\frac{d}{dt}\left(\frac{\partial L}{\partial x'_i(t)}\right) - \frac{\partial L}{\partial x_i(t)}$$

where $L$ is the Lagrange function

$$L=E_{Kin} (x'_{1}(t),x'_2(t)...x'_n(t) (x'_{1}(t),x'_2(t)...x'_n(t); x_1(t),x_2(t)...x_n(t) ) - E _{Pot}(x_1(t),x_2(t),...,x_n(t))$$

is the kinetic energy minus the potential energy. The kinetic energy depends on the speeds of the coordinates $x'_i(t)$, while the potential energy depends on the coordinates $x_i(t)$.

In Mathematica, the hack is

L = Ekin[x'[t]] - Epot[x[t]];
eq = 0 == D[D[L, x'[t]], t] - D[L, x[t]];


which gives

0 == Epot'[x[t]] + Ekin''[x'[t]] x''[t]


I can easily specify something for $E_{Kin}$ and $E_{Pot}$, solve the latter equation for $x''$, and give it as a right handside to some time integration scheme.

How can this be done in Sage? Assume you have complicated expressions for $E_{Pot}$ and $E_{Kin}$ which you do not want to differentiate by hand.

var('L,x,t')

x = function('x',t)

L = function('L',x,t)

allows for

diff(L,t)

but not

diff(L,x)