InClass4.3-FourierApprox.mws

4.3 Fourier Approximations

In-class demo by Russell Blyth.

> restart:with(plots):

Warning, the name changecoords has been redefined

We use Maple to perform the tedious computations for us.

Verify that the functions used as basis vectors in the approximating subspace are orthogonal.

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

Int(sin(i*Pi*x)*sin(j*Pi*x),x = -1 .. 1) = -2*(i*co...

Int(sin(i*Pi*x)^2,x = -1 .. 1) = (-cos(i*Pi)*sin(i*...

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

Int(cos(i*Pi*x)*cos(j*Pi*x),x = -1 .. 1) = -2*(-i*s...

Int(cos(i*Pi*x)^2,x = -1 .. 1) = (cos(i*Pi)*sin(i*P...

That left us with some work still to do. Try again. This time we first tell Maple that i and j are integers (Maple assumes i and j are nonzero):

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

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

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

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

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

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

Int(cos(i*Pi*x)^2,x = -1 .. 1) = 1

We need to also integrate against 1:

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

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

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

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

To compensate for this final integral, we must remember to half the coefficient of this function later.

Graph several of these integrals as a reality check:

> plot(sin(2*Pi*x)*sin(3*Pi*x),x=-1..1);

[Maple Plot]

> plot(sin(3*Pi*x)*cos(4*Pi*x),x=-1..1);

[Maple Plot]

> plot(cos(5*Pi*x)*cos(3*Pi*x),x=-1..1);

[Maple Plot]

> plot(sin(3*Pi*x)*sin(3*Pi*x),x=-1..1);

[Maple Plot]

Example

Find Fourier approximations for f(x) = x

We compute the Fourier coefficients for 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

Note there are only sine terms (not surprising because f is an odd function).

Next use Maple to find a general expression for the order n approximation

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

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

We can now plot (say) a 10 term approximation against the function.

> eval(Fapprox(x,10));
plot([Fapprox(x,10),x],x=-2..2,y = -2..2,
title = " 10th 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)-...
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 between the function and the approximation.

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

[Maple Plot]

Higher order approximations match the function f even more closely!

>

>