Fourier series of $f(x)=x\cos(x)$

I'd like to calculate the fourier series of $f(x)=x\cos(x)$, with $x\in(-\pi,\,\pi)$.

My solution, however, doesn't agree with my teacher's solution. So either I went wrong somewhere (most likely), or it was him who went wrong (but I don't think so).

So, $$f(x)=a_0+\sum_{n=1}^\infty\left(a_n\cos(nx)+b_n\sin(nx)\right)$$

I start by realising that $f(x)$ is an odd function, since $f(-x)=-f(x)$, and therefore the coefficients $a_0$ and $a_n$ will both equal $0$: $$a_0=a_n=0$$ Therefore, I'm left with: $$f(x)=\sum_{n=1}^\infty b_n\sin(nx)$$ And all I have to do now is calculate the coefficients $b_n$, and this is exactly where I'm going wrong.

Firstly I'll present my teacher's solution: $\boxed{b_n=\dfrac{2(-1)^{n+1}}{n^2-1}}$ ($n\neq1$)

Now I'll show you what I'm doing: $$b_n=\dfrac2\pi\int_0^\pi x\cos(x)\sin(nx)dx$$ Integrating by parts: $$\begin{cases} u = x & \Rightarrow du=dx\\ dv = \cos(x)\sin(nx)dx & \Rightarrow v=-\dfrac12\left(\dfrac{\cos x(n+1)}{n+1}+\dfrac{\cos x(n-1)}{n-1}\right) \end{cases}$$ Where $v$ was obtained via integration by parts. So: $$b_n=\dfrac2\pi\left(uv\vert_0^\pi-\int_0^\pi vdu\right)$$ Where the last integral equals $0$. Therefore I get: $$\boxed{b_n=\dfrac{2n(-1)^{n+1}}{1-n^2}}\,(n\neq1)$$ Because: $\cos [\pi(n+1)] = \cos [\pi(n-1)] = (-1)^{n+1}$

I don't know where I went wrong... In particular notice that in my solution I have an $n$ in the numerator, its sign is different from my teacher's solution.

Any hints? Thanks in advance.


I agree with Math Lover, I don't see anything wrong with your solution. Perhaps you should ask you teacher how they obtained theirs? It appears like theirs is wrong.

Wolfram Alpha gives the first 3 terms of this series to be $$-\frac{\sin x}2+\frac43\sin 2x-\frac34\sin 3x+\cdots$$ i.e. the coefficients are $$b_1=-\frac12; \,\,b_2=\frac43;\,\,b_3=-\frac34.$$ Your teacher's solution gives $$b_2=-\frac23;\,\,b_3=\frac14,$$which disagree, while yours give the correct coefficients for $b_2,b_3$.