## $$a_0 = \frac{2}{T}\int_a^b{f(x)}dx$$
## $$a_n =  \frac{2}{T}\int_a^b{f(x)\;cos(n\omega x)dx}$$
## $$b_n =  \frac{2}{T}\int_a^b{f(x)\;sin(n\omega x)dx}$$


```python
from sympy import *
import sympy as sp
import matplotlib.pyplot as plt
import numpy as np
import handcalcs.render
plt.style.use('dark_background')

x, t = symbols('x t', real = True)
n = symbols('n', real = True, positive = True, integer = True)
T = sp.symbols('T', real = True, positive = True)
a = symbols('a', real = True, positive = True, constant = True)
number_of_terms = 9
```

# Define function, free variable and range


```python
f = sp.Piecewise((x, x>0), (-x, x<0))
free_var = x
period = [-2, 2]
T = period[1]-period[0]
T
```




    4



# Calculation of coefficients


```python
a_0 = (2/T) * integrate(f, (free_var, period[0], period[1]))
a_0 = a_0.simplify()

a_n =(2/T) * (integrate(f*cos(n*2*pi*free_var/T), (free_var,period[0], period[1])))
a_n = a_n.simplify()

b_n =(2/T) * integrate(f*sin(n*2*pi/T*free_var), (free_var, period[0], period[1]))
b_n = b_n.simplify()
```


```python
a_0.factor()
a_0.factor()
```




$\displaystyle 2.0$




```python
%%render
a_0
a_n
b_n
```


\[
\begin{aligned}
a_{0} &= \displaystyle 2.0 \; 
\\[10pt]
a_{n} &= \displaystyle \frac{4.0 \left(\left(-1\right)^{n} - 1\right)}{\pi^{2} n^{2}} \; 
\\[10pt]
b_{n} &= \displaystyle 0 \; 
\end{aligned}
\]


# Calculation of whole series upto desired terms


```python
a_new = 0
b_new = 0
a1 = cos(n*free_var) * a_n
b1 = sin(n*free_var) * b_n
for i in range(number_of_terms,0, -1):
    a_new += a1.subs(n, i)
    b_new += b1.subs(n, i)
ser = (a_0/2) + a_new + b_new
```

# The whole series


```python
ser
```




$\displaystyle - \frac{8.0 \cos{\left(x \right)}}{\pi^{2}} - \frac{0.888888888888889 \cos{\left(3 x \right)}}{\pi^{2}} - \frac{0.32 \cos{\left(5 x \right)}}{\pi^{2}} - \frac{0.163265306122449 \cos{\left(7 x \right)}}{\pi^{2}} - \frac{0.0987654320987654 \cos{\left(9 x \right)}}{\pi^{2}} + 1.0$



ea4f6beee2ae99a67f636de0d9264073ed08aef1
ipynb
Jupyter Notebook
Fourier series.ipynb
ScientificArchisman/Mathematics
2aabf6627d39eb618abb428f1ef2a801b161a066
MIT
Fourier series.ipynb
ScientificArchisman/Mathematics
2aabf6627d39eb618abb428f1ef2a801b161a066
MIT
Fourier series.ipynb
ScientificArchisman/Mathematics
2aabf6627d39eb618abb428f1ef2a801b161a066
MIT
Qwen/Qwen-72B
1. YES
2. YES