Fourier Series

Fourier Series for a Triangle Wave

f[x_] = If[x<0, x+1, -x+1];

p0 = Plot[f[x], {x,-1,1}, PlotStyle->RGBColor[1,0,0]]

For the coefficients a[n] of Cos[n Pi x]:

a[0] = Integrate[(x+1),{x,-1,0}] +
           Integrate[(-x+1),{x,0,1}]

a[n_] = Integrate[(x+1)Cos[n Pi x], {x,-1,0}] + 
            Integrate[(-x+1)Cos[n Pi x], {x,0,1}]

Since Cos[n Pi]==1 if n is even and Cos[n Pi]==-1 if n is odd, a[n] can also be defined by
    a[2n-1] = 4/((2n-1)^2Pi^2);
    a[2n] = 0;

The coefficients b[n] of Sin[n Pi x] should be 0 since f[x] is an even function, f[-x]==f[x].

b[n_] = Integrate[(x+1)Sin[n Pi x], {x,-1,0}] + 
          Integrate[(-x+1)Sin[n Pi x], {x, 0,1}]

fs[k_] = a[0]/2 + Sum[a[2n-1]Cos[(2n-1)Pi x],{n,1,k}];

fs[3]

Do[
    p[i] = Plot[fs[i],{x,-1,1},PlotRange->{0,1}],
{i,1,10}]

Do[
    Show[p0,p[i],PlotRange->{0,1}],
{i,1,10}]

Interesting Observation: Since f[0] == 1, we see that
    1 == 1/2 + (4/Pi^2) Sum[1/(2n-1)^2,{n,1,Infinity}]
or
    Pi^2/8 == Sum[1/(2n-1)^2,{n,1,Infinity}]

Note also that

Sum[1/n^2,{n,1,Infinity}] ==
      Sum[1/(2n)^2,{n,1,Infinity}] +
        Sum[1/(2n-1)^2,{n,1,Infinity}]
    ==(1/4)Sum[1/n^2,{n,1,Infinity}] +
        Sum[1/(2n-1)^2,{n,1,Infinity}]
    ==(1/4)Sum[1/n^2,{n,1,Infinity}] + Pi^2/8

so

(3/4)Sum[1/n^2,{n,1,Infinity}] == Pi^2/8

or

Pi^2/6 == Sum[1/n^2,{n,1,Infinity}]
       == 1 + 1/2^2 + 1/3^2 + 1/4^2 + ...

N[Pi^2/6]

