Theorem: Fourier Series/x over 0 to 2, x-2 over 2 to 4

Let $\map f x$ be the real function defined on $\openint 0 4$ as:
$\quad \map f x = \begin{cases}
x & : 0 < x \le 2 \\
x - 2 & : 2 < x < 4 \end{cases}$
Then its Fourier series can be expressed as:
:$\ds \map f x \sim 1 + \frac 4 \pi \sum_{n \mathop = 1}^\infty \frac {\paren {-1}^{r - 1} } {2 r - 1} \paren {1 + \frac {4 \paren {-1}^r} {\paren {2 r - 1} \pi} } \cos \frac {\paren {2 r - 1} \pi x} 4$

Proof:
Let $\map f x$ be the function defined as:
$\quad \forall x \in \openint 0 4: \begin{cases}
x & : 0 < x \le 2 \\
x - 2 & : 2 < x < 4 \end{cases}$
Let $f$ be expressed by a half-range Fourier cosine series:
:$\ds \map f x \sim \frac {a_0} 2 + \sum_{n \mathop = 1}^\infty a_n \cos \frac {n \pi x} 4$
where for all $n \in \Z_{> 0}$:
:$\ds a_n = \frac 2 l \int_0^l \map f x \cos \frac {n \pi x} l \rd x $
In this context, $l = 4$ and so this can be expressed as:
{{begin-eqn}}
{{eqn | l = a_n
      | r = \frac 2 4 \paren {\int_0^2 x \cos \frac {n \pi x} 4 \rd x + \int_2^4 \paren {x - 2} \cos \frac {n \pi x} 4 \rd x}
      | c = 
}}
{{eqn | r = \frac 1 2 \paren {\int_0^2 x \cos \frac {n \pi x} 4 \rd x + \int_2^4 \paren {x - 2} \cos \frac {n \pi x} 4 \rd x}
      | c = 
}}
{{end-eqn}}
First the case when $n = 0$:
{{begin-eqn}}
{{eqn | l = a_0
      | r = \frac 1 2 \paren {\int_0^2 x \rd x + \int_2^4 \paren {x - 2} \rd x}
      | c = 
}}
{{eqn | r = \frac 1 2 \paren {\intlimits {\frac {x^2} 2} 0 2 + \intlimits {\frac {x^2} 2 - 2 x} 2 4}
      | c = Primitive of Power
}}
{{eqn | r = \frac 1 2 \paren {\paren {\frac {2^2} 2 - \frac {0^2} 2} + \paren {\paren {\frac {4^2} 2 - 2 \times 4} - \paren {\frac {2^2} 2 - 2 \times 2} } }
      | c = 
}}
{{eqn | r = \frac 1 2 \paren {\paren {\frac 4 2 - 0} + \paren {\paren {\frac {16} 2 - 8} - \paren {\frac 4 2 - 4} } }
      | c = 
}}
{{eqn | r = \frac 1 2 \paren {2 + 0 + 2}
      | c = 
}}
{{eqn | r = 2
      | c = 
}}
{{end-eqn}}
When $n \ne 0$:
{{begin-eqn}}
{{eqn | l = a_n
      | r = \frac 1 2 \paren {\int_0^2 x \cos \frac {n \pi x} 4 \rd x + \int_2^4 \paren {x - 2} \cos \frac {n \pi x} 4 \rd x}
      | c = 
}}
{{eqn | r = \frac 1 2 \paren {\int_0^2 x \cos \frac {n \pi x} 4 \rd x + \int_2^4 x \cos \frac {n \pi x} 4 \rd x - 2 \int_2^4 \cos \frac {n \pi x} 4 \rd x}
      | c = Linear Combination of Definite Integrals
}}
{{eqn | r = \frac 1 2 \int_0^4 x \cos \frac {n \pi x} 4 \rd x - \int_2^4 \cos \frac {n \pi x} 4 \rd x
      | c = Sum of Integrals on Adjacent Intervals for Integrable Functions
}}
{{end-eqn}}
Splitting it up into two:
{{begin-eqn}}
{{eqn | o = 
      | r = \frac 1 2 \int_0^4 x \cos \frac {n \pi x} 4 \rd x
      | c = 
}}
{{eqn | r = \frac 1 2 \intlimits {\frac {16} {n^2 \pi^2} \cos \frac {n \pi x} 4 + \frac 4 {n \pi} x \sin \frac {n \pi x} 4 } 0 4
      | c = Primitive of $x \cos a x$
}}
{{eqn | r = \frac 1 2 \paren {\paren {\frac {16} {n^2 \pi^2} \cos n \pi + \frac {16} {n \pi} \sin n \pi} - \paren {\frac {16} {n^2 \pi^2} \cos 0 + \frac 4 {n \pi} \times 0 \sin 0} }
      | c = 
}}
{{eqn | r = \frac 8 {n^2 \pi^2} \paren {\cos n \pi - \cos 0}
      | c = Sine of Multiple of Pi and simplification
}}
{{eqn | r = \frac {8 \paren {\paren {-1}^n - 1} } {n^2 \pi^2}
      | c = Cosine of Multiple of Pi
}}
{{eqn | r = \begin {cases} 0 & : \text {$n$ even} \\ \dfrac {-16} {n^2 \pi^2} & : \text {$n$ odd} \end {cases}
      | c = 
}}
{{eqn | r = \dfrac {-16} {\paren {2 r - 1}^2 \pi^2}
      | c = substituting $n = 2 r - 1$
}}
{{end-eqn}}
On the other hand:
{{begin-eqn}}
{{eqn | o = 
      | r = \int_2^4 \cos \frac {n \pi x} 4 \rd x
      | c = 
}}
{{eqn | r = \intlimits {\frac 4 {n \pi} \sin \frac {n \pi x} 4} 2 4
      | c = Primitive of $\cos a x$
}}
{{eqn | r = \frac 4 {n \pi} \paren {\sin \frac {4 n \pi} 4 - \sin \frac {2 n \pi} 4 }
      | c = 
}}
{{eqn | r = \frac 4 {n \pi} \paren {\sin n \pi - \sin \frac {n \pi} 2}
      | c = 
}}
{{eqn | r = -\frac 4 {n \pi} \sin \frac {n \pi} 2
      | c = Sine of Multiple of Pi
}}
{{end-eqn}}
The above expression is $0$ when $n$ is even, by Sine of Multiple of Pi.
Thus we may substitute $n = 2 r - 1$.
Then:
{{begin-eqn}}
{{eqn | o = 
      | r = \int_2^4 \cos \frac {n \pi x} 4 \rd x
      | c = 
}}
{{eqn | r = -\frac 4 {n \pi} \sin \frac {n \pi} 2
      | c = 
}}
{{eqn | r = -\frac 4 {\paren {2 r - 1} \pi} \sin \frac {\paren {2 r - 1} \pi} 2
      | c = 
}}
{{eqn | r = -\frac 4 {\paren {2 r - 1} \pi} \sin \paren {\paren {r - 1} + \frac 1 2} \pi
      | c = 
}}
{{eqn | r = -\frac 4 {\paren {2 r - 1} \pi} \paren {-1}^{r - 1}
      | c = Sine of Half-Integer Multiple of Pi
}}
{{end-eqn}}
Combining the results:
{{begin-eqn}}
{{eqn | l = \map f x
      | o = \sim
      | r = \frac {a_0} 2 + \sum_{n \mathop = 1}^\infty a_n \cos \frac {n \pi x} 4
}}
{{eqn | r = \frac {a_0} 2 + \sum_{n \text{ odd} } a_n \cos \frac {n \pi x} 4
      | c = as both integrals vanish for even $n$
}}
{{eqn | r = \frac {a_0} 2 + \sum_{r \mathop = 1}^\infty a_{2 r - 1} \cos \frac {\paren{2 r - 1} \pi x} 4
      | c = using our substitution $n = 2 r - 1$
}}
{{eqn | r = \frac 2 2 + \sum_{r \mathop = 1}^\infty \paren{\dfrac {-16} {\paren {2 r - 1}^2 \pi^2} + \frac 4 {\paren {2 r - 1} \pi} \paren {-1}^{r - 1} } \cos \frac {\paren{2 r - 1} \pi x} 4
      | c = from above
}}
{{eqn | r = 1 + \frac 4 \pi \sum_{n \mathop = 1}^\infty \frac {\paren {-1}^{r - 1} } {2 r - 1} \paren {\frac {- 4 \paren {-1}^{r - 1} } {\paren {2 r - 1} \pi} + 1} \cos \frac {\paren {2 r - 1} \pi x} 4
      | c = factorisation
}}
{{eqn | r = 1 + \frac 4 \pi \sum_{n \mathop = 1}^\infty \frac {\paren {-1}^{r - 1} } {2 r - 1} \paren {1 + \frac {4 \paren {-1}^r} {\paren {2 r - 1} \pi} } \cos \frac {\paren {2 r - 1} \pi x} 4
      | c = 
}}
{{end-eqn}}
{{qed}}