# How to create a Fourier-Series for $f(x)=\begin{cases}\frac{1}{\pi}x-2\\ 4-\frac{1}{\pi}x\end{cases}$, $f:\mathbb{R}\to\mathbb{R}$

Let $$f(x)=\begin{cases}\frac{1}{\pi}x-2 \quad \text{ for } 2\pi\leq x < 3\pi \\ 4-\frac{1}{\pi}x \quad \text{ for } 3\pi \leq x < 4\pi\end{cases}$$, $$f:\mathbb{R}\to\mathbb{R}$$

and $$f(x+2\pi)=f(x)$$ for all $$x\in\mathbb{N}$$. How do I create a Fourier-Series for that function?

Solution/Problem:

The function is periodical for every $$2\pi$$. That's why $$T=2\pi$$. $$a_k=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cdot \cos(k\cdot x)dx,$$ $$k=0,1,2,3,\dots$$

$$b_k=\frac{1}{\pi}\int_{-\pi}^{\pi}f(x)\cdot \sin(k\cdot x) dx,$$ $$k=1,2,3,\dots$$ and

$$F_n(x)=\frac{a_0}{2}+\sum\limits_{k=1}^{n}\left(a_k\cos(k\cdot x)+b_k\cdot \sin(k\cdot x)\right)$$ is the Taylor-Polynomial, the Taylor-Series is $$F_\infty(x)=\frac{a_0}{2}+\sum\limits_{k=1}^{\infty}(a_k\cos(k\cdot x)+b_k\cdot \sin(k\cdot x)).$$

Because the function is periodical, we can change the integration-limits to $$2\pi$$, $$4\pi$$ or is this wrong? Afterwards, I've computed the integrals for $$a_k$$ and $$b_k$$ with $$k$$ arbitrary except for $$a_0$$, which needs to be computed too.

$$a_k=\frac{1}{\pi}\left(\displaystyle\int\limits_{2\pi}^{3\pi}\frac{1}{\pi}x-2\cdot \cos(kx)dx+\int\limits_{3\pi}^{4\pi}4-\frac{1}{\pi}x\cdot \cos(kx)dx\right)$$

$$a_k=\frac{2\sin(2\pi k)+\sin(3\pi k)-4\sin(4\pi k)}{\pi k}+\frac{13}{2}$$ for $$k>0$$ and $$a_0=1$$.

(I've left out the integration-steps, because that takes too mucht time to type in $$\LaTeX$$)

The computation for $$b_k$$ is similar except that we need to insert the other function and multiply by $$\sin(kx)$$.

$$b_k=\frac{\sin(3\pi k)-\sin(4\pi k)}{\pi^2k^2}+\frac{-2\cos(2\pi k)-\cos(3\pi k)+4\cos(4\pi k)}{\pi k}+\frac{13}{2}$$

and therefore, the Taylor-Polynomial is $$F_n(x)=\frac{1}{2}+\sum\limits_{n=1}^{n}(a_k\cdot \cos(kx)+b_k\cdot \sin(kx))dx$$ and the Series:

$$F_\infty(x)=\frac{1}{2}+\sum\limits_{n=1}^{\infty}(a_k\cdot \cos(kx)+b_k\cdot \sin(kx))dx,$$

which is a kinda weird solution and I've probably made some mistakes while computing. Can you tell me what I've done wrong and how to do it right?

The problem is unfortunately in the part you skipped: the integrals, here is the result you should arrive to

$$\begin{eqnarray} a_0 &=& 1 \\ a_k &=& \frac{1}{k^2\pi^2}[-\cos 2\pi k + 2 \cos 3\pi k - \cos 4\pi k] = \frac{2}{k^2\pi^2}(-1 + (-1)^k) ~~~ k = 1, 2, \cdots \\ b_k &=& 0 \end{eqnarray}$$

This is a plot that shows convergence for different number of terms in the series $$N$$ EDIT Code to generate the previous plot

import matplotlib.pyplot as plt
import numpy as np

# fourier series
def fseries(x, nmax = 10):

k = np.arange(1, nmax + 1)
s = (-2 + 2 * (-1)**k) * np.cos(x * k) / (k * np.pi)**2
return 0.5 + sum(s)

# original function
def f(x):

x = x[(x > 2 * np.pi) & (x < 4 * np.pi)]
y = np.zeros_like(x)

i = x < 3 * np.pi
y[i] = x[i] / np.pi - 2

i = x > 3 * np.pi
y[i] = 4 - x[i] / np.pi

return x, y

# axis
ax = plt.gca()
ax.set_xlabel(r'$$x/\pi$$')
ax.set_ylabel(r'$$f(x)$$')

# plot fourier series
x = np.linspace(0, 5 * np.pi, num = 500)
for n in [1, 5, 20]:
y = np.array([fseries(k, nmax = n) for k in x])
plt.plot(x / np.pi, y, lw = 1, label = r'$$N = {n}$$'.format(n = n))

# plot original function
x1 = np.linspace(0, 5 * np.pi, num = 500)
x1, y1 = f(x1)

plt.plot(x1 / np.pi, y1, lw = 3, label = r'$$f(x)$$')
ax.legend(frameon = False)
plt.show()

• Thank you, I found my error: I forgot the brackets because we're multiplying $f(x)$ with $\cos(kx)$. – Doesbaddel Dec 11 '19 at 21:07
• How did you do the plot? it looks pretty beautiful! – Doesbaddel Dec 12 '19 at 6:40
• @Doesbaddel It's python, I'd be happy to include some MWV of the script if you're interested – caverac Dec 12 '19 at 10:34
• Yeah, sure! I would like to see it. – Doesbaddel Dec 12 '19 at 10:44
• @Doesbaddel Done! – caverac Dec 12 '19 at 18:34