```python
import sympy as sy
sy.init_printing()
```


```python
M, m, g, l, u, w, z, theta, thetadot = sy.symbols('M,m,g,l,u,w,z,theta, thetadot')
```


```python
Mm = sy.Matrix([[M+m, -m*l*sy.cos(theta)], [-m*l*sy.cos(theta), m*l**2]])
Mm
```


```python
G = sy.Matrix([[m*l*sy.sin(theta)*thetadot**2],[m*g*l*sy.sin(theta)]])
G
```


```python
F = sy.Matrix([[u+w],[l*w]])
F
```


```python
acc = Mm.inv()*(F-G)
sy.simplify(acc.subs({w:0}))
```


```python
sy.simplify(sy.sin(theta+sy.pi))
```


```python
sy.simplify(sy.cos(theta+sy.pi))
```


```python
sy.simplify(sy.cos(-theta))
```


```python
theta2 = -theta+sy.pi
```


```python
sy.simplify(sy.cos(theta2))
```

## With Lagrangian mechanics


```python
from sympy.physics.mechanics import *
q1,q2 = dynamicsymbols('q1,q2')  # q1=z, q2=theta
q1d,q2d = dynamicsymbols('q1,q2',1)

zpdot = q1d -l*sy.cos(q2)*q2d
ypdot = l*sy.sin(q2)*q2d

T = 0.5*M*q1d**2 + 0.5*m*(zpdot**2 + ypdot**2)
sy.simplify(sy.expand(T))
```


```python
yp = -l*sy.cos(q2)
U = m*g*yp
L = T-U
L
```


```python
LM=LagrangesMethod(L, [q1,q2])
```


```python
sy.simplify(LM.form_lagranges_equations())
```


```python
sy.simplify(LM.mass_matrix)
```


```python
sy.simplify(LM.forcing)
```


```python
sy.simplify(LM.mass_matrix.inv()*LM.forcing)

```


```python

```

839f7612f55e0010d2973ac4149425f98260a134
ipynb
Jupyter Notebook
state-space/.ipynb_checkpoints/Inverted pendulum-checkpoint.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2020-11-07T05:20:37.000Z
2020-12-22T09:46:13.000Z
state-space/.ipynb_checkpoints/Inverted pendulum-checkpoint.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2020-06-12T20:44:41.000Z
2020-06-12T20:49:00.000Z
state-space/.ipynb_checkpoints/Inverted pendulum-checkpoint.ipynb
kjartan-at-tec/mr2007-computerized-control
16e35f5007f53870eaf344eea1165507505ab4aa
MIT
2021-03-14T03:55:27.000Z
2021-03-14T03:55:27.000Z
Qwen/Qwen-72B
1. YES
2. YES