Fourier Cosine Series
a0=(1/L) Integral from 0 to Pi of f(x)
ak=(2/L)
Integral from 0 to L of f(x)cos(k Pi/L x)
bk=0

Let's do the same example.
f(x) = x for 0<=x<=Pi/2
Pi-x for Pi/2<=x<=Pi
a0=(1/Pi) (Integrate[x,{x,0,Pi/2}]+
Integrate[Pi-x,{x,Pi/2,Pi}])
a[n_]:=(2/Pi)(Integrate[x u[x,Pi,n],{x,0,Pi/2}]+
Integrate[(Pi-x) u[x,Pi,n],{x,Pi/2,Pi}])
Table[a[k],{k,1,10}]Can you see the pattern?
Let's calculate some partial sums. Be patient.
Clear[Sn]
Sn=Table[Sum[a[k] u[x,Pi,k],{k,1,n}],{n,1,10}];Let's see how we did.
Clear[sn1,sn3,sn5,sn10]
sn1=Plot[a0+Sn[[1]],{x,0,Pi},DisplayFunction->Identity];
Show[fgraph,sn1,PlotLabel->
FontForm["a0+a1Cos[x]",{"Helvetica-Bold",12}]];
sn3=Plot[a0+Sn[[3]],{x,0,Pi},DisplayFunction->Identity];
Show[fgraph,sn3,PlotLabel->
FontForm["a0+a1cos[x]+a2cos[2x]+a3cos[3x]",{"Helvetica-Bold",12}]];
sn5=Plot[a0+Sn[[5]],{x,0,Pi},DisplayFunction->Identity];
Show[fgraph,sn5,PlotLabel->
FontForm["Sum(k=1 to 5) a0+akCos[kx]",{"Helvetica-Bold",12}]];
sn10=Plot[a0+Sn[[10]],{x,0,Pi},DisplayFunction->Identity];
Show[fgraph,sn10,PlotLabel->
FontForm["Sum(k=1 to 10) a0+akCos[kx]",{"Helvetica-Bold",12}]];You may have noticed that the sine series seemed to get to the answer quicker. You can see because so many of the ak terms were 0 so they did not contribute to the series. In general we must have the answer to the following question: How do I know whether to use the cosine series or the sine series?
You want your periodic extension to be smooth (no sharp turns or points). The smoother the extension, the faster the series will converge.
Recall that a sharp turn or point on the graph means that the derivative does not exist there!
In this example, using the odd periodic extension did not produce a sharp turn at x=0, the even periodic extension did.