# How to take derivative of a expression with respect to a function?

I want to program the Euler-Lagrange equations for continuous systems. But in this formulation I have to compute the derivative of the Lagrangian with respect to all the derivatives of the generalized coordinate. $$\partial_i\left(\dfrac{\partial \mathcal{L}}{\partial (\partial_i\phi) } \right) = \dfrac{\partial \mathcal L}{\partial \phi }$$ For example, for a field $\phi=\phi(x,t)$ $$\dfrac{\partial }{\partial t}\dfrac{\partial \mathcal L}{\partial \dot\phi}+\dfrac{\partial }{\partial x}\dfrac{\partial \mathcal L}{\partial \phi'}=\dfrac{\partial\mathcal L}{\partial \phi}$$ Where $\dot\phi=\dfrac{\partial\phi}{\partial t }$ and $\phi'=\dfrac{\partial \phi}{\partial x }$

I have tried with something like this

D[L, D[ϕ, t]] + D[L, D[ϕ, x]] = D[L,ϕ]


But of course, it returns an error because of the variable with respect the derivative is taken.

• Have you seen the Variational Methods Package? Jun 1, 2018 at 0:59
• Yes, but the option I saw is just for functions of one parameter ($\phi=\phi(t)$). I will check the other options. Jun 1, 2018 at 2:34
• As it is written, your second equation is just wrong: partial derivatives of the Lagrangian are independent ! You may need additional assumptions that, e.g., the total derivative of Lagrangian vanishes, plus you need integrations by parts. Your problem arises because you are trying to jump over several steps. MA has no way to guess what is desired. Jun 1, 2018 at 8:59
• @yarchik It is not wrong. I know what you mean, but you are confused. I've checked the equation on the literature. Jun 1, 2018 at 20:47

It is straightforward. Let us take a Lagrangian

L = 1/2 D[y[x, t], t]^2 - 1/(2 c^2) D[y[x, t], x]^2 + 1/2 y[x, t]^2;


We need to load the package:

Needs["VariationalMethods"]


Now, this gives the variational derivative:

VariationalD[L, y[x, t], {x, t}] and this yields the Euler equation:

EulerEquations[L, y[x, t], {x, t}]
` Have fun!

• Pretty cool! Eh? Jun 1, 2018 at 20:48