This derives the equations of motion for the compound pendulum with a base attached to a vertical spring using both Kane's method and Langrange's method and compares the results of both.


```python
from sympy import symbols, solve, cos
from sympy.physics.mechanics import dynamicsymbols, ReferenceFrame, Point, Particle
from sympy.physics.mechanics import RigidBody, inertia, KanesMethod, init_vprinting, Lagrangian, LagrangesMethod
```


```python
init_vprinting()
```


```python
x, theta = dynamicsymbols('x, theta')
v, omega = dynamicsymbols('v, omega')
m, M, I, k, l, g = symbols('m, M, I, k, l, g')
```


```python
N = ReferenceFrame('N')
A = N.orientnew('A', 'Axis', (theta, N.z))
```


```python
O = Point('O')
a = O.locatenew('a', x * N.x)
b = a.locatenew('b', l * A.x)
```

# Kane's Method


```python
A.set_ang_vel(N, omega * N.z)
```


```python
O.set_vel(N, 0)
a.set_vel(N, v * N.x)
b.v2pt_theory(a, N, A)
```


```python
block = Particle('block', a, m)
bar = RigidBody('bar', b, A, M, (inertia(A, 0, 0, I), b))
```


```python
kane = KanesMethod(N, (x, theta), (v, omega), kd_eqs=(x.diff() - v, theta.diff() - omega))
fr, frstar = kane.kanes_equations(((a, -k * x * N.x + m * g * N.x), (b, M * g * N.x)), (block, bar))
```


```python
fr_plus_frstar = fr + frstar
fr_plus_frstar.simplify()
fr_plus_frstar
```


```python
rhs = kane.rhs()
rhs.simplify()
rhs
```

This gives the two equilibrium points:


```python
eq_points = solve(fr_plus_frstar.subs({omega.diff(): 0, v.diff(): 0, omega: 0, v: 0}), x, theta)
eq_points
```

Linearized equations of motion about the stable equilibrium point.


```python
A_mat, _, _ = kane.linearize(op_point=eq_points[0], new_method=True, A_and_B=True)
states = kane.q.col_join(kane.u)
states.diff() - A_mat * states 
A_mat * states
```

# Lagrange's Method


```python
A.set_ang_vel(N, theta.diff() * N.z)
a.set_vel(N, x.diff() * N.x)
b.v2pt_theory(a, N, A)
```


```python
b.vel(N).magnitude()
```


```python
bar.kinetic_energy(N) + block.kinetic_energy(N)
```


```python
block.potential_energy = k * x**2 / 2 - m * g * x
bar.potential_energy = -M * g * (x + l * cos(theta))
```


```python
L = Lagrangian(N, block, bar)
L
```


```python
L.diff(theta.diff()).simplify().diff(dynamicsymbols._t)
```


```python
lagrange = LagrangesMethod(L, (x, theta))
```


```python
lagranges_equations = lagrange.form_lagranges_equations()
lagranges_equations.simplify()
lagranges_equations
```

# Compare


```python
should_be_zero = fr_plus_frstar.subs({omega: theta.diff(), v: x.diff()}) + lagranges_equations
should_be_zero.simplify()
should_be_zero
```

bbda2fcc32cc7d8e33b6e539c54d93f5017e9ffe
ipynb
Jupyter Notebook
content/materials/notebooks/springy_compound_pendulum.ipynb
greg1877/eng122
3a9821f41c860e190cd3c8285fac212ae897337b
CC-BY-4.0
2020-05-11T05:56:54.000Z
2020-12-20T13:44:12.000Z
content/materials/notebooks/springy_compound_pendulum.ipynb
greg1877/eng122
3a9821f41c860e190cd3c8285fac212ae897337b
CC-BY-4.0
2016-09-20T01:26:33.000Z
2020-01-22T02:10:33.000Z
content/materials/notebooks/springy_compound_pendulum.ipynb
greg1877/eng122
3a9821f41c860e190cd3c8285fac212ae897337b
CC-BY-4.0
2017-06-09T01:28:45.000Z
2020-02-29T02:36:18.000Z
Qwen/Qwen-72B
1. YES
2. YES