Solution values doubled in Fourier series problem output?

I am trying to solve the following Fourier Series problem.

f(x)=\begin{cases} -1,& -1<x<0 \newline \;\; 1,&\;\; 0<x<1 \end{cases}

We know that on the interval [-1,1] is odd. We calculate the coefficients

a_0=0 , \quad a_n=0 , \quad b_n=\frac{4}{n\pi}

Thus,

f(x)= \sum_{n=1}^{\infty} \frac{4}{n\pi} \sin(n \pi x)

I tried to study the same problem on Mathematica with the following code

a[n_] := (2/L)*Integrate[f[x]*Cos[2 n*Pi*x/L], {x, -L/2, L/2}]
a[0] := (1/L)*Integrate[f[x], {x, -L/2, L/2}]
b[n_] := (2/L)*Integrate[f[x]*Sin[2 n*Pi*x/L], {x, -L/2, L/2}]
F[x_, N_] := a[0] + Sum[a[n]*Cos[2 n*Pi*x/L] + b[n]*Sin[2 n*Pi*x/L], {n, 1, N}]
p[N_, a_] := 
 Plot[Evaluate[F[x, N]], {x, -a, a}, PlotRange -> All, 
  PlotPoints -> 200]
L=2;
f[x_] = If[x > 0, 1, -1];
a[n]
a[0]
b[n]
Simplify[%, n \[Element] Integers]
Table[F[0.5, k], {k, 0, 20}]

We notice that with the exception of the first value all other values are double. Why is this phenomenon observed? I know that is more a maths question than a Mathematica one but I would appreciate any help.

Your formula for f[x] is wrong. Try plotting the result:

Sum[4/(n Pi) Sin[n Pi x], {n, 1, Infinity}]
Plot[Chop[%], {x, -1, 1}]

Could you explain to me please why the F(x) is wrong?

The series formula f(x)=∑n=1∞4nπsin(nπx) does not reproduce the function f(x)={−1,1,−1<x<00<x<1. It is the series for another function. Didn’t you make the plot?

Yes this is the notebook with my results

Your Mathematica calculation for the square wave is right. In your first post, the series formula after the “thus” is the series for the sawtooth wave, not for the square wave. This is why “all other values are double”.