OnLine4-3-FourierApprox.mws

Linear Algebra Powertool

Fourier Approximations

Online 4.3

Worksheet by Michael K. May, S.J., revised by Russell Blyth.

More on the Example, and then more Examples!

We first explore further the example given in class, and then tackle new ones.

> restart:with(plots):

Warning, the name changecoords has been redefined

Next, instruct Maple to assume that i and j are integers.

> assume(i,integer): assume(j,integer):
interface(showassumed=0):

Compute the Fourier coefficients for the function f(x) = x

> Int(x*sin(j*Pi*x),x=-1..1) = int(x*sin(j*Pi*x),x=-1..1);
Int(x*cos(j*Pi*x),x=-1..1) = int(x*cos(j*Pi*x),x=-1..1);
Int(x,x=-1..1) = int(x,x=-1..1);

Int(x*sin(j*Pi*x),x = -1 .. 1) = -2/j/Pi*(-1)^j

Int(x*cos(j*Pi*x),x = -1 .. 1) = 0

Int(x,x = -1 .. 1) = 0

We obtain the following n term Fourier approximation to the function f(x) = x.

> Fapprox := (x,n) ->
sum(-2*(-1)^j/(j*Pi)*sin(j*Pi*x),j=1..n);

Fapprox := proc (x, n) options operator, arrow; sum...

Let's plot a 6 term approximation against the function.

> eval(Fapprox(x,6));
plot([Fapprox(x,6),x],x=-2..2,y = -2..2,
title = " 6th Order Fourier approximation to y =x", titlefont = [HELVETICA,12], color = [green,blue]);

2/Pi*sin(Pi*x)-1/Pi*sin(2*Pi*x)+2/3/Pi*sin(3*Pi*x)-...

[Maple Plot]

It is also useful to plot the error , that is, the diference between the function and the approximation.

> plot(Fapprox(x,6)-x,x=-1..1,y=-1..1,
title = "error in 6th Order Fourier approximation to y = x", titlefont = [HELVETICA,10], color = red);

[Maple Plot]

Exercises:

1) Plot the function f(x) = x against the Fourier approximation with 100 terms

>

2) Plot the error function for f(x) = x and its Fourier approximation with 100 terms.

>

It is instructive to see an animation of the Fourier approximations as they converge to the function that is being approximated. The following block of code sets up frames that plot a Fourier approximations Fapprox against the function func.

> func := x:
Fapprox := (x,n) -> sum(-2*(-1)^j/(j*Pi)*sin(j*Pi*x), j=1..n):
framer := proc(n)
local A, B, C, D:
A := plot([Fapprox(x,n),func],
x = -2..2, y = -2..2, color = [blue,green]):
B := textplot({[0,1.8, `The `||n||` term Fourier approximation (blue)`],
[0,1.6, `graphed against the function (green)`]},
font = [HELVETICA, BOLD, 12] ):
C := textplot([-0.05,1.4, `f(x) = `], align=LEFT,
font = [HELVETICA, BOLD, 12] ):
D := textplot([0.05,1.4, func], align = RIGHT,
font = [HELVETICA, BOLD, 12] ):
display({A, B,C, D},view=[-2..2,-2..2]);
end:

With framer defined, it is a simple matter to animate a sequence of 30 frames.

> display([seq(framer(count), count=1..30)], insequence = true);

[Maple Plot]

To run the animation click once on the graph, then use the playback controls on the control bar.

A second example:

We would now like to look at approximations to the function that is -1 on [-1,0) and 1 on [0,1). This is a piecewise defined function. Note that we are approximating a very discontinuous function with continuous trigonometric polynomials.

> g:=x->piecewise(x<0,-1,1):
plot(g(x),x=-2..2);

[Maple Plot]

We determine the Fourier coefficients

> Int(g(x)*sin(j*Pi*x),x=-1..1) =
int(g(x)*sin(j*Pi*x),x=-1..1);
Int(g(x)*cos(j*Pi*x),x=-1..1) =
int(g(x)*cos(j*Pi*x),x=-1..1);
Int(g(x),x=-1..1) =
int(g(x),x=-1..1);

Int(PIECEWISE([-1, x < 0],[1, otherwise])*sin(j*Pi*...

Int(PIECEWISE([-1, x < 0],[1, otherwise])*cos(j*Pi*...

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

Again, the only Fourier coefficients that are nonzero are the sine ones, and even some of them are zero.

Note that the sine coefficient is 0 if j is even and 4/(jš) if j is odd.

> Fapprox := (x,n) ->
sum((-2*((-1)^j-1))/(j*Pi)*sin(j*Pi*x),
j=1..n);

Fapprox := proc (x, n) options operator, arrow; sum...

> eval(Fapprox(x,10));
plot([Fapprox(x,10),g(x)],x=-2..2,y=-2..2,
title=" 10th Order Fourier approximation to g(x)", titlefont=[HELVETICA,12], color=[green,blue]);

4/Pi*sin(Pi*x)+4/3/Pi*sin(3*Pi*x)+4/5/Pi*sin(5*Pi*x...

[Maple Plot]

Exercise:

3) Write down the 11th and 15th order Fourier approximations to g(x). Then animate the Fourier approximations up to order 30

>

4) Find the coefficients for the Fourier approximations of h(x) = x^3 .

>

5) Write down the 10th order Fourier approximation to h(x) = x^3 . Then animate the Fourier approximations up to order 30.

>

An example with nonzero cosines terms

In this third example we find Fourier approximations for the absolute value function.

> k :=x->abs(x);

k := abs

Find the Fourier coefficients:.

> Int(k(x)*sin(j*Pi*x),x=-1..1) =
int(k(x)*sin(j*Pi*x),x=-1..1);
Int(k(x)*cos(j*Pi*x),x=-1..1) =
int(k(x)*cos(j*Pi*x),x=-1..1);
Int(k(x),x=-1..1) =
int(k(x),x=-1..1);

Int(abs(x)*sin(j*Pi*x),x = -1 .. 1) = 0

Int(abs(x)*cos(j*Pi*x),x = -1 .. 1) = 2*((-1)^j-1)/...

Int(abs(x),x = -1 .. 1) = 1

Now the sine coefficients are all zero, along with half of the cosines terms - not surprising since k is an even function. Recall that the constant term is half the integral of k on the interval. Let's check:

> 1/2*Int(k(x),x=-1..1) = 1/2*int(k(x),x=-1..1);

1/2*Int(abs(x),x = -1 .. 1) = 1/2

> Fapprox := (x,n) -> 1/2 + sum(2*((-1)^j-1)*cos(j*Pi*x)/(j*Pi)^2,j=1..n);

Fapprox := proc (x, n) options operator, arrow; 1/2...

Plot the 6th Fourier approximation

> eval(Fapprox(x,6));
plot({k(x), Fapprox(x,6)},x=-2..2,y=-2..2);

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

[Maple Plot]

>

Exercise:

6) Write down the 11th and 15th order Fourier approximations to k(x) = abs(x). Animate up to the 30th order approximation.

>

7) Find the coefficients for the Fourier approximations of f(x) = x^3-x^2 . (The series has coefficients for both sine and cosine functions.)

>

8) Write down the 6th order Fourier approximation to f(x) = x^3-x^2 . Plot the function against the approximation. Animate up to the 30th order approximation.

>

>

>

>