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 2*L = 2 , defined by

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

f(x) = PIECEWISE([-1, x < 0], [1, otherwise])

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;

[Plot]

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;

[Plot]

Since L = 1 , the formula for a[0] is

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

a[0] := 1/2*Int(PIECEWISE([-1, x < 0], [1, otherwise]), x = (-1 .. 1))

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

a[0] := 0

>

For a[m] 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] := Int(PIECEWISE([-1, x < 0], [1, otherwise])*cos(m*Pi*x), x = (-1 .. 1))

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

a[m] := 0

>

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] := Int(PIECEWISE([-1, x < 0], [1, otherwise])*sin(m*Pi*x), x = (-1 .. 1))

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

b[m] := -2*((-1)^m-1)/(m*Pi)

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

b := proc (m) options operator, arrow; -2*((-1)^m-1)/(m*Pi) end proc

>

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:

1, 4/Pi

2, 0

3, 4/3/Pi

4, 0

5, 4/5/Pi

6, 0

7, 4/7/Pi

8, 0

9, 4/9/Pi

10, 0

>

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

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

S := proc (n) options operator, arrow; add(b(k)*sin(k*Pi*x), k = (1 .. n)) end proc

>

For example, here is the partial sum out to n = 10

> S(10);

4*sin(Pi*x)/Pi+4/3*sin(3*Pi*x)/Pi+4/5*sin(5*Pi*x)/Pi+4/7*sin(7*Pi*x)/Pi+4/9*sin(9*Pi*x)/Pi

>

Here is an animation that shows how the partial sums converge on the interval [-1, 1]

> maxn := 36;

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);

[Plot]

>

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);

[Plot]

>

Example 2

In this case let's try the sawtooth wave of period 2*L = 2 defined by

> f := x -> x+1;

f := proc (x) options operator, arrow; x+1 end proc

> f(x);

x+1

>

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

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

[Plot]

>

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;

[Plot]

>

Next, we evaluate the Fourier coefficients.

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

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

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

a[0] := 1

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

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

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

a[m] := 0

Can you explain that?

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

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

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

b[m] := 2*(-1)^(1+m)/(m*Pi)

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

b := proc (m) options operator, arrow; 2*(-1)^(m+1)/(m*Pi) end proc

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

S := proc (n) options operator, arrow; a[0]+add(b(k)*sin(k*Pi*x), k = (1 .. n)) end proc

> S(10);

1+2*sin(Pi*x)/Pi-sin(2*Pi*x)/Pi+2/3*sin(3*Pi*x)/Pi-1/2*sin(4*Pi*x)/Pi+2/5*sin(5*Pi*x)/Pi-1/3*sin(6*Pi*x)/Pi+2/7*sin(7*Pi*x)/Pi-1/4*sin(8*Pi*x)/Pi+2/9*sin(9*Pi*x)/Pi-1/5*sin(10*Pi*x)/Pi

>

Here's the animation on the interval [-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);

[Plot]

>

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);


[Plot]

>

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);

f := proc (x) options operator, arrow; piecewise(x < 0, x+1, 1-x) end proc

f(x) = PIECEWISE([x+1, x < 0], [1-x, otherwise])

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

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

[Plot]

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;

[Plot]

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

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

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

a[0] := 1/2

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

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[m] := -2*((-1)^m-1)/(m^2*Pi^2)

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

a := proc (m) options operator, arrow; -2*((-1)^m-1)/(m^2*Pi^2) end proc

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

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]);

b[m] := 0

Can you explain that?

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

S := proc (n) options operator, arrow; 1/2+add(a(k)*cos(k*Pi*x), k = (1 .. n)) end proc

> S(10);

1/2+4*cos(Pi*x)/Pi^2+4/9*cos(3*Pi*x)/Pi^2+4/25*cos(5*Pi*x)/Pi^2+4/49*cos(7*Pi*x)/Pi^2+4/81*cos(9*Pi*x)/Pi^2

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);

[Plot]

>

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);


[Plot]

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);

f := proc (x) options operator, arrow; x^2 end proc

f(x) = x^2

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

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

[Plot]

>

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;

[Plot]

>

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] := 1/2*Int(x^2, x = (0 .. 2))

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

a[0] := 4/3

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

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

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

a[m] := 4/(m^2*Pi^2)

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

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

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

b[m] := -4/(m*Pi)

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

a := proc (m) options operator, arrow; 4/(m^2*Pi^2) end proc

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

b := proc (m) options operator, arrow; -4/(m*Pi) end proc

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

S := proc (n) options operator, arrow; 4/3+add(a(k)*cos(k*Pi*x)+b(k)*sin(k*Pi*x), k = (1 .. n)) end proc

> S(10);

4/3+4*cos(Pi*x)/Pi^2-4*sin(Pi*x)/Pi+cos(2*Pi*x)/Pi^2-2*sin(2*Pi*x)/Pi+4/9*cos(3*Pi*x)/Pi^2-4/3*sin(3*Pi*x)/Pi+1/4*cos(4*Pi*x)/Pi^2-sin(4*Pi*x)/Pi+4/25*cos(5*Pi*x)/Pi^2-4/5*sin(5*Pi*x)/Pi+1/9*cos(6*Pi*...4/3+4*cos(Pi*x)/Pi^2-4*sin(Pi*x)/Pi+cos(2*Pi*x)/Pi^2-2*sin(2*Pi*x)/Pi+4/9*cos(3*Pi*x)/Pi^2-4/3*sin(3*Pi*x)/Pi+1/4*cos(4*Pi*x)/Pi^2-sin(4*Pi*x)/Pi+4/25*cos(5*Pi*x)/Pi^2-4/5*sin(5*Pi*x)/Pi+1/9*cos(6*Pi*...

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);

[Plot]

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);

[Plot]

>