Euler-Lagrange formalism with SymPy
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
add a comment |
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
add a comment |
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
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
euler-lagrange-equation python
edited Nov 26 at 20:23
gt6989b
32.9k22452
32.9k22452
asked Nov 26 at 20:21
Henrique Ferrolho
1233
1233
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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
I am using Python 3, so the1 / 2
should not pose a problem. I guess what I did wrong was really thediff(diff(L, xd), 't')
. Thank you for your answer!
– Henrique Ferrolho
Nov 27 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
I am using Python 3, so the1 / 2
should not pose a problem. I guess what I did wrong was really thediff(diff(L, xd), 't')
. Thank you for your answer!
– Henrique Ferrolho
Nov 27 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
add a comment |
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
I am using Python 3, so the1 / 2
should not pose a problem. I guess what I did wrong was really thediff(diff(L, xd), 't')
. Thank you for your answer!
– Henrique Ferrolho
Nov 27 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
add a comment |
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
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
answered Nov 26 at 21:02
caverac
13.3k21029
13.3k21029
I am using Python 3, so the1 / 2
should not pose a problem. I guess what I did wrong was really thediff(diff(L, xd), 't')
. Thank you for your answer!
– Henrique Ferrolho
Nov 27 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
add a comment |
I am using Python 3, so the1 / 2
should not pose a problem. I guess what I did wrong was really thediff(diff(L, xd), 't')
. Thank you for your answer!
– Henrique Ferrolho
Nov 27 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 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 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 at 0:15
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
@HenriqueFerrolho happy to help
– caverac
Nov 27 at 0:43
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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