s[1] = 1;
Do[
   Print[s[n] = s[n-1] + 1/n^2 //N],
{n,2,100}]

Fourier Series for a Square Wave

f[x_] = If[x<0, -1, 1];

p0 = Plot[f[x], {x,-1,1}, PlotStyle->RGBColor[1,0,0]]

The coefficients a[n] of Cos[n Pi x] should be 0 since f[x] is an odd function, f[-x]==-f[x].

a[n_] = Integrate[- Cos[n Pi x], {x,-1,0}] + 
            Integrate[Cos[n Pi x], {x,0,1}]

a[0] = Integrate[-1,{x,-1,0}] + Integrate[1,{x,0,1}]

For the coefficients b[n] of Sin[n Pi x]:

b[n_] = Integrate[-Sin[n Pi x], {x,-1,0}] + 
            Integrate[Sin[n Pi x], {x, 0,1}]

Since Cos[n Pi]==1 if n is even and Cos[n Pi]==-1 if n is odd, b[n] can be defined by
    b[2n-1] = 4/((2n-1) Pi);
    b[2n] = 0;

fs[k_] = Sum[b[2n-1]Sin[(2n-1)Pi x],{n,1,k}];

fs[3]

Do[
    p[i] = Plot[fs[i],{x,-1,1},PlotRange->{-1.5,1.5}],
{i,1,10}]

Do[
    Show[p0,p[i],PlotRange->{-1.5,1.5}],
{i,1,10}]

Interesting Observation:
Since f[1/2]==1,  and Sin[(2n-1)Pi/2]==(-1)^(n-1)

1 == Sum[4/((2n-1) Pi)Sin[(2n-1)Pi/2], {n,1,Infinity}]
     == (4/Pi)Sum[(-1)^(n-1)/(2n-1),{n,1,Infinity}]

or

Pi/4 == Sum[(-1)^n/(2n-1),{n,1,Infinity}]
     == 1 - 1/3 + 1/5 - 1/7 + ...

N[Pi/4]

s[1] = 1;
Do[
   Print[s[n] = s[n-1] + (-1)^(n-1)/(2n-1) //N],
{n,2,100}]

Fourier series of an odd function

Consider the periodic function defined on the interval -2 <= x <= 2:

f[x_] = If[ x<-1 || x>1, 0, x ];

and defined outside this interval by f[x + 4] = f[x].

p0 = Plot[f[x], {x,-2,2}, PlotStyle->RGBColor[1,0,0]]

Since f[-x] == -f[x], f[x] is an odd function. We thus need only compute the coefficients b[n] of Sin[n Pi x/2]. Since f[x]==0 on the intervals (-2,-1) and (1,2), the integral of (1/2)f[x]Sin[n Pi x/2] from -2 to 2 reduces to

b[n_] = (1/2)Integrate[x Sin[n Pi x/2], {x,-1,1}]//Simplify

Since
Sin[n Pi/2] == 1 and Cos[n Pi/2] == 0 when n==4k+1
Cos[n Pi/2] ==-1 and Sin[n Pi/2] == 0 when n==4k+2
Sin[n Pi/2] ==-1 and Cos[n Pi/2] == 0 when n==4k+3
Cos[n Pi/2] == 1 and Sin[n Pi/2] == 0 when n==4k+4
the coefficients b[n] can be rewritten as
  b[4n+1] =  4/((4n+1)^2 Pi^2);
  b[4n+2] =  2/((4n+2) Pi);
  b[4n+3] = -4/((4n+3)^2 Pi^2);
  b[4n+4] = -2/((4n+4) Pi),

fs[k_] = Sum[b[n]Sin[n Pi x/2],{n,1,k}];

fs[3]

Do[
    p[i] = Plot[fs[i],{x,-2,2},PlotRange->{-1.5,1.5}],
{i,1,10}]

Do[
    Show[p0,p[i],PlotRange->{-1.5,1.5}],
{i,1,10}]

Fourier series involving both sine and cosine

Consider the periodic function defined on the interval -1 <= x <= 1:

f[x_] = If[ x<0, 0, x^2 ];

and defined outside this interval by f[x + 2] = f[x].

p0 = Plot[f[x], {x,-1,1}, PlotStyle->RGBColor[1,0,0],
PlotRange->{0,1}]

Since f[x] is neither even nor odd, we must compute both the coefficients a[n]of Cos[n Pi x] and the coefficients b[n] of Sin[n Pi x]. Since f[x]==0 on the intervals (-1,0), the integrals involving f[x] from -1 to 1 reduce to

a[0] = Integrate[x^2, {x,0,1}]

a[n_] = Integrate[x^2 Cos[n Pi x], {x,0,1}] /. Sin[n Pi]->0

b[n_] = Integrate[x^2 Sin[n Pi x], {x,0,1}] /. Sin[n Pi]->0 //Simplify

Since Cos[2n Pi]==1 and Cos[(2n-1)Pi]==-1, the coefficients a[n] and b[n] can also be written as
  a[2n]   =  2/((2n)^2 Pi^2);
  a[2n-1] = -2/((2n-1)^2 Pi^2);
  b[2n]   = -1/((2n) Pi);
  b[2n-1] = -4/((2n-1)^3 Pi^3)+1/((2n-1) Pi);

fs[k_] = a[0]/2 + Sum[a[n]Cos[n Pi x] +
                      b[n]Sin[n Pi x],{n,1,k}];

fs[3]

Do[
    p[i] = Plot[fs[i],{x,-1,1},PlotRange->{-1,1}],
{i,1,10}]

Do[
    Show[p0,p[i],PlotRange->{-1.5,1.5}],
{i,1,10}]