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.

  • $\begingroup$ Have you seen the Variational Methods Package? $\endgroup$
    – halirutan
    Jun 1, 2018 at 0:59
  • $\begingroup$ Yes, but the option I saw is just for functions of one parameter ($\phi=\phi(t)$). I will check the other options. $\endgroup$ Jun 1, 2018 at 2:34
  • $\begingroup$ 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. $\endgroup$
    – yarchik
    Jun 1, 2018 at 8:59
  • $\begingroup$ @yarchik It is not wrong. I know what you mean, but you are confused. I've checked the equation on the literature. $\endgroup$ Jun 1, 2018 at 20:47

1 Answer 1


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:


Now, this gives the variational derivative:

VariationalD[L, y[x, t], {x, t}]

enter image description here

and this yields the Euler equation:

EulerEquations[L, y[x, t], {x, t}]

enter image description here

Have fun!

  • $\begingroup$ Pretty cool! Eh? $\endgroup$ Jun 1, 2018 at 20:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.