After solving a heat equation and applying the initial condition I need to find coefficients $a_n$ for following solution:
$$\phi(x,0) = \sum_{n=0}^{\infty} a_n \cos{\Big(\frac{(2n+1)\pi x}{L}}\Big) = f(x).$$
The function $f(x)$ for initial condition is: $$f(x) = \begin{cases} 1 - x^2, & 0 \leq x \leq 1, \\ 0, & 1 < x < L. \end{cases}$$
I solved this by substituting $m=2n+1$ and using the Fourier cosine formula: $$a_0 = \frac{1}{L} \int_0^L f(x) \ dx$$ $$a_n = \frac{2}{L} \int_0^L f(x) \cos{\Big(\frac{m \pi x}{L} \Big)} \ dx.$$
This formula works for approximating $a_n \cos(m \pi x/L)$. You can see the upside-down parabola with maximum value of $y(0) = 1$, the same one as in the initial conditions:
See interactive graph here: https://www.desmos.com/calculator/buwlinay71
However, when I substitute back $m=(2n + 1)$ and ignore $a_0$ the formula produces result that is $0.5$ of the desired amplitude. You can see the upside-down parabola, but its maximum value is $0.5$ instead of $1$.
Interactive graph: https://www.desmos.com/calculator/mgdwcgxkof
I'm banging my head agains the wall for hours now checking my algebra and calculations seem correct. Did I make a conceptual mistake by using $m=2n+1$ and throwing away the $a_0$ coefficient? It bothers me that by replacing $m=2n+1$ I threw away the even terms of the series, and I suspect that this is what's causing this bug.


Both of your series solutions are incorrect (and also completely different series). The first one happens to match the given function out of luck. You sum should be something like
$$ f(x) = a_0\cos\left(\frac{\pi x}{L}\right) + a_1 \cos\left(\frac{3\pi x}{L}\right) + a_2 \cos\left(\frac{5\pi x}{L}\right) + \dots $$
Notice there is no constant term here, since all the frequencies are odd. It seems the indexing threw you off since $a_0$ corresponds to the first odd frequency, and so on.
Your first series appears to be a different Fourier expansion including even frequencies ($\cos (\frac{2\pi x}{L})$ and so on), which is incorrect.
Your second series only contains odd frequencies, so it's much closer to the actual solution.
I would suggest you redo your integrals this way (no substitutions needed), and note that
$$ a_n = \frac{2}{L}\int_0^L f(x) \cos \left(\frac{(2n+1)\pi x}{L} \right)\ dx $$
for all values of $n$