I am trying to create some animations of classical mechanical systems in mathematica by using Lagrangians. I wanted to start off by doing the simple pendulum that is visually just a line, then adding more complex stuff like springs and moving supports. I am very new to mathematica but I have some experience with scheme and C. Any time I try to put {qx[t],qy[t]} as a 2-vector (in ParametricPlot or any Graphics environment) I get errors about wrongly sized arrays, but I tried to Join them and I still get errors. Could someone tell me what is wrong with my code? Thanks.

x[t_] = Sin [\[Theta][t]]
y[t_] = Cos[\[Theta][t]]

L = Simplify[(1/2) ((D[x[t], t])^2 + (D[y[t], t])^2) - 9.81*y[t]]

ELeqn = Simplify[D[L, \[Theta][t]] == D[D[L, D[\[Theta][t], t]], t]]

sol = NDSolve[{ELeqn , \[Theta][0] == Pi/4, \[Theta]'[0] == 
    0}, \[Theta][t], {t, 0, 10}]

qx[t_] = (x[t] /. sol)
qy[t_] = (y[t] /. sol)
framelist = Table[
   Graphics[{Line[{{0, 0}, {qx[t], qy[t]}}]}], {t, 0, 10, .1}];

1 Answer 1


I think you need something more like:

x[t_] = Sin[\[Theta][t]]
y[t_] = Cos[\[Theta][t]]

L = Simplify[(1/2) ((D[x[t], t])^2 + (D[y[t], t])^2) - 9.81*y[t]]

ELeqn = Simplify[D[L, \[Theta][t]] == D[D[L, D[\[Theta][t], t]], t]]

sol = First@NDSolve[{ELeqn, \[Theta][0] == Pi/4, \[Theta]'[0] == 
    0}, \[Theta][t], {t, 0, 10}]

qx = (\[Theta][t] /. sol)
qy = (\[Theta][t] /. sol)
framelist = Table[Graphics[{
     Line[{{0, 0}, {Sin@qx, Cos@qy}}]
    PlotRange -> {{-2, 2}, {-2, 2}}
    ], {t, 0, 10, .1}];

The main changes I made:

  • sol returns something that looks like {{theta(t) -> InterpolatingFunction[](t)}}. This extra set of curly braces causes issues, so I added First@NDSolve... to get rid of it.
  • Since it returns a function of $\theta$, x[t]/.sol and y[t]/.sol won't match the pattern and will not be replaced. They need to be theta[t]/.sol in order to match.
  • I removed the [t_] from qx and qy as they're not necessary since sol already has (t) at the end.
  • I added Sin and Cos to the positions of the lines since sol contains the angle, not the Cartesian position.
  • I added a PlotRange to the Graphics so that it doesn't try to resize the graphic each time.

Pendulum animation.

  • $\begingroup$ Thank you, I was really confused about the syntax and such and why things were not working. This is very helpful. $\endgroup$ Commented May 6, 2020 at 0:41
  • $\begingroup$ @inquisitivelearner Yes, for sure. For an early project, there are lots of things that are easy to get tripped up on when combining NDSolve along with replacements, plotting, animating, and various symbols. You were really close on your own! $\endgroup$
    – MassDefect
    Commented May 6, 2020 at 0:56
  • $\begingroup$ How would one do replacement rules, but with function composition? Such as having qx be x, but solved for the boundary conditions in NDsolve and all in terms of t? $\endgroup$ Commented May 6, 2020 at 1:03
  • $\begingroup$ @inquisitivelearner Do you mean having NDSolve return x and y instead of $\theta$? If so, I'm not completely sure how to do that. I've always solved pendulum problems in $\theta$. $\endgroup$
    – MassDefect
    Commented May 6, 2020 at 2:26
  • $\begingroup$ Nevermind, figured out what I was talking about. Thanks for the help, friend. $\endgroup$ Commented May 6, 2020 at 9:09

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.