Fourier Series: 
Introductory Example

First, let's check orthgonality and normalization.

Orthogonality

Compute the inner product of two Cos functions:

Integrate[Cos[n*Pi*t/L]*Cos[m*Pi*t/L],{t,-L,L}]

Note that this need NOT be zero and orthogonality is NOT guaranteed. We need to add the assumption that the frequencies are integer multiples of a fundamental frequency.

Compute inner product of Sin' s with different multiples of the fundamental frequency.

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals, m ≠  n} ,Integrate[Sin[n*Pi*t/L]*Sin[m*Pi*t/L],{t,-L,L}] ]

Compute inner product of Cos' s with different multiples of the fundamental frequency.

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals, m≠ n} ,Integrate[Cos[n*Pi*t/L]*Cos[m*Pi*t/L],{t,-L,L}] ]

Compute inner product of Sin and Cos with any multiples of the fundamental frequency.

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals } ,Integrate[Sin[n*Pi*t/L]*Cos[m*Pi*t/L],{t,-L,L}] ]

What happens if we remove the assumptions in this case?

Integrate[Sin[n*Pi*t/L]*Cos[m*Pi*t/L],{t,-L,L}]

Here, orthogonality is ensured by symmetry...

Compute squared magnitudes 
(for normalization)

Sin

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals, m== n } ,Integrate[Sin[n*Pi*x/L]*Sin[m*Pi*x/L],{x,-L,L}] ]

Cos

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals, m== n } ,Integrate[Cos[n*Pi*x/L]*Cos[m*Pi*x/L],{x,-L,L}] ]

Unity

Assuming[{m ∈ Integers, n ∈ Integers, L ∈ Reals, m== n==0 } ,Integrate[Cos[n*Pi*x/L]*Cos[m*Pi*x/L],{x,-L,L}] ]

Fourier Series for Rectangular Wave

Start by defining the function over one fundamental period:

rect[t_, d_] := Piecewise[{{-1, -d<t<0}, {1,0≤t≤d}}]

Plot[rect[t,1], {t,-2,2}]

Compute values of the Fourier coefficients

a0 = Integrate[rect[t,1], {t,-1,1}]

a= Table[
Integrate[rect[t,1]*Cos[n*Pi*t/1], {t,-1,1}],
{n,1,50}]

b= Table[
Integrate[rect[t,1]*Sin[n*Pi*t/1], {t,-1,1}],
{n,1,50}]

What we would like to obtain are partial sums that provide useful approximations to the function of interest, so define partialSum to compute a finite truncation of the Fourier Series:

partialSum[t_,nmax_] := a0/2 + 
Sum[a[[n]]*Cos[n*Pi*t/1]+ b[[n]]*Sin[n*Pi*t/1],{n,1,nmax}]

partialSum[t,3]

thick = AbsoluteThickness[3]

Plot a partial sum with two non-trivial terms and compare with the rectangular wave.

Plot[{rect[t,1],partialSum[t,3]}, {t,-1,1}, PlotStyle -> thick]

What happens when we keep more terms in the partial sum?

15 terms (8 non-trivial)

partialSum[t,15]

Plot[{rect[t,1],partialSum[t,15]}, {t,-2,2}, PlotStyle -> thick]

50 terms:

Plot[{rect[t,1],partialSum[t,50]}, {t,-2,2}, PlotStyle -> thick]

The partial sum is getting closer to the square wave, but non-trivial difference persists in a progressively narrower region near the discontinuity.

Convergence vs. Uniform Convergence

If enough terms  are included in the partial sum, does the value approach the value of the function being approximated? The numerical evidence suggests that the Fourier series we computed does converge to the rectangular wave at continuous points (and the average of left and right limits at the jumps).

Does the number of terms needed vary at different points along the time axis? Yes, we need more terms to achieve a desired accuracy near the jumps, so the convergence appears to be  non-uniform. We'll look at convergence in more detail next quarter...

Resonance

Define the periodic function. Here we take a different approach since we need the function to be defined for all time (not just over one period).

Clear[f]

f[x_,d_] := -1 /; -d <x <0

f[x_,d_] := 1 /; 0≤x≤d

f[x_, d_] := f[x-2d,d] /; x>d

f[x_, d_] := f[x+2d,d] /; x<-d

Plot[f[x,4], {x,-12, 12}]

Compute a solution of the homogeneous equation:

soln0 =NDSolve[{y''[t]+y[t]==0, y[0]==0.5, y'[0]==0},y[t],{t,0,25}]

Compute a particular solution when the period of the rectangular wave input matches the natural period of the homogeneous solution:

soln1 =NDSolve[{y''[t]+y[t]==f[t,Pi], y[0]==0.5, y'[0]==0},y[t],{t,0,25}]

Plot these solutions along with the rectangular forcing input for comparison:

Plot[{f[t, Pi],y[t] /. soln0, y[t] /. soln1}, {t,0,25}, PlotRange -> {-25,25}]

partialSum[t,7]

The first term in the Fourier expansion of the rectangular wave input is a solution of the homogeneous equation; i.e. the fundamental period of the forcing function matches the natural period of the homogeneous solution. The modification rule kicks in and resonance (magnitude that grows over time) occurs. The other frequency components are odd multiples of the fundamental.

What if the half-period of the input is 2π? The fundamental frequency is half of the original and odd multiples of 1/2 do not include the natural frequency (ω=1), so we do not expect a resonant solution.

soln2 =NDSolve[{y''[t]+y[t]==f[t,2*Pi], y[0]==0.5, y'[0]==0},y[t],{t,0,25}]

Plot[{f[t, 2*Pi], y[t] /. soln2}, {t,0,25}, PlotRange -> {-5,5}]

Indeed, the solution appears to be periodic and no resonant growth is observed.

What if the half-period of the rectangular wave is 3π? The fundamental frequency component has angular frequency ω=1/3, and the second non-trivial component has ω=3*1/3=1, so we expect a resonant response.

soln3 =NDSolve[{y''[t]+y[t]==f[t,3*Pi], y[0]==0.5, y'[0]==0},y[t],{t,0,50}]

Plot[{f[t, 3*Pi], y[t] /. soln3}, {t,0,50}, PlotRange -> {-25,25}]

Indeed, resonant growth is observed in the numerical solution. Note that each cycle of the input corresponds to 3 cycles of the output.

Below, we look at inputs with half-period 4π and 5π and include a small amount of damping.

soln4 =NDSolve[{y''[t]+0.1*y'[t]+y[t]==f[t,4*Pi], y[0]==0.5, y'[0]==0},y[t],{t,0,50}]

Plot[{f[t, 4*Pi], y[t] /. soln4}, {t,0,50}, PlotRange -> {-5,5}]

soln5 =NDSolve[{y''[t]+0.01*y'[t]+y[t]==f[t,5*Pi], y[0]==0.5, y'[0]==0},y[t],{t,0,200}]

Plot[{f[t, 5*Pi], y[t] /. soln5}, {t,0,200}, PlotRange -> {-25,25}]

Once again, a frequency component that matches the homogenous solution is what drives the resonant growth.

Asymptotic behavior/Integral/Derivative

partialSum[t,50]

Integrate[partialSum[t,15],t]

Plot[%,{t,-2,2}, PlotStyle-> Thick]

D[partialSum[t,50],t]

Plot[%,{t,-2,2},PlotRange -> {-25,25}]

Integration of a Fourier series causes the coefficients to diminish more rapidly and enhances convergence (i.e., a chosen accuracy is attained with fewer terms).

Differentiation causes the coefficients to diminish more slowly (or not at all) and achieving a given accuracy, if it is possible at all, requires keeping more terms.