While solving a PDE, I ran into the following problem:
$$ \sum_{m=1}^\infty A_m \cos(m \pi x)=x-1 \tag 1 $$
$A_m$ can be determined using the principle of orthogonality:
$$ \int^1_0 A_m \cos^2(m \pi x)dx=\int^1_0(x-1) \cos(m \pi x)dx \Rightarrow A_m = 2 \int^1_0(x-1) \cos(m \pi x)dx \tag 2 $$
The solution to the integral is:
$$ \int^1_0(x-1) \cos(m \pi x)dx = {\cos (m \pi) - 1 \over (m \pi)^2} \tag 3 $$
which means the expression for $A_m$ is:
$$ A_m = 2 \cdot {\cos (m \pi) - 1 \over (m \pi)^2} \tag 4 $$
However, when I ploted the left and right hand side of equation $(1)$ I got the next graph:
Somehow I am missing an offset of one half in my solution. However, I don't know where did I go wrong. Can someone please point out my mistake?

Here $m$ should start from $0$, not $1$.
Note your result for $A_m$ works for $m=1,2,3...$
But for $m=0$, you need to compute it separately, which gives:
$$ \int^1_0 A_0 dx=\int^1_0(x-1) dx \Rightarrow A_0 = -\frac{1}2 $$
This should give you a downward shift and match your plot.