Cart - Pendulum - Spring System

L : equilibrium length of pendulum
L0: unstretched length of spring
M : mass of cart
m : mass of bob
k : spring constant
x : displacement of cart
s : length of pendulum
θ : angular displacement of pendulum

An interesting parameter set : g = 9.8; k = 5; L0 = 1.0; M = 4.0; m = 4; L = L0 + m g/k

Initial Conditions and Parameters

g=9.8; k=5;L0=1.0; M=4.0; m=4;L=L0+m g/k;
x0=0;s0=L+m g /k;θ0=π/6;xdot0=0;θdot0=0;sdot0=0;
Tpend=2 π Sqrt[L/g]; Tspring=2 π Sqrt[m/k];
Tsoln=60*Max[{Tpend,Tspring}];


Tpend/Tspring

Equations of Motion

xeqn=x''[t]==1/(M+m)(-m s''[t] Sin[θ[t]] - 2 m s'[t] θ'[t] Cos[θ[t]]-
m s[t] θ''[t] Cos[θ[t]]+m s[t] θ'[t]^2 Sin[θ[t]]);
θeqn= θ''[t]== 1/s[t] (- 2 s'[t] θ'[t] - x''[t] Cos[θ[t]] - g Sin[θ[t]]);
seqn=s''[t]== -  x''[t] Sin[θ[t]] +  s[t] θ'[t]^2 +  g Cos[θ[t]]-k/m (s[t]-L);

Solution

soln=NDSolve[{xeqn,θeqn,seqn,
x[0]==x0,s[0]==s0,θ[0]==θ0,
x'[0]==xdot0,θ'[0]==θdot0,s'[0]==sdot0},
{x,θ,s},{t,0,Tsoln},MaxSteps->100000];
Tplot=Tsoln;
xplt=Plot[Evaluate[x[t]/.soln],{t,0,Tplot},PlotStyle->Red,AxesLabel->{"Time","X(t)"}];
θplt=Plot[Evaluate[θ[t]/.soln],{t,0,Tplot},PlotStyle->Blue,AxesLabel->{"Time","θ(t)"}];
splt=Plot[Evaluate[s[t]-(L+m g /k)/.soln],{t,0,Tplot},PlotStyle->Green,
AxesLabel->{"Time","S(t)"}];
Show[GraphicsGrid[{{xplt},{θplt},{splt}}]]

Plot Normalized Displacements Together - Note Energy exchage between modes

Xmax=Max[Table[Evaluate[x[t]/.soln],{t,0,Tsoln}]];
θmax=Max[Table[Evaluate[θ[t]/.soln],{t,0,Tsoln}]];
Smax=Max[Table[Evaluate[s[t]-(L+ m g /k)/.soln],{t,0,Tsoln}]];

8.05984852770715`

Needs["PlotLegends`"]

Plot[Evaluate[{2 x[t]/Xmax-1.0,θ[t]/θmax,(s[t]-(L+ m g /k))/Smax}/.soln],{t,0,Tsoln},PlotStyle->{Red,Blue,Green},
PlotLegend->{"x(t)","θ(t)","s(t)"},LegendOrientation->Horizontal,LegendShadow->False]