Finding the coefficients of a triangular wave. I have the following equation that I want to solve
$$a_k = \color{blue}{\frac{1}{T} \int_{0}^{T/2} 2 \frac{t}{T} e^{-i \frac{2\pi}{T}kt} dt} + \color{red}{\frac{1}{T} \int_{T/2}^{T} 2 \frac{T-t}{T} e^{-i \frac{2\pi}{T}kt} dt}$$
I decided started by taking the two parts as colored above. So for the blue part I would have
$$\frac{1}{T} \int_{0}^{T/2} 2 \frac{1}{T} e^{-i \frac{2\pi}{T}kt} dt$$
If I remove the constants I get
$$=\frac{2}{T^2} \int_0^{T/2} t \ e^{-i \frac{2\pi}{T}kt} \, dt$$
this allows me to take the integrand $(t \ e^{-i \frac{2\pi}{T}kt})$ and integrate it by parts with $f = t, \ dg = e^{-i \frac{2\pi}{T}kt}$ and $df = dt,  \ g = -\frac{e^{-i \frac{2\pi}{T}kt}}{i \frac{2\pi}{T}k}$, thus I now have
$$= \left. -\frac{2t \ e^{-i \frac{2\pi}{T}kT}}{ikT^2\frac{2\pi}{T}} \right|_{t=0}^\frac{T}{2} + \frac{2}{ikT^2\frac{2\pi}{T}} \int_0^{T/2} e^{-i \frac{2\pi}{T}kt}dt$$
I then proceed with calculating the antiderivative and substituting $ u = -i\frac{2\pi}{T}kt $ at the integrand. This gives me 
$$= \left.\frac{e^{-i\frac{1}{2}\frac{2\pi}{T}kT}}{i\frac{2\pi}{T}kT} + \Big(-\frac{2e^u}{i\frac{2\pi}{T})^2 k^2 T^2}\Big)\right|_{u=0}^{- i (1/2) (2\pi/T)kT} $$
I again evaluate the antiderivative so as to end up with this
$$ = \color{blue}{\frac{2\Big(1 - \frac{1}{2}e^{-i\frac{1}{2}\frac{2\pi}{T}kT}(i\frac{2\pi}{T}kT + 2)\Big)}{i(\frac{2\pi}{T})^2 k^2 T^2}} $$
Continuing with the red part, by following similar steps I end up with this
$$ \color{red}{\frac{e^{-i 2 \pi k} (-1 + e^{i \pi k}(1 - i \pi k))}{2 \pi^2 k^2}} $$
I know by having a look at the solution for this that I should end up with 
$$ a_k = \frac{e^{-ik \pi}-1}{\pi^2 k^2} $$
But I could not end up to anything similar by combining the two parts that I have found above (assuming they are correct).
Is there something wrong with the way I've tried to solve this and if yes how should I go on about doing it correctly?
 A: You want the Fourier coefficients of
