f(x)=|x|Note: For this example, you will want to click once on the plots that you get and drag the little black boxes to increase the size of the graph.
part4=Plot[Abs[x+2Pi],{x,-3Pi,-Pi},DisplayFunction->Identity];
part5=Plot[Abs[x],{x,-Pi,Pi},DisplayFunction->Identity];
part6=Plot[Abs[x-2Pi],{x,Pi,3Pi},DisplayFunction->Identity];
absgraph=Show[part4,part5,part6,
DisplayFunction->$DisplayFunction,AspectRatio->Automatic];Mathematica does not like to integrate piecewise defined functions so we must break it down. We cannot use the innerproduct function if we do not integrate between -Pi and Pi.
Clear[a0,a,b,Sn]
a0=(1/(2Pi))(Integrate[-x,{x,-Pi,0}]+Integrate[x,{x,0,Pi}]);
a=(1/Pi)Table[Integrate[-x u[x][[i]],{x,-Pi,0}]+
Integrate[x u[x][[i]],{x,0,Pi}],{i,1,10}];
b=(1/Pi)Table[Integrate[-x v[x][[i]],{x,-Pi,0}]+
Integrate[x v[x][[i]],{x,0,Pi}],{i,1,10}];
a0 a
Can you see the pattern?
b
Well?
Let's calculate a few partial sums.
Sn=Table[a0+Sum[a[[k]]Cos[k x],{k,1,n}],{n,1,10}];Let's see how we did.
Clear[sn1,sn3,sn5,sn10]
sn1=Plot[Sn[[1]],{x,-2 Pi,2 Pi},DisplayFunction->Identity];
Show[absgraph,sn1,PlotLabel->
FontForm["a0+a1Cos[x]",{"Helvetica-Bold",12}]];Pretty good, even with only one term!
sn3=Plot[Sn[[3]],{x,-2 Pi,2 Pi},DisplayFunction->Identity];
Show[absgraph,sn3,PlotLabel->
FontForm["a0+a1Cos[x]+a2Cos[2x]+a3Cos[3x]",{"Helvetica-Bold",12}]];
sn5=Plot[Sn[[5]],{x,-2 Pi,2 Pi},DisplayFunction->Identity];
Show[absgraph,sn5,PlotLabel->
FontForm["a0+Sum(k=1 to 5) akCos[kx]",{"Helvetica-Bold",12}]];
sn10=Plot[Sn[[10]],{x,-2 Pi,2 Pi},DisplayFunction->Identity];
Show[absgraph,sn10,PlotLabel->
FontForm["a0+Sum(k=1 to 10) akCos[kx]",{"Helvetica-Bold",12}]];Pretty impressive!
Up to Fourier Series Examples