May 27, 2010

The coefficients  (Subscript[a, k]) and (Subscript[b, k]) of the Fourier series expansion 
                   ((Subscript[a, 0])/2)+(∑_(k=1)^∞)( (Subscript[a, k])cos(k x) + (Subscript[b, k])sin(k x) )
for a function  f(x) defined on 0 ⩽ x ⩽ 2π   are given by 

             (Subscript[a, k])= (1/π)(∫_0^(2π))f(x)cos(k x)dx,     for  k ⩾ 0,
and 
             (Subscript[b, k])= (1/π)(∫_0^(2π))f(x)sin(k x)dx,     for  k ⩾ 1.

In terms of delayed evaluation for a function f(x) specified later, we introduce

a[k_] := (1/Pi)Integrate[f[x]*Cos[k*x], {x,0,2Pi}]

b[k_] := (1/Pi)Integrate[f[x]*Sin[k*x], {x,0,2Pi}]

S[n_,x_] := a[0]/2 + Sum[ (a[k]*Cos[k*x]+ b[k]*Sin[k*x]), {k,1,n} ]

?Mod

F[x_] := f[ Mod[ x,2Pi] ]

?Which

For instance,  the absolute value of x is given by

f[x_] := Which[ x < 0, -x, x≥ 0, x]

Plot[f[x],{x,-2,2}]

Example 1.

f[x_] := Which[ 0 ≤ x ≤ 2Pi, x(2Pi-x)]

Plot[F[x],{x,-2Pi,4Pi}]

g[x_] = S[6,x]

Plot[g[x],{x,-2Pi,4Pi}]

Example 2.

f[x_] := Which[x==0, 0, 0 < x ≤ Pi/2, 1, Pi/2 <x ≤ Pi, -1, Pi < x ≤ 3Pi/2, 2, 3Pi/2 < x < 2Pi, -2, x==2Pi, 0]

Plot[f[x],{x, 0,2Pi}]

g[x_] = S[10,x]

Plot[{F[x],g[x]}, {x,0,4Pi}]

Example 3.

f[x_] := Which[ 0 ≤ x ≤ Pi/2, x(x-Pi/2), Pi/2  <x ≤Pi, 2, Pi < x ≤ 3Pi/2, -2, 3Pi/2  < x ≤ 2Pi, 2Pi-x]

Plot[f[x],{x,0,2Pi}]

g[x_] = S[7,x];

Plot[{f[x],g[x]},{x,0,2Pi}]

h[x_] = S[15,x];

Plot[{f[x],h[x]},{x,0,2Pi}]

Example 4.

f[x_] := Which[ 0 ≤ x < Pi/2, Sin[x], Pi/2 ≤ x <Pi, -1, Pi ≤ x <3Pi/2,1, 3Pi/2 ≤ x ≤ 2Pi, Sin[x]]

Plot[f[x],{x,0,2Pi}]

g[x_] = S[10,x]

Plot[{F[x],g[x]},{x,0,4Pi}]

Example 5.

f[x_] := Which [0 ≤ x < Pi, Sin[x/2], Pi ≤ x ≤ 2Pi, -Sin[x/2]]

Plot[f[x],{x,0,2Pi}]

g[x_] = S[10,x]

Plot[{f[x],g[x]}, {x,0,2Pi}]