I have been trying to prove Leibniz's formula for $\pi$:
\begin{equation} \frac{\pi}{4}=\sum_{i=0}^\infty \frac{(-1)^i}{(2i+1)} \end{equation}
derivating the following function as a Fourier series:
\begin{equation} f(x) = \left\{\begin{matrix} &1 &\text{if } & x\in[\pi/2,\pi)\\ &0 &\text{if } & x\in(-\pi/2,\pi/2)\\ &-1 &\text{if } & x\in(-\pi,-\pi/2] \end{matrix}\right. \end{equation}
Given that $f$ is an odd function, all $a_n$ terms of the Fourier series will be zero so it suffices to calculate $b_n$ terms for each $n\in\mathbb{N}$. Let $n$ be any natural number. Then:
\begin{equation} b_n = \frac{1}{\pi}\int_{-\pi}^\pi f(x) \sin(xn) dx=\frac{1}{\pi}\left(\int_{-\pi}^{-\pi/2}-\sin(xn) dx+\int_{\pi/2}^\pi \sin(xn)dx\right)=\frac{2}{\pi}\int_{\pi/2}^\pi \sin(xn)dx=\frac{-2}{n\pi}\left(\cos(\pi n) - \cos\left(\frac{\pi n}{2}\right)\right)=\frac{-2}{\pi n}\left((-1)^n - \cos\left(\frac{\pi n}{2}\right)\right) \end{equation}
Hence, $f$ can be expressed as:
\begin{equation} f(x) = \sum_{n=1}^\infty \frac{-2}{\pi n}\left((-1)^n - \cos\left(\frac{\pi n}{2}\right)\right)\sin(nx) \end{equation}
Evaluating $f$ at $x=\pi/2$:
\begin{equation} f\left(\frac{\pi}{2}\right) = \sum_{n=1}^\infty \frac{-2}{\pi n}\left((-1)^n - \cos\left(\frac{\pi n}{2}\right)\right)\sin\left(\frac{\pi n}{2}\right) \end{equation}
For any even number $k\in\mathbb{N}$, $\sin\left(\frac{\pi k}{2}\right) = 0$. Otherwise, $\sin\left(\frac{\pi (2k+1)}{2}\right) = (-1)^{k}\quad\forall k\in\mathbb{N}$. Therefore:
\begin{equation} f\left(\frac{\pi}{2}\right) = \sum_{n=0}^\infty \frac{-2}{\pi (2n+1)}\left((-1)^{2n+1} - \cos\left(\frac{\pi (2n+1)}{2}\right)\right)(-1)^{n} = \sum_{n=0}^\infty \frac{2}{\pi (2n+1)}(-1)^{n}\overset{\text{def. of }f}{=}1 \end{equation} Leading to the equation:
\begin{equation} \frac{\pi}{2}=\sum_{i=0}^\infty \frac{(-1)^i}{(2i+1)} \end{equation}
What am I doing wrong? I have been checking over and over again but cannot seem to spot the mistake.
When there is a discontinuity, as you have in $f(x)$ at $x=\frac\pi2$, the Fourier series converges to the average of the left and right limits: that is the sum should evaluate to $\frac12$, not $1$.
Think of what would have happened if you had defined $$ f(x)=\left\{\begin{array}{rl} 1&\text{if }x\in\left(\frac\pi2,\pi\right)\\ 0&\text{if }x\in\left[-\frac\pi2,\frac\pi2\right]\text{ or }x=-\pi\\ -1&\text{if }x\in\left(-\pi,-\frac\pi2\right)\\ \end{array}\right. $$ You would get the same integrals, yet $f\!\left(\frac\pi2\right)=0$.
I changed the definition at $x=-\pi$ so that the function would be odd, but this will not change the values of the integrals.