Euler-Lagrange formalism with SymPy












3














I am trying to use SymPy to apply the Euler-Lagrange formalism to the following Lagrangian function:



$$mathcal{L}(x, dot{x}) = mathcal{K}(x, dot{x}) - mathcal{P}(x) = dfrac{1}{2} mathfrak{m} dot{x} - mathfrak{m} g x.$$



The result should be



$$
f = frac{d}{dt} frac{partialmathcal{L}}{partialdot{x}}
- frac{partialmathcal{L}}{partial x}
= mathfrak{m} ddot{x} + mathfrak{m} g.$$



However, I get $f = mathfrak{m} g$ with SymPy...



Below is the source of my jupyter notebook.



In [1]: from sympy import *
from sympy.physics.mechanics import *

In [2]: m,g = symbols('m g', real=True)
x = dynamicsymbols('x')
xd = dynamicsymbols('x', 1)
xdd = dynamicsymbols('x', 2)

In [3]: kin_energy = 1/2 * m * xd ** 2
pot_energy = m * g * x

In [4]: L = kin_energy - pot_energy
f = diff(L, xdd) - diff(L, x)

In [5]: pprint(f)
Out[5]: g⋅m









share|cite|improve this question





























    3














    I am trying to use SymPy to apply the Euler-Lagrange formalism to the following Lagrangian function:



    $$mathcal{L}(x, dot{x}) = mathcal{K}(x, dot{x}) - mathcal{P}(x) = dfrac{1}{2} mathfrak{m} dot{x} - mathfrak{m} g x.$$



    The result should be



    $$
    f = frac{d}{dt} frac{partialmathcal{L}}{partialdot{x}}
    - frac{partialmathcal{L}}{partial x}
    = mathfrak{m} ddot{x} + mathfrak{m} g.$$



    However, I get $f = mathfrak{m} g$ with SymPy...



    Below is the source of my jupyter notebook.



    In [1]: from sympy import *
    from sympy.physics.mechanics import *

    In [2]: m,g = symbols('m g', real=True)
    x = dynamicsymbols('x')
    xd = dynamicsymbols('x', 1)
    xdd = dynamicsymbols('x', 2)

    In [3]: kin_energy = 1/2 * m * xd ** 2
    pot_energy = m * g * x

    In [4]: L = kin_energy - pot_energy
    f = diff(L, xdd) - diff(L, x)

    In [5]: pprint(f)
    Out[5]: g⋅m









    share|cite|improve this question



























      3












      3








      3







      I am trying to use SymPy to apply the Euler-Lagrange formalism to the following Lagrangian function:



      $$mathcal{L}(x, dot{x}) = mathcal{K}(x, dot{x}) - mathcal{P}(x) = dfrac{1}{2} mathfrak{m} dot{x} - mathfrak{m} g x.$$



      The result should be



      $$
      f = frac{d}{dt} frac{partialmathcal{L}}{partialdot{x}}
      - frac{partialmathcal{L}}{partial x}
      = mathfrak{m} ddot{x} + mathfrak{m} g.$$



      However, I get $f = mathfrak{m} g$ with SymPy...



      Below is the source of my jupyter notebook.



      In [1]: from sympy import *
      from sympy.physics.mechanics import *

      In [2]: m,g = symbols('m g', real=True)
      x = dynamicsymbols('x')
      xd = dynamicsymbols('x', 1)
      xdd = dynamicsymbols('x', 2)

      In [3]: kin_energy = 1/2 * m * xd ** 2
      pot_energy = m * g * x

      In [4]: L = kin_energy - pot_energy
      f = diff(L, xdd) - diff(L, x)

      In [5]: pprint(f)
      Out[5]: g⋅m









      share|cite|improve this question















      I am trying to use SymPy to apply the Euler-Lagrange formalism to the following Lagrangian function:



      $$mathcal{L}(x, dot{x}) = mathcal{K}(x, dot{x}) - mathcal{P}(x) = dfrac{1}{2} mathfrak{m} dot{x} - mathfrak{m} g x.$$



      The result should be



      $$
      f = frac{d}{dt} frac{partialmathcal{L}}{partialdot{x}}
      - frac{partialmathcal{L}}{partial x}
      = mathfrak{m} ddot{x} + mathfrak{m} g.$$



      However, I get $f = mathfrak{m} g$ with SymPy...



      Below is the source of my jupyter notebook.



      In [1]: from sympy import *
      from sympy.physics.mechanics import *

      In [2]: m,g = symbols('m g', real=True)
      x = dynamicsymbols('x')
      xd = dynamicsymbols('x', 1)
      xdd = dynamicsymbols('x', 2)

      In [3]: kin_energy = 1/2 * m * xd ** 2
      pot_energy = m * g * x

      In [4]: L = kin_energy - pot_energy
      f = diff(L, xdd) - diff(L, x)

      In [5]: pprint(f)
      Out[5]: g⋅m






      euler-lagrange-equation python






      share|cite|improve this question















      share|cite|improve this question













      share|cite|improve this question




      share|cite|improve this question








      edited Nov 26 '18 at 20:23









      gt6989b

      33.1k22452




      33.1k22452










      asked Nov 26 '18 at 20:21









      Henrique Ferrolho

      1233




      1233






















          1 Answer
          1






          active

          oldest

          votes


















          2














          There are a couple of errors in your code




          1/2




          Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use



          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x



          ${rm d}/{rm d}t(partial L/ partial dot{x})$




          Note that



          $$
          frac{{rm d}}{{rm d}t}frac{partial mathcal{L}}{partial dot{x}} not= frac{partial mathcal{L}}{partial ddot{x}}
          $$



          If you implement these two things, you should get



          m = symbols('m', real = True)
          g = symbols('g', real = True)
          x = dynamicsymbols('x')
          xd = dynamicsymbols('x', 1)

          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x

          L = kin_energy - pot_energy
          f = diff(diff(L, xd), 't') - diff(L, x)
          pprint(f)

          >>> 2
          d
          g⋅m + 1.0⋅m⋅───(x(t))
          2
          dt





          share|cite|improve this answer





















          • I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
            – Henrique Ferrolho
            Nov 27 '18 at 0:15










          • @HenriqueFerrolho happy to help
            – caverac
            Nov 27 '18 at 0:43











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "69"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          noCode: true, onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3014868%2feuler-lagrange-formalism-with-sympy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          There are a couple of errors in your code




          1/2




          Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use



          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x



          ${rm d}/{rm d}t(partial L/ partial dot{x})$




          Note that



          $$
          frac{{rm d}}{{rm d}t}frac{partial mathcal{L}}{partial dot{x}} not= frac{partial mathcal{L}}{partial ddot{x}}
          $$



          If you implement these two things, you should get



          m = symbols('m', real = True)
          g = symbols('g', real = True)
          x = dynamicsymbols('x')
          xd = dynamicsymbols('x', 1)

          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x

          L = kin_energy - pot_energy
          f = diff(diff(L, xd), 't') - diff(L, x)
          pprint(f)

          >>> 2
          d
          g⋅m + 1.0⋅m⋅───(x(t))
          2
          dt





          share|cite|improve this answer





















          • I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
            – Henrique Ferrolho
            Nov 27 '18 at 0:15










          • @HenriqueFerrolho happy to help
            – caverac
            Nov 27 '18 at 0:43
















          2














          There are a couple of errors in your code




          1/2




          Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use



          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x



          ${rm d}/{rm d}t(partial L/ partial dot{x})$




          Note that



          $$
          frac{{rm d}}{{rm d}t}frac{partial mathcal{L}}{partial dot{x}} not= frac{partial mathcal{L}}{partial ddot{x}}
          $$



          If you implement these two things, you should get



          m = symbols('m', real = True)
          g = symbols('g', real = True)
          x = dynamicsymbols('x')
          xd = dynamicsymbols('x', 1)

          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x

          L = kin_energy - pot_energy
          f = diff(diff(L, xd), 't') - diff(L, x)
          pprint(f)

          >>> 2
          d
          g⋅m + 1.0⋅m⋅───(x(t))
          2
          dt





          share|cite|improve this answer





















          • I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
            – Henrique Ferrolho
            Nov 27 '18 at 0:15










          • @HenriqueFerrolho happy to help
            – caverac
            Nov 27 '18 at 0:43














          2












          2








          2






          There are a couple of errors in your code




          1/2




          Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use



          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x



          ${rm d}/{rm d}t(partial L/ partial dot{x})$




          Note that



          $$
          frac{{rm d}}{{rm d}t}frac{partial mathcal{L}}{partial dot{x}} not= frac{partial mathcal{L}}{partial ddot{x}}
          $$



          If you implement these two things, you should get



          m = symbols('m', real = True)
          g = symbols('g', real = True)
          x = dynamicsymbols('x')
          xd = dynamicsymbols('x', 1)

          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x

          L = kin_energy - pot_energy
          f = diff(diff(L, xd), 't') - diff(L, x)
          pprint(f)

          >>> 2
          d
          g⋅m + 1.0⋅m⋅───(x(t))
          2
          dt





          share|cite|improve this answer












          There are a couple of errors in your code




          1/2




          Depending on the version of python you're using, this could be interpreted as either '0' or '1/2'. So To avoid problems just use



          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x



          ${rm d}/{rm d}t(partial L/ partial dot{x})$




          Note that



          $$
          frac{{rm d}}{{rm d}t}frac{partial mathcal{L}}{partial dot{x}} not= frac{partial mathcal{L}}{partial ddot{x}}
          $$



          If you implement these two things, you should get



          m = symbols('m', real = True)
          g = symbols('g', real = True)
          x = dynamicsymbols('x')
          xd = dynamicsymbols('x', 1)

          kin_energy = 0.5 * m * xd ** 2
          pot_energy = m * g * x

          L = kin_energy - pot_energy
          f = diff(diff(L, xd), 't') - diff(L, x)
          pprint(f)

          >>> 2
          d
          g⋅m + 1.0⋅m⋅───(x(t))
          2
          dt






          share|cite|improve this answer












          share|cite|improve this answer



          share|cite|improve this answer










          answered Nov 26 '18 at 21:02









          caverac

          13.8k21130




          13.8k21130












          • I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
            – Henrique Ferrolho
            Nov 27 '18 at 0:15










          • @HenriqueFerrolho happy to help
            – caverac
            Nov 27 '18 at 0:43


















          • I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
            – Henrique Ferrolho
            Nov 27 '18 at 0:15










          • @HenriqueFerrolho happy to help
            – caverac
            Nov 27 '18 at 0:43
















          I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
          – Henrique Ferrolho
          Nov 27 '18 at 0:15




          I am using Python 3, so the 1 / 2 should not pose a problem. I guess what I did wrong was really the diff(diff(L, xd), 't'). Thank you for your answer!
          – Henrique Ferrolho
          Nov 27 '18 at 0:15












          @HenriqueFerrolho happy to help
          – caverac
          Nov 27 '18 at 0:43




          @HenriqueFerrolho happy to help
          – caverac
          Nov 27 '18 at 0:43


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Mathematics Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3014868%2feuler-lagrange-formalism-with-sympy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Plaza Victoria

          In PowerPoint, is there a keyboard shortcut for bulleted / numbered list?

          How to put 3 figures in Latex with 2 figures side by side and 1 below these side by side images but in...