0
$\begingroup$

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..

The code

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.

Updated code

$\endgroup$
3
  • 1
    $\begingroup$ 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. $\endgroup$
    – MarcoB
    Jan 1 at 16:44
  • $\begingroup$ 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. $\endgroup$
    – Nasser
    Jan 1 at 18:40
  • 4
    $\begingroup$ 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! $\endgroup$ Jan 1 at 20:38
4
$\begingroup$

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 = VariationalMethods`EulerEquations[lag, x[t], t]
sol = DSolve[ode, x[t], t]

Mathematica graphics

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[0] == 1, x'[0] == 0};
sol = DSolve[{ode /. g -> 98/100, ic}, x[t], t]

Mathematica graphics

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}]

Mathematica graphics

Update to answer comment

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 = VariationalMethods`EulerEquations[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.

Mathematica graphics

$\endgroup$
5
  • $\begingroup$ 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 VariationalMethods`EulerEquations would not be popular haha. $\endgroup$
    – Parko
    Jan 1 at 16:44
  • $\begingroup$ @Parko updated. $\endgroup$
    – Nasser
    Jan 1 at 16:57
  • $\begingroup$ 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. $\endgroup$
    – Parko
    Jan 1 at 18:05
  • $\begingroup$ @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. $\endgroup$
    – Nasser
    Jan 1 at 18:12
  • $\begingroup$ Thanks, I edited the problem and added the current code above as you suggested. $\endgroup$
    – Parko
    Jan 1 at 18:27

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