## Spring mass damper


```python
from sympy import symbols
from sympy.physics.mechanics import dynamicsymbols, ReferenceFrame
from sympy.physics.mechanics import Point, Particle, KanesMethod
from pydy.codegen.ode_function_generators import generate_ode_function
from scipy.integrate import odeint
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
```


```python
q,u = dynamicsymbols('q u')
qd = dynamicsymbols('q', 1)
m, c, k = symbols('m c k')

```


```python
N = ReferenceFrame('N')
```


```python
P = Point('P')
P.set_vel(N, u * N.x)
```


```python
kinenematic_differential_equation = [qd - u]
force_vector_spring = (-k*q)*N.x
force_vector_damper = (-c*u)* N.x
force_vector_external = F*N.x
force_spring = (P, force_vector_spring)
force_damper = (P, force_vector_damper)
force_external = (P,force_vector_external)

forces = [force_spring,force_damper,force_external]


particle = Particle('pa', P, m)
bodies = [particle]
```


```python
coordinates = [q]
speeds = [u]

kane = KanesMethod(frame=N, q_ind=coordinates, u_ind=speeds, kd_eqs=kinenematic_differential_equation)
(fr, frstar) = kane.kanes_equations(bodies=bodies, loads=forces)

```


```python
mass_matrix = kane.mass_matrix
mass_matrix
```




$\displaystyle \left[\begin{matrix}m\end{matrix}\right]$




```python
kane.mass_matrix_full
```




$\displaystyle \left[\begin{matrix}1 & 0\\0 & m\end{matrix}\right]$




```python
forcing = kane.forcing
forcing
```




$\displaystyle \left[\begin{matrix}F - c u{\left(t \right)} - k q{\left(t \right)}\end{matrix}\right]$




```python
rhs = MM.inv() * forcing
rhs

```




$\displaystyle \left[\begin{matrix}\frac{F - c u{\left(t \right)} - k q{\left(t \right)}}{m}\end{matrix}\right]$




```python
kane.forcing_full
```




$\displaystyle \left[\begin{matrix}u{\left(t \right)}\\F - c u{\left(t \right)} - k q{\left(t \right)}\end{matrix}\right]$




```python
kane.mass_matrix_full.inv()
```




$\displaystyle \left[\begin{matrix}1 & 0\\0 & \frac{1}{m}\end{matrix}\right]$




```python
kane.mass_matrix_full.inv()*kane.forcing_full
```




$\displaystyle \left[\begin{matrix}u{\left(t \right)}\\\frac{F - c u{\left(t \right)} - k q{\left(t \right)}}{m}\end{matrix}\right]$




```python
constants = [m,c,k]

specified = [F]  # External force

right_hand_side = generate_ode_function(kane.forcing_full, coordinates,
                                        speeds, constants,
                                        mass_matrix=kane.mass_matrix_full,specifieds=specified)
```


```python
x0 = np.array([1,0])
t = 0.
F_ = 0.0
numerical_specified = np.array([F_])
m_ = 1
c_ = 0.1
k_ = 1
numerical_constants = np.array([m_,c_,k_])

right_hand_side(x0,t,numerical_specified, numerical_constants)
```




    array([ 0., -1.])




```python
t = np.linspace(0,10,100)

x0 = np.array([1,0])
F_ = 0.0
numerical_specified = np.array([F_])
m_ = 1
c_ = 0.1
k_ = 1
numerical_constants = np.array([m_,c_,k_])

df = pd.DataFrame(index=t)
y = odeint(right_hand_side, x0, t, args=(numerical_specified, numerical_constants))

df['motion'] = y[:,0]
df['velocity'] = y[:,1]

fig,ax = plt.subplots()
df.plot(y='motion', ax=ax);
ax.set_xlabel('time [s]')

fig,ax = plt.subplots()
df.plot(y='velocity', ax=ax);
ax.set_xlabel('time [s]')
```


```python
t = np.linspace(0,10,100)

x0 = np.array([1,0])
F_ = 0.3
numerical_specified = np.array([F_])
m_ = 1
c_ = 0.1
k_ = 1
numerical_constants = np.array([m_,c_,k_])

df = pd.DataFrame(index=t)
y = odeint(right_hand_side, x0, t, args=(numerical_specified, numerical_constants))

df['motion'] = y[:,0]
df['velocity'] = y[:,1]

fig,ax = plt.subplots()
df.plot(y='motion', ax=ax);
ax.set_xlabel('time [s]')

fig,ax = plt.subplots()
df.plot(y='velocity', ax=ax);
ax.set_xlabel('time [s]')
```


```python

```


```python

```


```python

```


```python

```

18cf03c5ac024f9e8123051d6a8135fde292266c
ipynb
Jupyter Notebook
spring_mass_damper.ipynb
axelande/rigidbodysimulator
a87c3eb3b7978ef01efca15e66a6de6518870cd8
MIT
spring_mass_damper.ipynb
axelande/rigidbodysimulator
a87c3eb3b7978ef01efca15e66a6de6518870cd8
MIT
2020-10-26T19:47:02.000Z
2020-10-26T19:47:02.000Z
spring_mass_damper.ipynb
axelande/rigidbodysimulator
a87c3eb3b7978ef01efca15e66a6de6518870cd8
MIT
2020-10-26T09:17:00.000Z
2020-10-26T09:17:00.000Z
Qwen/Qwen-72B
1. YES
2. YES