fexamples.mw

Fourier Series Examples

Math 3350

In these pages we will use the computer algebra package Maple to do a few examples of Fourier series, including animations to show how the

partial sums of the Fourier Series converge to the function.   The examples should be informative even if  you don't know Maple.  Just

concentrate on the math and the graphs.

 > restart;

Example 1.

Consider the square wave with period , defined by

f (x)= piecewise(x<0,-1,1);

on the interval from -1 to 1.   Here is a graph on that interval.

 > f := x -> piecewise(x<0,-1,1):

 > sp :=plot(f(x),x=-1..1, color=blue): sp;

If we plot the function over several periods, it would look like this.

 > with(plots):

Warning, the name changecoords has been redefined

 > bp:=display([plot(f(x+4), x=-5..-3, color=blue, discont=true), plot(f(x+2), x=-3..-1, color=blue, discont=true), plot(f(x), x=-1..1, color=blue, discont=true), plot(f(x-2), x=1..3, color=blue, discont=true), plot(f(x-4), x=3..5, color=blue, discont=true)]): bp;

Since , the formula for is

 > a[0]:= Int(f(x),x=-1..1)/2;

 > a[0]:=value(a[0]);

 >

For we have

 > assume(m, posint); #tell the machine that m is a postive integer

 >

 > a[m] := Int(f(x)*cos(m*Pi*x),x=-1..1);

 > a[m]:=value(a[m]);

 >

Of course, it is predicable that the a's are zero, since the function is odd.

 > b[m]:=Int(f(x)*sin(m*Pi*x),x=-1..1);

 > b[m]:=value(b[m]);

 > b := unapply(b[m],m);

 >

We can use now use the function b to produce the fourier coefficients.

 > for n from 1 to 10 do print(n, b(n)); od:

 >

The following function will produce a partial sum of the Fourier Series

 > S := n -> add(b(k)*sin(k*Pi*x),k=1..n);

 >

For example, here is the partial sum out to

 > S(10);

 >

Here is an animation that shows how the partial sums converge on the interval

 > maxn := 36;

 > for n from 0 to maxn do pl[n]:= display([sp,plot(S(n), x=-1..1, title=cat("n = ", convert(n,string)))]); od:

 > display([seq(pl[n],n=0..maxn)], insequence=true);

 >

If we look at a similar animation over several periods, the picture looks like this.

 > for n from 0 to maxn do pl2[n]:= display([bp,plot(S(n), x=-5..5,numpoints=200, title=cat("n = ", convert(n,string)))]); od: display([seq(pl2[n],n=0..maxn)], insequence=true);

 >

Example 2

In this case let's try the sawtooth wave of period defined by

 > f := x -> x+1;

 > f(x);

 >

on the interval .   The graph over the Interval looks like

 > sp := plot(f(x),x=-1..1, color=blue): sp;

 >

and the graph over several periods looks like

 > bp:=display([plot(f(x+4), x=-5..-3, color=blue, discont=true), plot(f(x+2), x=-3..-1, color=blue, discont=true), plot(f(x), x=-1..1, color=blue, discont=true), plot(f(x-2), x=1..3, color=blue, discont=true), plot(f(x-4), x=3..5, color=blue, discont=true)]): bp;

 >

Next, we evaluate the Fourier coefficients.

 > a[0]:= Int(f(x),x=-1..1)/2;

 > a[0]:=value(a[0]);

 > a[m]:=Int(f(x)*cos(m*Pi*x),x=-1..1);

 > a[m] := value(a[m]);

Can you explain that?

 > b[m] := Int(f(x)*sin(m*Pi*x),x=-1..1);

 > b[m]:=value(b[m]);

 > b := unapply(b[m],m);

 > S := n -> a[0] + add(b(k)*sin(k*Pi*x),k=1..n);

 > S(10);

 >

Here's the animation on the interval

 > for n from 0 to maxn do pl[n]:= display([sp,plot(S(n), x=-1..1, title=cat("n = ", convert(n,string)))]); od: display([seq(pl[n],n=0..maxn)], insequence=true);

 >

