home

PDF (letter size)

PDF (legal size)

Mathematica notebook

Animation of swinging pendulum on spring

Nasser M. Abbasi

February 22, 2025   Compiled on February 22, 2025 at 5:59pm

Let original length of pendulum be L and extension at instance shown be x. The position vector of bob is

r¯=(L+x)b¯1

Hence the velocity vector is

(ddtr¯)A=(ddtr¯)B+ω¯×r¯v¯A=(ddt(L+x)b¯1)B+ω¯×((L+x)b¯1)=x˙b¯1+θ˙b¯3×((L+x)b¯1)=x˙b¯1+θ˙(L+x)b¯2

And the acceleration is

(ddtv¯)A=(ddtv¯)B+ω¯×v¯

But

(ddtv¯)B=ddt(x˙b¯1+θ˙(L+x)b¯2)B(2)=x¨b¯1+(θ¨(L+x)+θ˙x˙)b¯2

And

ω¯×v¯=ω¯×(x˙b¯1+θ˙(L+x)b¯2)=θ˙b¯3×(x˙b¯1+θ˙(L+x)b¯2)(3)=θ˙x˙b¯2+θ˙x˙b¯2θ˙2(L+x)b¯1

Substituting (2,3) into (1) gives the acceleration of the bob in frame A as

a¯A=x¨b¯1+θ¨(L+x)b¯2+θ˙x˙b¯2+θ˙x˙b¯2θ˙2(L+x)b¯1=x¨b¯1+θ¨(L+x)b¯2+2θ˙x˙b¯2θ˙2(L+x)b¯1

To obtain F¯=ma¯ we now just need to find F¯. The force on bob is given by just the weight and the spring force acting on it. The spring force is proportional to spring coefficient k times the extension. Fs=kx. Hence the force vector is

F¯=mgcosθb¯1mgsinθb¯2kxb¯1

Therefore the equation of motion is

F¯=ma¯mgcosθb¯1mgsinθb¯2kxb¯1=m(x¨b¯1+θ¨(L+x)b¯2+2θ˙x˙b¯2θ˙2(L+x)b¯1)gcosθb¯1gsinθb¯2kmxb¯1=x¨b¯1+θ¨(L+x)b¯2+2θ˙x˙b¯2θ˙2(L+x)b¯1

Or, by equating each vector component

x¨θ˙2(L+x)+kmx=gcosθθ¨(L+x)+2θ˙x˙=gsinθ

Or

x¨=θ˙2(L+x)kmx+gcosθθ¨=2θ˙x˙L+xgL+xsinθ

Let initial conditions be L=1,x(0)=0.1,x˙(0)=0,θ(0)=200,θ˙(0)=.1 (rad/sec).

We can now solve for x,θ as function of time and make the animations. We can assume values for m,k and g=9.81.

The solution will give x(t),θ(t). To plot the path, we need to express x(t) in frame A coordinates of course which is the fixed frame. But this is easy, since the pendulum frame B is fixed at the base on the frame A origin.

The following is plot of the solution using k=0.99,m=0.09

The following is a quick animation of the above

PIC

The following is the code used

(*Nasser M. Abbasi, Jan 25, 2025*) 
 
SetDirectory[NotebookDirectory[]] 
 
L = 1; 
k = .99; 
m = .09; 
g = 9.81; 
ode1 = x''[t] == z'[t]^2*(L + x[t]) + g*Cos[z[t]] - k/m*x[t]; 
ode2 = z''[t] == -2 z'[t]*x'[t]/(L + x[t]) - g*Sin[z[t]]/(L + x[t]); 
IC = {x[0] == .1, x'[0] == 0, z[0] == 20 Degree, z'[0] == .1}; 
sol = NDSolve[{ode1, ode2, IC}, {x, z}, {t, 0, 100}] 
 
Manipulate[ 
 Module[{currentX, currentY}, 
  currentX = (L + Evaluate[x[t0] /. sol])*Sin[Evaluate[z[t0] /. sol]]; 
  currentY = (L + Evaluate[x[t0] /. sol])*Cos[Evaluate[z[t0] /. sol]]; 
  Grid[{ 
    {Graphics[{ 
       Line[{{0, 0}, {First@currentX, -First@currentY}}], 
       {Blue, Disk[{First@currentX, -First@currentY}, .1]} 
       }, 
      PlotRange -> {{-2, 2}, {-4, 1}}, GridLines -> Automatic, 
      GridLinesStyle -> LightGray 
      ]}}] 
  ], 
 {{t0, 0, "time"}, 0, 10, .01}, 
 TrackedSymbols :> {t0} 
 ]