Review of Fourier Series

Part 1. Approximation of 2π-Periodic Functions

1. Define the function f.  Because we will be plotting graphs with similar names, we will suppress the spelling warnings.

Off[General::spell1];

f[x_] :=  
        Which[x ≥ -Pi  &&  x ≤ -Pi/2, 
          x + Pi, x > -Pi/2  &&  x ≤ Pi/2, 
          x^2 - Pi^2/4 + Pi/2, x ≥ Pi/2  &&  x < 2, Pi/2, 
          x ≥ 2  &&  x < Pi, 
          Pi/2 - Pi/2*(x - 2)/(Pi - 2)];
graph1  =  
        Plot[f[x], {x, -Pi, Pi}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph1b  =  
        Plot[f[x], {x, -Pi, Pi}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

The function f extended:

fe1[x_] :=  f[x + 2*Pi];
graph2  =  
      Plot[fe1[x], {x, -3*Pi, -Pi}, 
        PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity]; 
graph2b  =  
        Plot[fe1[x], {x, -3*Pi, -Pi}, 
          PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];
fe2[x_] :=  f[x - 2*Pi];
graph3  =  
      Plot[fe2[x], {x, Pi, 3*Pi}, PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity]; 
graph3b  =  
        Plot[fe2[x], {x, Pi, 3*Pi}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

Show[graph1, graph2, graph3, 
        DisplayFunction -> $DisplayFunction];

2. Define the Fourier coefficients.  We first rewrite f in terms of the UnitStep function so that Mathematica can carry out the integration symbolically.  Convince yourself that this expression for f is correct.

f[
          x_] :=  (x + Pi)*
            UnitStep[-(x + Pi) (x + Pi/2)] + (x^2 - Pi^2/4 + 
                Pi/2)*
            UnitStep[-(x + Pi/2) (x - Pi/2)] + (Pi/2)*
            UnitStep[-(x - Pi/2) (x - 2)] + (Pi/2 - 
                Pi/2*(x - 2)/(Pi - 2))*
            UnitStep[-(x - 2) (x - Pi)];

a0  =  
      1/(2*Pi)*Integrate[f[x], {x, -Pi, Pi}] // Simplify

(1/Pi)*Integrate[f[x]*Cos[k*x], {x, -Pi, Pi}] // 
        TrigReduce;
a[k_] =  %

(1/Pi)*Integrate[f[x]*Sin[k*x], {x, -Pi, Pi}] // 
        TrigReduce;
b[k_]=%

Define the approximating polynomial.

A[x_] :=  
        a0  +  Sum[a[k]*Cos[k*x] + b[k]*Sin[k*x], {k, n}];

Compare the graphs of the approximations with the graph of f.

\(n\  = \ 1; \  (*\ An\ Example\ *) 

    graph4\  = \ 
      Plot[A[x], {x, \(-3\)*Pi, 3*Pi}, PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity];\)
Show[graph1b, graph2b, graph3b, graph4, 
        DisplayFunction -> $DisplayFunction];

3. Define the polynomial P.

P[x_] :=  
        3 - 4*Cos[x] + 2*Sin[x] + 5*Cos[2*x] + Sin[2*x];

Calculate the coefficient a[0].

1/(2*Pi)*Integrate[P[x], {x, -Pi, Pi}]

Calculate the coefficient a[1]

1/(2*Pi)*Integrate[P[x]*Cos[x], {x, -Pi, Pi}]

Calculate the other coefficients using the input cell below.

4. Integral calculation:

Integrate[P[x]*Cos[2*x], {x, -Pi, Pi}]

5. Justification:

6. Define and graph the new function f.  Note that Mathematica joins plots across asymptotes.

f[x_] :=  x - 2*Pi*Floor[x/(2*Pi)];
graph5  =  
        Plot[f[x], {x, -3*Pi, 3*Pi}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph5b  =  
        Plot[f[x], {x, -3*Pi, 3*Pi}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

Calculate the Fourier coefficients for this function.  Again, we express f in terms of UnitStep.  The expression agrees with f only on [-2π,2π], but this is large enough to cover our interval of integration.

f[x_] :=  (x + 2*Pi)*UnitStep[-(x + 2*Pi) x] + 
          x*UnitStep[-x (x - 2*Pi)];

a0  =  1/(2*Pi)*Integrate[f[x], {x, -Pi, Pi}]

(1/Pi)*Integrate[f[x]*Cos[k*x], {x, -Pi, Pi}] // 
        Simplify;
a[k_] = %

(1/Pi)*Integrate[f[x]*Sin[k*x], {x, -Pi, Pi}] // 
        Simplify;
b[k_] = %

Compare the graphs of the approximations with the graph of the function.

A[x_] :=  
        a0  +  Sum[a[k]*Cos[k*x] + b[k]*Sin[k*x], {k, n}];

n  =  1;
graph6  =  
        Plot[A[x], {x, -3*Pi, 3*Pi}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
Show[graph5b, graph6, 
        DisplayFunction -> $DisplayFunction];

Part 2. Approximation of Periodic Functions with Arbitrary Period

1 and 2. Determine the value of alpha, and check the sine graphs.

alpha  =  ?;
const  =  alpha/L;

L  =  2;
n  =  1;
Plot[Sin[n*x], {x, -Pi, Pi}, 
        PlotStyle -> RGBColor[0, 0, 1]];
Plot[Sin[const*n*x], {x, -L, L}, 
        PlotStyle -> RGBColor[1, 0, 0]];

3. Check the cosine functions using the input cell below.

4. Explanation:

5. Define and graph the function.

L  =  2;
f[x_] :=  Floor[x/2] - Round[x/2] + 1/2;

graph7  =  
        Plot[f[x], {x, -2*L, 2*L}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph7b  =  
        Plot[f[x], {x, -2*L, 2*L}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

Define the Fourier coefficients.  Note that our UnitStep formulation is valid over the interval [-2,2], and thus sufficient for the integration.

f[x_] :=  
        1/2*UnitStep[x + 2] - UnitStep[x + 1] + UnitStep[x] - 
          UnitStep[x - 1];

1/L*Integrate[f[x]*Sin[k*Pi/L*x], {x, -L, L}] // 
        Simplify;
b[k_] = %

Explanation:

Compare the function and the approximations.

A[x_] :=  Sum[b[k]*Sin[k*Pi/L*x], {k, n}];

\(n\  = \ 1; \  (*\ An\ Example\ *) 

    graph8\  = \ 
      Plot[A[x], {x, \(-2\)*L, 2*L}, PlotStyle -> RGBColor[1, 0, 0], 
        DisplayFunction -> Identity];\)
Show[graph7b, graph8, 
        DisplayFunction -> $DisplayFunction];

Part 3. Half-Range Expansions

1. Define and graph the function f.

f[x_] :=  
        Which[x > 0  &&  x ≤ 3, 1 - (x - 1)^2, 
          x > 3  &&  x ≤ 4, -3 + 
            3*(x - 3)];

graph9  =  
        Plot[f[x], {x, 0, 4}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph9b  =  
        Plot[f[x], {x, 0, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

The function g is the extension of f to [-4,4] as an odd function.

g[x_] :=  
        Which[x > -4  &&  x < 0, -f[-x], x > 0  &&  x < 4, 
          f[x]];

graph10  =  
        Plot[g[x], {x, -4, 4}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph10b  =  
        Plot[g[x], {x, -4, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

2. Compare the Fourier approximations with both f and g.

f[
          x_] :=  (1 - (x - 1)^2)*
            UnitStep[-x (x - 3)] + (-3 + 3*(x - 3))*
            UnitStep[-(x - 3) (x - 4)];

2/4*Integrate[f[x]*Sin[Pi/4*k*x], {x, 0, 4}] // Simplify;
b[k_] = %

A[x_] :=  Sum[b[k]*Sin[k*Pi/4*x], {k, n}];

The approximations on [0,4]:

n  =  1;

graph12  =  
        Plot[A[x], {x, 0, 4}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
Show[graph9b, graph12, 
        DisplayFunction -> $DisplayFunction];

The approximations on [-4,4]:

graph13  =  
        Plot[A[x], {x, -4, 4}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
Show[graph10b, graph13, 
        DisplayFunction -> $DisplayFunction];

3. The even extension h

h[x_] :=  
        Which[x > -4  &&  x < 0, f[-x], x > 0  &&  x < 4, 
          f[x]];
graph11  =  
        Plot[h[x], {x, -4, 4}, 
          PlotStyle -> RGBColor[1, 0, 0]];
graph11b  =  
        Plot[h[x], {x, -4, 4}, PlotStyle -> RGBColor[0, 0, 1], 
          DisplayFunction -> Identity];

4. The approximations:

h[x_] :=  
        f[-x]*UnitStep[-(x + 4) x] + 
          f[x]*UnitStep[-x (x - 4)];

a0  =  1/4*Integrate[f[x], {x, 0, 4}]

2/4*Integrate[f[x]*Cos[k*Pi/4*x], {x, 0, 4}] // Simplify;
a[k_] = %

cossum[x_] :=  
        a0  +  Sum[a[k]*Cos[k*Pi/4*x], {k, n}];

n  =  1;
graph12  =  
        Plot[cossum[x], {x, -4, 4}, PlotStyle -> RGBColor[1, 0, 0], 
          DisplayFunction -> Identity];
Show[graph11b, graph12, 
        DisplayFunction -> $DisplayFunction];

Part 4. Summary