TITLE: How to expand the Fourier series for $f(x)=\max \{0, \frac{\pi}{2}-\lvert x\rvert \} $?
QUESTION [6 upvotes]: My Question: My Goal is to determine the Fourier series for $f(x)=\max \{0, \frac{\pi}{2}-\lvert x\rvert \} \quad$ for $x \in [-\pi, \pi ]$ This function is $2\pi$-periodic. 
My Approach: i found out, $f(x)$ is an even function, because its graph is symmetric to the y-axis. The Expression: $\frac{\pi}{2}-\lvert x \rvert$ has to "zeros":
$\frac{\pi}{2}-\lvert x \rvert = 0$ 
$\frac{\pi}{2}= \lvert x \rvert  $
$x_{0}=-\frac{\pi}{2}$ 
$x_{1}=+\frac{\pi}{2}$ 
the graph must look somehow like that.. so the function is linear all over the Intervall:

But now Comes the hard stuff. I'm stuck in building the Fourier series for $f(x)$
The integral should be builded maybe this way, but i am not sure if if am right:
$\int (\frac{\pi}{2}-x)\cdot \cos (nx)\ dx = \frac{\pi}{2n}\sin (nx) - \frac{x}{n}\sin (nx) - \frac{1}{n^2}\cos (nx)$
What do you think, is that right? And what would be the next step? My textbook doesn't describes the further calculation.
p.s. edits for improving language and latex

REPLY [3 votes]: Since the function is even, the coefficients of $\sin nx$ are all zero.  The constant term is the average value of the function on $[-\pi,\pi]$:
$$
\frac{1}{2\pi}\int_{-\pi}^\pi f(x)\,dx\;=\; \frac{1}{2\pi}\left(\frac{\pi^2}{4}\right) \;=\; \frac{\pi}{8}
$$
(The integral here was evaluated using the formula for the area of a triangle.)
The coefficient of $\cos nx$ is given by the formula
$$
a_n \;=\; \frac{1}{\pi}\int_{-\pi}^\pi f(x)\,\cos nx\;dx
$$
Since $f(x)$ is nonzero only on $[-\pi/2,\pi/2]$, this is the same as
$$
a_n \;=\; \frac{1}{\pi}\int_{-\pi/2}^{\pi/2} f(x)\,\cos nx\;dx
$$
Moreover, since $f(x) \cos nx$ is an even function, we can restrict to $[0,\pi/2]$ and double the integral:
$$
a_n \;=\; \frac{2}{\pi}\int_0^{\pi/2} f(x)\,\cos nx\;dx \;=\; \frac{2}{\pi}\int_0^{\pi/2} \left(\frac{\pi}{2} - x\right)\,\cos nx\;dx
$$
The integral on the right can be evaluated using integration by parts.  The result is:
$$
a_n \;=\; \begin{cases}\dfrac{2}{\pi n^2} & \text{if }n\equiv 1,3\pmod{4}, \\[6pt] \dfrac{4}{\pi n^2} & \text{if }n\equiv 2\pmod 4, \\[6pt] 0 & \text{if }n\equiv 0 \pmod{4}.\end{cases}
$$
Thus
$$
f(x) \;=\; \frac{\pi}{8} + \frac{2}{\pi}\cos x + \frac{1}{\pi}\cos 2x  + \frac{2}{9\pi}\cos 3x + \frac{2}{25\pi}\cos 5x + \frac{1}{9\pi}\cos 6x + \cdots.
$$
In summation form,
$$
f(x) \;=\; \frac{\pi}{8} \,+\, \sum_{k=0}^\infty \frac{2}{\pi(2k+1)^2} \cos\bigl((2k+1)x\bigr) \,+\, \sum_{k=0}^\infty \frac{1}{\pi(2k+1)^2} \cos\bigl((4k+2)x\bigr).
$$
By the way, the following animation shows thee convergence of this Fourier series for the first twelve terms: