I am trying to expand the following piecewise function as a cosine series:
$$ f(x)= \begin{cases} 3 & -7 < x < -1 \\ 8 & -1\leq x\leq 1 \\ 3 & 1 \leq x < 7 \end{cases} $$
The expansion should be in the form of: $$ f(x) = \frac{a_0}{2}+\sum_{n=1}^\infty{a_n\cos{\frac{n\pi}{p}x}} $$
My attempt at a solution: $$ 2a_0 = \frac{2}{L}\int_0^L f(x) \ dx \\ 2a_0 = \frac{2}{6}\int_1^7 3 \ dx + 2\int_{0}^1 8 \ dx \\ 2a_0 = 22 \\ a_0 = 11$$
For $a_n$: $$ a_n = \frac{2}{L}\int_0^L f(x) \cos{\frac{\pi nx}{L}} \ dx \\ a_n = \frac{2}{6}\int_1^7 3\cos{\frac{\pi nx}{6}} \ dx + 2\int_{0}^1 8\cos{\frac{\pi nx}{1}} \ dx \\ a_n = \frac{3\sin{\frac{7 \pi n}{6}}}{\pi n} - \frac{3\sin{\frac{\pi n}{6}}}{\pi n} + \frac{16\sin{\frac{\pi n}{6}}}{\pi n} $$
However, my solution is incorrect.. and I'm not sure what I'm doing wrong.
One thing you're doing wrong is that $L=7$ here, but you keep switching between using $6$ and $1$. Another thing, that isn't really wrong, but klutzy, is that you're not taking advantage of symmetry. You could use two integrals
$$\frac{2}{7}\int_0^1 \mbox{stuff} + \frac{2}{7} \int_1^7 \mbox{stuff}$$
instead of 3.