Hello, I wanted to do a pendulum simulation and be able to replicate the patterns that are created by creating several pendulums at once. But for some reason after the first seconds, I cant see them. I tried to replicate something similar to the pendulum of Berger Dillon in Twitter, Snake pendulum - Berger Dillon. My script is shown below, but something seems a little bit off. I really don't know what could it be. Thanks!

g = 9.81;
initialAngle = \[Pi]/4;
nSpheres = 10;
radius = 1;
initialLength = 5;
time = 300;
timeStep = 0.25;

sol2[l_] := \[Theta] /. 
   NDSolve[{\[Theta]''[t] + g/l \[Theta][t] == 0, \[Theta][0] == 
      initialAngle, \[Theta]'[0] == 0}, \[Theta], {t, 0, 1000}] // 

timeList = Table[i, {i, 0, time, timeStep}];
xPos = Table[i, {i, 1, nSpheres}];

pos = Table[
  Map[(l + initialLength) {Sin[
       sol2[l + initialLength][#1]], -Cos[
        sol2[l + initialLength][#1]]} &, timeList], {l, 0, 
   nSpheres - 1}];

spheres[t_] := 
       Sphere[Join[pos[[i, t + 1]], {x}], radius]}], {i, 1, 
      nSpheres}][[j]] /. x -> xPos[[j]], {j, 1, Length[xPos]}]

lines[t_] := 
       Line[{{0, 0, x}, Join[pos[[i, t + 1]], {x}]}]}], {i, 1, 
      nSpheres}][[j]] /. x -> xPos[[j]], {j, 1, Length[xPos]}]

 Show[{lines[t], spheres[t]}, ViewPoint -> {0, 0, -2}, 
  Background -> Black, Boxed -> True, 
  PlotRange -> {{-15, 15}, {-15, 10}, {-5, 15}}], {t, 0, 
  Length[pos[[1]]] - 1, 1}]
  • $\begingroup$ Your code runs for me, but it takes a few minutes. The ODE really should be solved symbolically instead of numerically, like this sol2[l_] = θ /. DSolve[{θ''[t] + g/l θ[t] == 0, θ[0] == initialAngle, θ'[0] == 0}, θ, t] // First Don't use SetDelayed (:=). You may want to change Manipulate to Animate. $\endgroup$
    – LouisB
    Commented Apr 17, 2020 at 5:55

1 Answer 1


This can be done with just a couple lines and pretty quickly.

First define your ODE and your system parameters

ode = ϕ''[t] + g/l Sin[ϕ[t]] == 0;
params = Table[{g -> 9.81, l -> 11 - a}, {a, 1, 10}];

Make a Table of all the numerical solutions:

sols = Table[NDSolve[Evaluate[{ode, ϕ'[0] == 0, ϕ[0] == π/4} /. params[[i]]],
                     ϕ[t], {t, 0, 300}], {i, 1, Length[params]}] // Flatten

Define a vector for a parametric plot

v1 = {l Sin[ϕ[t]], -l Cos[ϕ[t]] };

A table for the graphics

gsol = Table[{Gray, Thin, 
Line[{{0, 0}, {l Sin[ϕ[t]], -l Cos[ϕ[t]]} /. sols[[a]] /. 
   params[[a]] }], Darker[Blue], Disk[{0, 0}, .1], Darker[Red], 
Disk[{l Sin[ϕ[t]], -l Cos[ϕ[t]]} /. sols[[a]] /. 
  params[[a]], .1]}, {a, 1, Length[params]}];

frames = Table[Graphics[gsol, PlotRange -> {{-9, 9}, {-10, 0.1}}], {t, 0, 10, .05}];

The 0.05 will define how smooth/how long your animation is.

something between 0.05 and 0.1 will probably be fine.

Export["snake.gif", frames] // AbsoluteTiming



took about 28 seconds on my system at 0.05.

If you want to make it a little more realistic, you can add dampening to your ODE:

ode = ϕ''[t] + g/l Sin[ϕ[t]] + 0.1 ϕ'[t] == 0;


Welcome to MMA, don't forget to vote up and mark any answers as accepted via the little arrows and checkmark beside them!

  • $\begingroup$ Than you a lot! Your script is much more efficient compared to mine. And they both seem to do the same thing but at Dillon's gif other patterns arise, like double snakes and so on. Am I missing something at the equations? Or the length of the strings should obey certain rules for the appearance of the rest of the patterns ? $\endgroup$ Commented Apr 17, 2020 at 18:10
  • $\begingroup$ @LeonardoFlores There are 3 things to say about his video compared to mine....The biggest is that it's hard to know if he did a linear approximation for his ODE, or used a nonlinear form...if he used an sort of dampening (even if it's very small), his initial conditions are unknown. his pendulum lengths are unknown, and his is titled and in 3d. While mine are all on the same plane a combination of all three is why his gets the visible effect that after a while they synchronise for a second or two. Playing with the ODE, the I.C., and lengths, approximating or not, will give a similar result. $\endgroup$ Commented Apr 17, 2020 at 18:42
  • $\begingroup$ @LeonardoFlores it's also unknown if his are coupled in any sort of way...I haven't done enough reading about it. $\endgroup$ Commented Apr 17, 2020 at 18:52
  • $\begingroup$ I happen to know where Dillon's script is for his animation. I'm still a beginner with Mathematica, but as far as I could see in his script he did the same approximation as you showed me without the dampening. The script is in his GitHub page twitter_gifs/pendulum_snake.nb $\endgroup$ Commented Apr 17, 2020 at 19:07
  • 1
    $\begingroup$ @LeonardoFlores I didn't approximate anything....though I'm surprised he did. However, his pendulums are very short compared to mine...all near the same length....play with the lengths and the results will be the same! $\endgroup$ Commented Apr 17, 2020 at 19:12

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.