Solving Lagrangian with Mathematica [closed]

I'm pretty new to Mathmatica but I'm trying to use it to find a Lagrangian and then find the equation of motion from the Lagrangian.

Think the method is correct but I just cant seem to get the syntax correct.. Know that it would be easier just to do this problem with pen and paper but I'm just trying to set up a script that I could use for harder problems.

Edit : Its working with simple Ke and Pe but when I tried to put in another more difficult equation for the Ke I started getting "Not a valid variable" errors that I dont understand as seen below. • Note: 1) underscores are used in pattern matching and are not allowed in symbol names; 2) please post code as formatted text as well, so people don't have to retype it. Jan 1 at 16:44
• You have to be careful to know what is your main degree of freedom here. It seems it is theta. Hence try this x[t_] := r*Cos[w*t] + L*Sin[theta[t]]; y[t_] := r*Sin[w*t] - L*Cos[theta[t]]; ke = 1/2*m*(D[x[t], t]^2 + D[y[t], t]^2); pe = m*g*x[t]; lag = ke - pe; ode = D[D[lag, theta'[t]], t] - D[lag, theta[t]] == 0 // Simplify In the derivation of the ode, you need to use theta[t] are your degree of freedom and not x[t]. Mathematica can do the calculations for you, but you need to do the Physics part and give it the correct equation. Jan 1 at 18:40
• Please post the code as formatted text that can be copy and pasted by those who wish to help you. As it is, the task to visually extract the code from your images will turn many away from helping you. It is quite a quick and easy task for you to provide such formatted code, while on the other side proves to be not so quick or easy. That said, I wish you good results with your tasks! Jan 1 at 20:38

You can't use NDSolve without initial conditions and with unknown numerical values for gravity.

You can obtain symbolic solution

Clear["Global*"]
Needs["VariationalMethods"]
ke = 1/2*m*x'[t]^2
pe = m*g*x[t]
lag = ke - pe;
ode = VariationalMethodsEulerEquations[lag, x[t], t]
sol = DSolve[ode, x[t], t] If you have initial conditions and replace value for g with number, then you can obtain a particular solution to plot. Something like

ic  = {x == 1, x' == 0};
sol = DSolve[{ode /. g -> 98/100, ic}, x[t], t] No need to use NDSolve here as DSolve can solve it analytically.

Plot[x[t] /. sol, {t, 0, 2}, AxesLabel -> {"time", "x(t)"},
BaseStyle -> 14, GridLines -> Automatic, GridLinesStyle -> LightGray,
PlotStyle -> Red, AxesOrigin -> {0, 0}] Is there a way to do this without the VariationalMethods'EulerEquations?

Yes ofcourse. Just apply the standard method to obtain the ode from the Lagrangian.

Clear["Global*"]
ke = 1/2*m*x'[t]^2
pe = m*g*x[t]
lag = ke - pe;

And now replace

ode = VariationalMethodsEulerEquations[lag, x[t], t]

with

ode = D[D[lag, x'[t]], t] - D[lag, x[t]] == 0

And now continue as before.

sol = DSolve[ode, x[t], t]

If you have an external force applied, you need to add this of course to the ode. But your example do not have one. Make sure to resolve the external force to the same direction as each degree of freedom you have. • Is there a way to do this without the VariationalMethods'EulerEquations? The main reason why I'm trying to get this working with a script in a step by step way is that I have an exam that was moved to online so I'm trying to do this step by step inside the script and I feel like the using VariationalMethodsEulerEquations would not be popular haha. Jan 1 at 16:44
• @Parko updated. Jan 1 at 16:57
• Works perfectly for the simple solution now but soon as I tried throwing something more complicated at it I got "General: ... is not a valid variable.", any ideas what is causing it? I replaced ke with D[L*Sin[theta[t]]] and it does not like it. Jan 1 at 18:05
• @Parko I do not know what you type. it is better to update your question and post what your typed in Plain text format. Make sure you start with clean kernel. note that ke is the kinetic energy. It is what you used T letter for in your post. D[L*Sin[theta[t]]] does not look like K.E. to me. Jan 1 at 18:12
• Thanks, I edited the problem and added the current code above as you suggested. Jan 1 at 18:27