<jupyter_start><jupyter_text>Differetial Equations **First Order ODEs**Air friction while falling$$ v' - \alpha v^2 + \beta = 0 \hspace{10mm} v(0) = 0$$<jupyter_code>from scipy.integrate import odeint<jupyter_output><empty_output><jupyter_text>All information about differential equation<jupyter_code>def dvdt(v, t):
    return 3*v**2 - 5
v0 = 0<jupyter_output><empty_output><jupyter_text>Solve differential equation<jupyter_code>t = np.linspace(0, 1, 100)
sol = odeint(dvdt, v0, t)
sol
v_sol = sol.T[0]
plt.plot(t, v_sol)<jupyter_output><empty_output><jupyter_text>**Coupled first order ODEs**$$ y_1' = y_1 + y_2^2 + 3x \hspace{10mm} y_1(0)=0$$$$ y_2' = 3y_1 + y_2^3 - \cos(x) \hspace{10mm} y_2(0)=0$$Letting $S=(y_1, y_2)$ we need to write a function that returns $dS/dx = (dy_1/dx, dy_2/dx)$. The function $dS/dx$ can take in $S=(y_1, y_2)$ and $x$<jupyter_code>def dSdx(S, x):
    y1, y2 = S
    return [y1 + y2**2  + 3*x,
           3*y1 + y2**3 - np.cos(x)]
y1_0 = 0
y2_0 = 0
S_0 = (y1_0, y2_0)
x = np.linspace(0, 1, 100)
sol = odeint(dSdx, S_0, x)
sol
y1_sol = sol.T[0]
y2_sol = sol.T[1]
plt.plot(x, y1_sol)
plt.plot(x, y2_sol)<jupyter_output><empty_output><jupyter_text>**Second Order ODEs**Equation for a pendulum$$\theta'' - \sin(\theta) = 0$$Scipy can only solve coupled first order ODEs, but **any second order ODE can be turned into two coupled first order ODEs**. The same thing goes for higher order ODEs. Define $\omega = d\theta/dt$ so that one has the following coupled ODEs$$d \omega / dt = \sin(\theta)$$$$d \theta / dt = \omega $$Let $S = (\theta, \omega)$<jupyter_code>def dSdt(S, t):
    theta, omega = S
    return [omega,
           np.sin(theta)]
theta0 = np.pi/4
omega0 = 0

S0 = (theta0, omega0)
t = np.linspace(0, 20, 100)
sol = odeint(dSdt, S0, t)
theta, omega = sol.T
plt.plot(t, theta)
plt.show()<jupyter_output><empty_output><jupyter_text>Fourier Transforms The discrete Fourier transform is defined as$$y[k] = \sum_{n=0}^{N-1} e^{-2 \pi i n (k / N)} x[n] $$* $k/N$ represents a specific frequency (dimensionless)* can be converted to a frequency (Hz) if you know the spacing in $x$<jupyter_code>x = np.linspace(0, 10*np.pi, 100)
y = np.sin(2*np.pi*x) + np.sin(4*np.pi*x) + 0.1*np.random.randn(len(x))
plt.plot(x, y)
from scipy.fft import fft, fftfreq
N = len(y)
yf = fft(y)[:N//2]
xf = fftfreq(N, np.diff(x)[0])[:N//2]
plt.plot(xf, np.abs(yf))
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17576509511993382, 'fraction_numerical': 0.04466501240694789, 'mean_word_length': 4.548165137614679, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '27540', 'license': 'Unlicense', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1069, 'n_words': 264, 'path': 'day4/03. Scipy - ODE-FT.ipynb', 'repo_name': 'ubutnux/bosscha-python-workshop-2022', 'size': '2418'}