First example in (L^2)[-π,π]

f[x_]=Piecewise[{{Pi^2/8(-2-2/Pi x),-Pi<x<-Pi/2},{Pi^2/8(2/Pi x),-Pi/2<x<Pi/2},{Pi^2/8(2-2/Pi x), Pi/2<x<Pi}}]

Plot[f[x],{x,-Pi,Pi},
Ticks->{Range[-Pi,Pi,Pi/2],Range[-Pi^2/8,Pi^2/8,Pi^2/8]}]

Manipulate[Plot[{f[x],a1*Sin[ x]+a2*Sin[2x]+a3*Sin[3x],a2*Sin[2x],a3*Sin[3x]},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
ImageSize->Large],
{{a1,0},-2,2},
{{a2,0},-2,2},
{{a3,0},-2,2}]

Plot[{f[x], Sin[x]-(1/9)*Sin[3x]+(1/25)*Sin[5x]-(1/49)Sin[7x]},{x,-Pi,Pi},ImageSize->Large]

fdotv=Integrate[f[x] Sin[m x],{x,-Pi,Pi}]

fdotv=Simplify[fdotv,Assumptions->Element[m,Integers]]

fdotv=FullSimplify[fdotv,Assumptions->Element[m,Integers]]

vdotv=Integrate[ Sin[m x]^2,{x,-Pi,Pi}]

vdotv=Simplify[vdotv,Assumptions->Element[m,Integers]]

a[m_]=fdotv/vdotv

a[1]

a[2]

a[3]

Table[a[m],{m,1,9}]

Table[Sum[a[m]Sin[m x],{m,1,n}],{n,1,9}]

Manipulate[
Plot[{f[x],Sum[a[m]Sin[m x],{m,1,n}],f[x]-Sum[a[m]Sin[m x],{m,1,n}]},{x,-Pi,Pi}, ImageSize->Large],{n,1,101,2}]

Second example in (L^2)[-π,π]

f[x_]=Piecewise[{{-1,-Pi<x<0},{1,0<x<Pi}}]

Plot[f[x],{x,-Pi,Pi},
Ticks->{Range[-Pi,Pi,Pi/2],Range[-1,1]}]

fdotv=Integrate[f[x] Sin[m x],{x,-Pi,Pi}]

fdotv=Simplify[fdotv,Assumptions->Element[m,Integers]]

vdotv=Integrate[ Sin[m x]^2,{x,-Pi,Pi}]

vdotv=Simplify[vdotv,Assumptions->Element[m,Integers]]

a[m_]=fdotv/vdotv

a[1]

a[2]

a[3]

Table[a[m],{m,1,9}]

Manipulate[
Plot[{f[x],Sum[a[k]Sin[k x],{k,1,n}]},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
 ImageSize->Large],{n,1,201,2}]

Manipulate[
Plot[{f[x],Sum[a[k]Sin[k x],{k,1,n}],(f[x]-Sum[a[k]Sin[k x],{k,1,n}])^2},{x,-Pi,Pi},
PlotRange->{-1.5,1.5},
 ImageSize->Large],{n,1,201,2}]

Mean-square error

The Manipulate object below shows a plot of the step function from the second example above along with the first two terms in the sine expansion.  The coefficient on the sin(x) term is fixed at the correct value of 4/π.  The coefficient on the sin(3x) term is the manipulation variable.  The number in the plot is the mean-square difference between the function f(x) and the two-term sine expansion.  We can adjust the coefficient value to minimze this mean-square difference.

Manipulate[
Show[Plot[{f[x],4/Pi*Sin[x]+a3*Sin[3x]},{x,-Pi,Pi},PlotRange->{-1.5,1.5},ImageSize->Large],Graphics[Text[NIntegrate[(f[x]-4/Pi*Sin[x]-a3*Sin[3x])^2,{x,-Pi,Pi}],{2,-1}]]],
{{a3,0},-2,2}]

We can repeat this with the sin(3x) coefficient fixed at the correct value of 4/3π and a sin(5x) term included.

Manipulate[
Show[Plot[{f[x],4/Pi*Sin[x]+4/Pi/3*Sin[3x]+a5*Sin[5x]},{x,-Pi,Pi},PlotRange->{-1.5,1.5},ImageSize->Large],Graphics[Text[NIntegrate[(f[x]-4/Pi*Sin[x]-4/Pi/3*Sin[3x]-a5*Sin[5x])^2,{x,-Pi,Pi}],{2,-1}]]],
{{a5,0},-2,2}]

Manipulate[
Show[Plot[{f[x],4/Pi*Sin[x]+4/Pi/3*Sin[3x]+a5*Sin[5x]},{x,-Pi,Pi},PlotRange->{-1.5,1.5},ImageSize->Large],Graphics[Text[NIntegrate[(f[x]-4/Pi*Sin[x]-4/Pi/3*Sin[3x]-a5*Sin[5x])^2,{x,-Pi,Pi}],{2,-1}]]],
{{a5,0},-2,2}]

Here's the same with the addition of a plot of the square of the difference.  The integral of the square of the difference is the mean square difference.

Manipulate[
Show[Plot[{f[x],4/Pi*Sin[x]+4/Pi/3*Sin[3x]+a5*Sin[5x],(f[x]-4/Pi*Sin[x]-4/Pi/3*Sin[3x]-a5*Sin[5x])^2},{x,-Pi,Pi},PlotRange->{-1.5,1.5},ImageSize->Large],Graphics[Text[NIntegrate[(f[x]-4/Pi*Sin[x]-4/Pi/3*Sin[3x]-a5*Sin[5x])^2,{x,-Pi,Pi}],{2,-1}]]],
{{a5,0},-2,2}]