# Fourier Series
## $$ f(x) = a_0 + \sum_{n=1}^{\infty}\Big(a_n\cos(nx) + b_n \sin(nx) \Big)$$

$$ a_0 = \frac{1}{2\pi} \int_{-\pi}^{\pi} f(x)dx $$

$$ a_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)cos(dx) $$

$$ b_n = \frac{1}{\pi} \int_{-\pi}^{\pi} f(x)sin(dx) $$

# $$x:v = p:2\pi$$


```python

import sympy as sp
import numpy as np
import matplotlib.pyplot as plt
%matplotlib widget

x = np.linspace(-2*np.pi, 2*np.pi, 100)
y = np.sin(x)*np.cos(x)
plt.plot(x,np.cos(x),label=r'$\cos(x)$')
#plt.plot(x,np.cos(x)*np.cos(x),label=r'$\cos(x)\cos(x)$')

plt.plot(x,np.sin(x),label=r'$\sin(x)$')
#plt.plot(x,np.sin(x)*np.sin(2*x),label=r'$\sin(x)\sin(x)$')

plt.plot(x,y,label=r'$\cos(x)\sin(x)$')
plt.legend()
plt.show()
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>




```python

```


```python

```


```python
x, y, z, t = sp.symbols('x y z t')
x= sp.Piecewise((-1, t<0), (1,t>0))
ser = sp.fourier_series(x, (t, -sp.pi, sp.pi))
```


```python
ser.truncate(5)
```




$\displaystyle \frac{4 \sin{\left(t \right)}}{\pi} + \frac{4 \sin{\left(3 t \right)}}{3 \pi} + \frac{4 \sin{\left(5 t \right)}}{5 \pi} + \frac{4 \sin{\left(7 t \right)}}{7 \pi} + \frac{4 \sin{\left(9 t \right)}}{9 \pi}$




```python
sp.plot(ser.truncate(5), (t, -sp.pi, sp.pi))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc99b0e2bb0>




```python
sp.plot(ser.truncate(20), (t, -sp.pi, sp.pi))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc9983f5c10>




```python
x1 = sp.Piecewise( (-t - 2, ((t >= -2) & (t <= -1))),
                   ( t,     ((t >= -1) & (t <=  1))),
                   (-t + 2, ((t >=  1) & (t <=  2))) )
ser1 = sp.fourier_series(x1, (t, -2, 2))
```


```python
sp.plot(ser1.truncate(20))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc9992f8670>




```python
x2 = sp.Piecewise((5/sp.pi*t + 5, ((0 < t) & (t < 2*sp.pi))))
ser2 = sp.fourier_series(x2, (0, 2*sp.pi))
sp.plot(ser2.truncate(5))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc998eb9d60>




```python
x3 = -(5/sp.pi)*t +5
ser3 = sp.fourier_series(x3, (t, 0,2*sp.pi))
```


```python
sp.plot(ser3.truncate(20))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc99d76ac70>




```python
x4 = sp.sin(t)
ser4 = sp.fourier_series(x4,(t,0,sp.pi))
sp.plot(ser4.truncate(20))
```



<div style="display: inline-block;">
    <div class="jupyter-widgets widget-label" style="text-align: center;">
        Figure
    </div>
    
</div>






    <sympy.plotting.plot.Plot at 0x7fc99904bee0>




```python

```

77649a698b2e6fc4071a904852a9f14413efa8ea
ipynb
Jupyter Notebook
python/matplotlib/Fourier_Series/sympy_fourier_series.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/matplotlib/Fourier_Series/sympy_fourier_series.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
python/matplotlib/Fourier_Series/sympy_fourier_series.ipynb
karng87/nasm_game
a97fdb09459efffc561d2122058c348c93f1dc87
MIT
Qwen/Qwen-72B
1. YES
2. YES