f(x)=|x|

Let's look at f(x)=|x|. Remember, the functions must be periodic with period 2Pi, so what we really consider is the periodic extension f^(x)=|x| from -Pi to Pi, |x-2Pi| from Pi to 3Pi, etc., which is continuous.

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.

In[64]:=

  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.

In[65]:=

  Clear[a0,a,b,Sn]
  a0=(1/(2Pi))(Integrate[-x,{x,-Pi,0}]+Integrate[x,{x,0,Pi}]);

In[66]:=

  a=(1/Pi)Table[Integrate[-x u[x][[i]],{x,-Pi,0}]+
  Integrate[x u[x][[i]],{x,0,Pi}],{i,1,10}];

In[67]:=

  b=(1/Pi)Table[Integrate[-x v[x][[i]],{x,-Pi,0}]+
  Integrate[x v[x][[i]],{x,0,Pi}],{i,1,10}];

In[68]:=

  a0
  a

Out[68]=

  Pi
  --
  2

Out[69]=

   -4      -4       -4        -4        -4
  {--, 0, ----, 0, -----, 0, -----, 0, -----, 0}
   Pi     9 Pi     25 Pi     49 Pi     81 Pi

Can you see the pattern?

Can you see the pattern? ...

In[70]:=

  b

Out[70]=

  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

Well?

Well? ...

Let's calculate a few partial sums.

In[71]:=

  Sn=Table[a0+Sum[a[[k]]Cos[k x],{k,1,n}],{n,1,10}];

Let's see how we did.

In[72]:=

  Clear[sn1,sn3,sn5,sn10]

In[73]:=

  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!

In[74]:=

  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}]];

In[75]:=

  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}]];

In[76]:=

  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