# Trouble calculating Fourier series of a function

Calculate the Fourier series

f(x) = \left\{\begin{aligned} & 7\sin(x), && 0 \le x \le \pi\\ & 0, && \pi \le x \le 2\pi \end{aligned} \right.

I know that when $f(x+L) = f(x)$, for all real $x$, the Fourier series expansion is

$$f(x) = \dfrac{a_0}{2} + \sum_{n = 1}^{\infty} a_n \cos\left(n \tfrac{2\pi}{L}x \right) + b_n\sin\left(n \dfrac{2\pi}{L}x \right)$$

where

$$a_n = \dfrac{2}{L}\int\limits_{x_0}^{x_0+L}f(x)\cos\left( n \tfrac{2\pi}{L} x \right) dx \qquad x_0 \in \mathbb{R}$$

and $$b_n = \dfrac{2}{L}\int\limits_{x_0}^{x_0+L}f(x)\sin\left( n \tfrac{2\pi}{L} x \right) dx \qquad x_0 \in \mathbb{R}$$

But if we attempt to calculate $a_n$, we get $a_n = \dfrac{7}{\pi}\int^{\pi}_0 \sin(x)\cos(nx) dx$. For $n \ge 1$, I do not understand how to calculate this integral. Even If I use integration by parts, I still get an integral that is not solvable.

EDIT: To be honest, I am not even sure I got this part correct. We are supposed to have $-L \le x \le L$, but for this problem, we do not have a $-L$? Instead, we have $0 \le x \le 2\pi$. What is going on here? (This was corrected in the equations above.)

I would greatly appreciate it if people could please take the time to explain how to solve such a problem.

• Comments are not for extended discussion; this conversation has been moved to chat. – Daniel Fischer May 5 '17 at 11:40
• Shortcut: $f(x)-\frac{7}{2}\sin x=\frac{7}{2}\lvert\sin x\rvert$ is even, hence $b_1=\frac{7}{2}$ and $b_n=0$ for $n>1$. To compute the $a_n$, note that $\sin x\cos (nx)=\frac{1}{2}\Bigl(\sin\bigl((n+1)x\bigr)-\sin\bigl((n-1)x\bigr)\Bigr)$. Note that $\int_0^{\pi}\sin(kx)\,dx=0$ for even $k$, so $a_n=0$ for odd $n$. And for odd $k$, we have $$\int_0^{\pi}\sin(kx)\,dx=\frac{-\cos(kx)}{k}\biggr\rvert_0^{\pi}=\frac{1-(-1)^k}{k}=\frac{2}{k}$$ since $\cos(k\pi)=(-1)^k=-1$ then. So for even $n$, we have $$a_n=\frac{7}{\pi}\biggl(\frac{1}{n+1}-\frac{1}{n-1}\biggr)=-\frac{14}{\pi(n^2-1)}.$$ – Daniel Fischer May 5 '17 at 13:29
• You can simplify that. $\cos (\pi + n\pi) = \cos \bigl((n+1)\pi\bigr) = (-1)^{n+1}$. Similarly, $\cos (\pi - n\pi) = (-1)^{n-1} = (-1)^{n+1}$. So your numerator becomes $$2 - (-1)^{n+1}\bigl((1-n) + (1+n)\bigr) = 2 - (-1)^{n+1}\cdot 2 = 2\bigl(1 + (-1)^n\bigr).$$ Altogether, for odd $n$ you get $0$ (because $1 + (-1)^n = 0$ then), and for even $n$, since $1 + (-1)^n = 2$ then, you get $-\dfrac{7\cdot 4}{2\pi(n^2-1)}$. Which, when you simplify becomes exactly what MyGlasses and I got. – Daniel Fischer May 5 '17 at 13:49
• No, the sign of $1-n$ doesn't play a role, you have $2 -(-1)^{n+1}\bigl(1-n) + (1+n)\bigr)$. – Daniel Fischer May 5 '17 at 14:39
• Yes, but you have $2 - a(-1)^{n+1} - b(-1)^{n+1}$. Grouping the last two terms together, it's $a+b$ that matters. And $(1-n) + (1+n) = 2$ for all $n$. – Daniel Fischer May 5 '17 at 14:43

Hint: The Fourier series of a function $f$ in $[a,a+2\ell]$ with period $T=2\ell$ is $$f(x) = \dfrac{a_0}{2} + \sum_{i = 1}^{\infty} a_n \cos\left( \dfrac{n\pi x}{\ell} \right) + b_n\sin\left( \dfrac{n\pi x}{\ell} \right)$$ where \begin{eqnarray} a_0 &=& \dfrac{1}{\ell}\int_a^{a+2\ell}f(x) dx \\ a_n &=& \dfrac{1}{\ell}\int_a^{a+2\ell}f(x)\cos\left( \dfrac{n\pi x}{\ell} \right) dx \\ b_n &=& \dfrac{1}{\ell}\int_a^{a+2\ell}f(x)\sin\left( \dfrac{n\pi x}{\ell} \right) dx \end{eqnarray}
Edit: Here $a=0$ and $\ell=\pi$ then \begin{eqnarray} a_0 &=& \dfrac{1}{\pi}\int_0^\pi7\sin x\,dx\\ &=&\dfrac{7}{\pi}\int_0^\pi\sin x\,dx\\ &=&\color{blue}{\dfrac{14}{\pi}}\\ a_n&=&\dfrac{1}{\pi}\int_0^\pi7\sin x\cos nx\,dx\\ &=&\dfrac{7}{2\pi}\left(-\dfrac{\cos(1-n)x}{1-n}-\dfrac{\cos(1+n)x}{1+n}\right)_0^\pi\\ &=&\color{blue}{\dfrac{7}{\pi}\dfrac{(-1)^n+1}{1-n^2}} \end{eqnarray} for $n\neq1$ and when $n=1$ $$a_1=\dfrac{1}{\pi}\int_0^\pi7\sin x\cos x\,dx=\dfrac{7}{2\pi}\int_0^\pi\sin2x\,dx=\color{blue}{0}$$ also \begin{eqnarray} b_n &=&\dfrac{1}{\pi}\int_0^\pi7\sin x\sin nx\,dx\\ &=&\dfrac{-7}{2\pi}\int_0^\pi\cos(1+n)x-\cos(1-n)x\,dx\\ &=&\dfrac{-7}{2\pi}\left(\dfrac{\sin(1+n)x}{1+n}-\dfrac{\sin(1-n)x}{1-n}\right)_0^\pi\\ &=&\color{blue}{0}~,~(n>1)\\ b_1&=&\dfrac{1}{\pi}\int_0^\pi7\sin x\sin x\,dx\\ &=&\dfrac{7}{2\pi}\int_0^\pi1-\cos2 x\,dx\\ &=&\color{blue}{\dfrac{7}{2}} \end{eqnarray} thus $$\color{blue}{\boxed{f(x)=\dfrac{7}{\pi}+\dfrac{7}{2}\sin x+\dfrac{7}{\pi}\sum_{n=2}^\infty\dfrac{(-1)^n+1}{1-n^2}\cos nx}}$$ or $$\color{blue}{\boxed{f(x)=\dfrac{7}{\pi}+\dfrac{7}{2}\sin x-\dfrac{14}{\pi}\left(\dfrac{\cos 2x}{3}+\dfrac{\cos 4x}{15}+\dfrac{\cos 6x}{35}+\cdots\right)}}$$