$$
           f(t) = \left\{\begin{array}{cc}
     \frac{2t}{T}, & 0 \le t \le \frac{T}{2} \\
     \frac{2(T-t)}{T}, & \frac{T}{2} < t \le T
                         \end{array} \right.
$$
For $k \ne 0$, these can be written as
\begin{align}
    c_k & = \frac{1}{T}\int_{0}^{T}f(t)e^{-2\pi ikt/T}dt \\
        & = \left.\frac{1}{T}f(t)\frac{e^{-2\pi ikt/T}}{-2\pi i k/T}\right|_{t=0}^{T}-\frac{1}{T}\int_{0}^{T}f'(t)\frac{e^{-2\pi ikt/T}}{-2\pi i k/T}dt \\
   & = -\frac{1}{T}\int_{0}^{T}f'(t)\frac{e^{-2\pi ikt/T}}{-2\pi ik/T}dt \\
   & = -\frac{1}{T}\int_{0}^{T/2}\frac{2}{T}\cdot\frac{e^{-2\pi ikt/T}}{-2\pi ik/T}dt-\frac{1}{T}\int_{T/2}^{T}\frac{-2}{T}\cdot\frac{e^{-2\pi ikt/T}}{-2\pi ik/T}dt \\
   & = \frac{1}{\pi i kT}\int_{0}^{T/2}e^{-2\pi ikt/T}dt
       -\frac{1}{\pi ikT}\int_{T/2}^{T}e^{-2\pi ikt/T}dt \\
   & = \frac{1}{\pi ikT}\cdot\frac{T}{(-2\pi i k)}\{e^{-2\pi ik T/2T}-1\}
       -\frac{1}{\pi ikT}\cdot\frac{T}{(-2\pi i k)}\{1-e^{-2\pi ikT/2T}\} \\
   & = \frac{1}{\pi ikT}\cdot\frac{T}{(-2\pi i k)}2\{(e^{-\pi i})^{k}-1\} \\
   & = \frac{1}{\pi^2 k^2}\{(-1)^k-1\}.
\end{align}
I think you were getting messed up with intermediate evaluation terms at $T/2$, which cancel anyway because of the continuity of $f$ at $T/2$. It's only after taking the derivative of $f$ that you have to split the interval in order to further integrate by parts.
A: Let's call $\nu=\frac{2\pi}{T}$ so we have the coefficient of the Fourier series of the triangual function is
$$
a_k = \color{blue}{\frac{1}{T} \int_{0}^{T/2} 2 \frac{t}{T} \mathrm e^{-i \nu kt} \mathrm dt} + \color{red}{\frac{1}{T} \int_{T/2}^{T} 2 \left(1-\frac{t}{T} \right)\mathrm e^{-i \nu kt} \mathrm dt}
$$
Observe that integrating by parts
$$
\int t\,\mathrm e^{at} \mathrm dt=\frac{\mathrm e^{at} (at-1)}{a^2}+C
$$
So for the blue part we have
\begin{align}
\color{blue}{\frac{2}{T^2} \int_{0}^{T/2} t \,\mathrm e^{-i \nu kt} \mathrm dt} &=\frac{2}{T^2}\cdot  \left. \frac{\mathrm e^{-i \nu kt}(-i \nu kt-1)}{(-i \nu k)^2}\right|_0^{T/2}=\frac{2}{T^2}\cdot  \left. \frac{\mathrm e^{-i \nu kt}(i \nu kt+1)}{( \nu k)^2}\right|_0^{T/2}\\
&=\frac{2}{T^2}\cdot  \left[ \frac{\mathrm e^{-i \nu kT/2}(i \nu kT/2+1)}{( \nu k)^2}-\frac{1}{(\nu k)^2}\right]\\
&=\frac{2}{T^2}\cdot  \frac{\mathrm e^{-i \nu kT/2}(i \nu kT/2+1)-1}{ \nu^2 k^2}\\
&=\frac{1}{2}\cdot  \frac{\mathrm e^{-i \pi k}(i \pi k+1)-1}{\pi^2 k^2}
\end{align}
using $\nu T/2=\pi$.
For the red part we have
\begin{align}
\color{red}{\frac{1}{T} \int_{T/2}^{T} 2 \left(1-\frac{t}{T} \right)\mathrm e^{-i \nu kt} \mathrm dt} &=\frac{2}{T} \int_{T/2}^{T} \mathrm e^{-i \nu kt} \mathrm dt-\frac{2}{T^2} \int_{T/2}^{T} t\,\mathrm e^{-i \nu kt} \mathrm dt \\
&=\frac{2}{T}\cdot  \left. \frac{\mathrm e^{-i \nu kt}}{-i \nu k} \right|_{T/2}^{T}-\frac{2}{T^2}\cdot  \left. \frac{\mathrm e^{-i \nu kt}(i \nu kt+1)}{( \nu k)^2}\right|_{T/2}^{T}\\
&=\frac{2}{T}\cdot  \left[ \frac{\mathrm e^{-i \nu kT}}{-i \nu k}-\frac{\mathrm e^{-i \nu kT/2}}{-i \nu k} \right]-\frac{2}{T^2}\cdot  \left[ \frac{\mathrm e^{-i \nu kT}(i \nu k T+1)}{( \nu k)^2}-\frac{\mathrm e^{-i \nu kT/2}(i \nu kT/2+1)}{( \nu k)^2}\right]\\
&=\frac{i}{\pi k}\cdot  \left[ \mathrm e^{-i 2k\pi}-\mathrm e^{-i k\pi} \right]-\frac{1}{2k^2\pi^2}\cdot  \left[ \mathrm e^{-i 2k\pi}(i 2k\pi+1)-\mathrm e^{-i k\pi}(i k\pi+1)\right]\\
&=\frac{i}{\pi k}\cdot  \left[ 1-\mathrm e^{-i k\pi} \right]-\frac{1}{2k^2\pi^2}\cdot  \left[ (i 2k\pi+1)-\mathrm e^{-i k\pi}(i k\pi+1)\right]\\
&=\frac{i}{\pi k}-\frac{i 2k\pi+1}{2k^2\pi^2}
-\mathrm e^{-i k\pi}  \left[\frac{i}{\pi k}-\frac{i k\pi+1}{2k^2\pi^2}\right]\\
&=-\frac{1}{2k^2\pi^2}
+\mathrm e^{-i k\pi}  \frac{1-i k\pi}{2k^2\pi^2}\\
&=\frac{1}{2}\cdot  \frac{\mathrm e^{-i \pi k}(1-i \pi k)-1}{\pi^2 k^2}
\end{align}
using $\nu T/2=\pi$ and $ \mathrm e^{-i 2k\pi}=1$.

Thus summing
  $$
a_k = \frac{1}{2}\cdot  \frac{\mathrm e^{-i \pi k}(i \pi k+1)-1}{\pi^2 k^2}+\frac{1}{2}\cdot  \frac{\mathrm e^{-i \pi k}(1-i \pi k)-1}{\pi^2 k^2}=\frac{\mathrm e^{-i \pi k}-1}{\pi^2 k^2}
$$
  and observing that $\mathrm e^{-i \pi k}=(-1)^k$, we have $a_{2k}=0$ and 
  $$
a_{2k+1}=-\frac{2}{\pi^2 (2k+1)^2}
$$