and here is the animation over several periods

 > for n from 0 to maxn do pl2[n]:= display([bp,plot(S(n), x=-5..5,numpoints=200, title=cat("n = ", convert(n,string)))]); od: display([seq(pl2[n],n=0..maxn)], insequence=true);

 >

Example 3

Next, let's consider the triangular wave of period 2, specified by

 > f := x -> piecewise(x<0,x+1,1-x); 'f(x)'=f(x);

on the interval [-1.1].   The graph over [-1,1] looks like

 > sp := plot(f(x),x=-1..1, color=blue): sp;

and the graph over several periods looks like this.

 > bp:=display([plot(f(x+4), x=-5..-3, color=blue, discont=true), plot(f(x+2), x=-3..-1, color=blue, discont=true), plot(f(x), x=-1..1, color=blue, discont=true), plot(f(x-2), x=1..3, color=blue, discont=true), plot(f(x-4), x=3..5, color=blue, discont=true)]): bp;

 > a[0]:=Int(x+1, x=-1..0)/2 + Int(1-x,x=0..1)/2;

 > a[0]:=value(a[0]);

 > a[m] := Int((x+1)*cos(m*Pi*x),x=-1..0)+Int((1-x)*cos(m*Pi*x),x=0..1);

 > a[m]:=value(a[m]);

 > a := unapply(a[m],m);

 > b[m]:= Int((x+1)*sin(m*Pi*x),x=-1..0)+Int((1-x)*sin(m*Pi*x),x=0..1);

 > b[m]:=value(b[m]);

Can you explain that?

 > S := n -> 1/2+add(a(k)*cos(k*Pi*x),k=1..n);

 > S(10);

Here's the animation over [-1,1]

 > for n from 0 to maxn do pl[n]:= display([sp,plot(S(n), x=-1..1, title=cat("n = ", convert(n,string)))]); od: display([seq(pl[n],n=0..maxn)], insequence=true);

 >

and here is the animation over several periods.

 > for n from 0 to maxn do pl2[n]:= display([bp,plot(S(n), x=-5..5,numpoints=200, title=cat("n = ", convert(n,string)))]); od: display([seq(pl2[n],n=0..maxn)], insequence=true);

Since there are no jumps, the series converges to the function at every point.  We see the convergence is pretty fast.

Example 4

Finally, lets do an example where the function is specified over an interval other than [-L,L].

In this case, lets do the function of period 2 specified by

 > f := x -> x^2; 'f(x)'=f(x);

on the interval [0,2].   The graph on [0,2] looks like

 > sp := plot(f(x),x=0..2, color=blue): sp;

 >

Graphed over several periods, the function looks like this

 > bp:=display([plot(f(x+4), x=-4..-2, color=blue, discont=true),plot(f(x+2), x=-2..0, color=blue, discont=true),plot(f(x), x=0..2, color=blue, discont=true),plot(f(x-2), x=2..4, color=blue, discont=true),plot(f(x-4), x=4..6, color=blue, discont=true)]): bp;

 >

We can determine the Fourier Coefficents by integrating over an interval whose length is equal to the period.  So, we can compute them as follows.

 > a[0]:=Int(x^2,x=0..2)/2;

 > a[0]:=value(a[0]);

 > a[m] := Int(x^2*cos(m*Pi*x),x=0..2);

 > a[m]:=value(a[m]);

 > b[m]:=Int(x^2*sin(m*Pi*x),x=0..2);

 > b[m]:=value(b[m]);

 > a := unapply(a[m],m);

 > b := unapply(b[m],m);

 > S := n -> 4/3 + add(a(k)*cos(k*Pi*x)+b(k)*sin(k*Pi*x),k=1..n);

 > S(10);

Here is the animation on [0,2]

 > for n from 0 to maxn do pl[n]:= display([sp,plot(S(n), x=0..2, title=cat("n = ", convert(n,string)))]); od: display([seq(pl[n],n=0..maxn)], insequence=true);

and here is the animation graphed over serveral periods.

 > for n from 0 to maxn do pl2[n]:= display([bp,plot(S(n), x=-4..6,numpoints=200, title=cat("n = ", convert(n,string)))]); od: display([seq(pl2[n],n=0..maxn)], insequence=true);

 >