MGSwingingSpring2DPendulumKaneLagrange.html  (MotionGenesis input/output).
```   (1) % MotionGenesis file:  MGSwingingSpring2DPendulumKaneLagrange.txt
(2) % Problem: 2D Pendulum on a swinging spring (coupled nonlinear beat phenomenon).
(4) %--------------------------------------------------------------------
(5) NewtonianFrame  N            % Earth.
(6) Particle        Q            % Particle at end of spring.
(7) RigidFrame      B            % Vector basis with By> directed from Q to No.
(8) %--------------------------------------------------------------------
(9) Variable   y''               % Spring stretch (from natural length)
(10) Variable   q''               % Pendulum angle
(11) Constant   g = 9.8 m/s^2     % Earth's gravitational acceleration
(12) Constant   k = 100 N/m       % Linear spring  constant
(13) Constant   Ln = 0.3 m        % Natural length of spring
(14) Q.SetMass( m = 1 kg )
(15) %--------------------------------------------------------------------
(16) %       Rotational and translational kinematics.
(17) B.RotateZ( N,  q )
-> (18) B_N = [cos(q), sin(q), 0;  -sin(q), cos(q), 0;  0, 0, 1]
-> (19) w_B_N> = q'*Bz>
-> (20) alf_B_N> = q''*Bz>

(21) Q.Translate( No,  -(Ln+y)*By> )
-> (22) p_No_Q> = (-Ln-y)*By>
-> (23) v_Q_N> = (Ln+y)*q'*Bx> - y'*By>
-> (24) a_Q_N> = (2*q'*y'+(Ln+y)*q'')*Bx> + ((Ln+y)*q'^2-y'')*By>

(25) %--------------------------------------------------------------------
(26) %       Add gravity and spring forces to Q.
(27) Q.AddForce(  -m*g*Ny>  +  k*y*By> )
-> (28) Force_Q> = k*y*By> - m*g*Ny>

(29) %--------------------------------------------------------------------
(30) %       Equations of motion with Kane's method.
(31) SetGeneralizedSpeeds(  q',  y' )
(32) DynamicsKane = System.GetDynamicsKane()
-> (33) DynamicsKane[1] = m*(Ln+y)*(g*sin(q)+2*q'*y'+(Ln+y)*q'')
-> (34) DynamicsKane[2] = k*y + m*y'' - m*g*cos(q) - m*(Ln+y)*q'^2

(35) %--------------------------------------------------------------------
(36) %       Kinetic and potential energy (gravity and spring).
(37) KineticEnergy = System.GetKineticEnergy()
-> (38) KineticEnergy = 0.5*m*(y'^2+(Ln+y)^2*q'^2)

(39) PEGravity = Q.GetForceGravityPotentialEnergy( -g*Ny>, No )
-> (40) PEGravity = -m*g*cos(q)*(Ln+y)

(41) PotentialEnergy = PEGravity + 1/2*k*y^2
-> (42) PotentialEnergy = 0.5*k*y^2 + PEGravity

(43) TotalEnergy = KineticEnergy + PotentialEnergy
-> (44) TotalEnergy = PotentialEnergy + KineticEnergy

(45) %--------------------------------------------------------------------
(46) %       Equations of motion with Lagrange's method.
(47) SetGeneralizedCoordinates(  q,  y  )
(48) DynamicsLagrange = System.GetDynamicsLagrange( systemPotential = PotentialEnergy )
-> (49) DynamicsLagrange[1] = m*(Ln+y)*(g*sin(q)+2*q'*y'+(Ln+y)*q'')
-> (50) DynamicsLagrange[2] = k*y + m*y'' - m*g*cos(q) - m*(Ln+y)*q'^2

(51) Solve( DynamicsLagrange,  q'', y'' )
-> (52) q'' = -(g*sin(q)+2*q'*y')/(Ln+y)
-> (53) y'' = g*cos(q) + (Ln+y)*q'^2 - k*y/m

(54) %--------------------------------------------------------------------
(55) %       Integration parameters and initial values.
(56) Input  tFinal = 16 sec,  tStep = 0.01 sec,  absError = 1.0E-07
(57) Input  q = 1.0 deg,  q'=0 deg/sec,  y = 0.2 m,  y' = 0 m/s
(58) %--------------------------------------------------------------------
(59) %       List output quantities and solve ODEs.
(60) Output  t sec,  q deg,  y m,  TotalEnergy Joules
(61) ODE() MGSwingingSpring2DPendulumKaneLagrange

(62) %--------------------------------------------------------------------
```