# Testing out SymPy dsolve


```python
%matplotlib inline
from sympy import init_printing
init_printing(use_latex='mathjax')

import sympy as sp
import matplotlib.pyplot as plt
import numpy as np
from sympy.plotting import plot
from scipy.integrate import cumtrapz
from sympy import sin,cos,Eq
```

## Problem
A ship should have:
* heading ($\psi$) varying as a sinus
* constant global X motion 
* no drift angle

how does the global Y motion look like to achieve this?


```python
t,X_dot,C1 = sp.symbols('t \dot{X} C1')
Y,psi = sp.symbols('Y psi',cls = sp.Function)
```


```python
psi = sp.pi/4*sin(t)
psi
```




$$\frac{\pi \sin{\left (t \right )}}{4}$$




```python
plot(psi,(t,0,10),title='Psi motion');
```

Local transverse velocity $v$ expressed in global coordinates:


```python
v = -X_dot*sin(psi) + Y(t).diff(t)*cos(psi)
v
```




$$- \dot{X} \sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} + \cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} \frac{d}{d t} Y{\left (t \right )}$$



This transverse velocity should always be zero (no drift angle), resulting the following ordinary differential equation:


```python
v_equation = Eq(v,0)
v_equation
```




$$- \dot{X} \sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} + \cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} \frac{d}{d t} Y{\left (t \right )} = 0$$



Use sympy to find the analytical solution to this equation:


```python
Y_motion = sp.dsolve(eq = v_equation)
Y_motion
```




$$Y{\left (t \right )} = C_{1} + \dot{X} \int \frac{\sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}}{\cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}}\, dt$$



Set $C_1$ to 0 and $\dot{X}=1$ (1 m/s)


```python
Y_motion_ = Y_motion.rhs.subs({C1:0,X_dot:1,psi:sin(t)})
Y_motion_
```




$$\int \frac{\sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}}{\cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}}\, dt$$




```python
Y_motion_ = sp.simplify(Y_motion_)
Y_motion_
```




$$\int \tan{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}\, dt$$




```python
plot(Y_motion_.args[0],(t,0,10),title = 'Y velocity');
```

Calculate $v$ with this solution to verify that v is 0


```python
v
```




$$- \dot{X} \sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} + \cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} \frac{d}{d t} Y{\left (t \right )}$$




```python
v_ = v.subs({X_dot:1,Y(t).diff(t):Y_motion_.args[0]})
v_
```




$$- \sin{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} + \cos{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )} \tan{\left (\frac{\pi \sin{\left (t \right )}}{4} \right )}$$




```python
sp.simplify(v_)
```




$$0$$




```python
plot(v_,(t,0,10),title = 'v veclocity is zero');
```

Sympy seems to be unable to solve the integral for Y motion analytically. The integral is instead solved numerically


```python
dY = sp.lambdify((t),Y_motion_.args[0])

t_ = np.linspace(0,10,100)
dYs = dY(t = t_)


```


```python
Ys = cumtrapz(dYs,t_)

fig,ax = plt.subplots()
ax.plot(t_[0:-1],Ys);
ax.set_title('Y motion')
ax.set_ylabel('Y [m]')
ax.set_xlabel('t [s]')
```

47f8a91bf234e53faecf6aa0fcb1bb1f70e61a73
ipynb
Jupyter Notebook
notebooks/01_dynamics_experiments/pure_yaw.ipynb
martinlarsalbert/ForceMan
b07541f1c04d0ed103d7a5f66256812ebca2824c
MIT
notebooks/01_dynamics_experiments/pure_yaw.ipynb
martinlarsalbert/ForceMan
b07541f1c04d0ed103d7a5f66256812ebca2824c
MIT
2019-12-15T17:23:21.000Z
2019-12-15T17:23:21.000Z
notebooks/01_dynamics_experiments/pure_yaw.ipynb
martinlarsalbert/ForceMan
b07541f1c04d0ed103d7a5f66256812ebca2824c
MIT
Qwen/Qwen-72B
1. YES
2. YES