15. FOURIER SERIES WITH MAPLE

> restart;with(plots):

Warning, the name changecoords has been redefined

We will give a few example of evaluation and summation of Fourier series using Maple:

EXAMPLE 1:

Trapezoidal function

f(x)=x, for 0<x <1;=1, for 1<x<2;

=2-x for 2<x<3

The first thing to do is to define f(x). One way to define a function is

f:=x->expression(x)

This definition can be extended to several variables

f:=(x,y...)->expression(x,y...)

Our function is defined piecewise and the maple command for that is

> f:=x->piecewise(x<1,x,x<2,1
,x<3,3-x):f(x);

PIECEWISE([x, x < 1],[1, x < 2],[3-x, x < 3])

The period in the periodic extension is

> a:=3;

a := 3

Plot the function to check that we have got it right

> plot(f(x),x=0..a);

[Maple Plot]

The Maple command for integration is int, the command attempts a symbolic integration, but will do it numerically if that fails

and there are no undefined constants in the expression.

SINE AND COSINE SERIES.

We want to write the series on the form

f(x) = alpha(0)+sum(alpha(n)*cos(2*n*Pi*x/a)+beta(n...

Using the formulas of lecture 13 we find:

> alpha0:=int(f(x)/a,x=0..a);

>

alpha0 := 2/3

> an:=(int(2/a*f(x)*cos(2*n*Pi*x/a),x=0..a));

an := -3/2*(2*cos(n*Pi)^2-cos(4/3*n*Pi)-cos(2/3*n*P...

The command unapply assigns a function alpha(n) to the result

> alpha:=unapply(an,n);

alpha := proc (n) options operator, arrow; -3/2*(2*...

> bn:=(int(2/a*f(x)*sin(2*n*Pi*x/a),x=0..a));

bn := 3/2*(-2*sin(n*Pi)*cos(n*Pi)+sin(4/3*n*Pi)+sin...

bn is actually zero as can be seen by using the identity

2*cos(A)*sin(B) = sin(A+B)+sin(B-A) with B = n*Pi and A = -n*Pi/3 , and the

fact that sin(n*Pi) = 0 when n is an integer. But this is perhaps something which it is easier for

a human than for Maple to see.

> g:=x->alpha0+sum(alpha(n)*cos(2*n*Pi*x/a),n=1..20);

g := proc (x) options operator, arrow; alpha0+sum(a...

> plot(g(x),x=-a..3*a,numpoints=500);

[Maple Plot]

We see that if we extend the domain function f(x) beyond 0<x<a we get the periodic extension

which also happens to be the even periodic extension. This is actually an easier way to see that

the sine terms in the general fourier series must be zero! If we include more terms in the sum we

get more accuracy.

COMPLEX FOURIER SERIES

Let us begin again

> restart:

> f:=x->piecewise(x<1,x,x<2,1
,x<3,3-x):f(x);a:=3;

PIECEWISE([x, x < 1],[1, x < 2],[3-x, x < 3])

a := 3

We wish to express this in a series on the form

f(x) = sum(c(n)*exp(2*Pi*I*n*x/a),n = -infinity .. ...

to compute the coefficients we must do the integral

> g:=1/a*int(exp(-2*Pi*I*n*x/a)*f(x),x=0..a);

g := -3/4*(-exp(-4/3*I*Pi*n)+exp(-2*I*Pi*n)-exp(-2/...

To assign a function to the result:

> c:=unapply(g,n);

c := proc (n) options operator, arrow; -3/4*(-exp(-...

Maple has trouble with the special case n=0, because of the denominator.

Must treat n=0 as aspecial case

> c0:=1/a*int(f(x),x=0..a);

c0 := 2/3

To avoid dividing by zero we must exclude n=0 from sum and add the n:=0 term separately

> Ref:=x->c0+Re(sum(c(-n)*exp(-I*2*n*Pi*x/a)+c(n)*exp(I*2*n*Pi*x/a),n=1..10));

Ref := proc (x) options operator, arrow; c0+Re(sum(...

> Imf:=x->Im(sum(c(-n)*exp(-I*2*n*Pi*x/a)+c(n)*exp(I*2*n*Pi*x/a),n=1..10));

Imf := proc (x) options operator, arrow; Im(sum(c(-...

> Imf(0.5);

0.

> plot(Ref(x),x=-6..6);

[Maple Plot]

Let us check that the imaginary part is zero as it should be!

> Imf(1);plot(Imf(x),x=-6..6);

0

[Maple Plot]

The complex Fourier series produces the periodic extension of f(x), which is also the even periodic extension.

As another example let us next consider the odd period extension! We write the function as

f(x) = sum(beta(n)*sin(n*Pi*x/a),n = 0 .. infinity)...

Using the formulas in lecture 13 we find the

SINE SERIES

> h:=2/a*int(sin(Pi*n*x/a)*f(x),x=0..a);

h := -6*(sin(Pi*n)-sin(1/3*Pi*n)-sin(2/3*Pi*n))/(Pi...

> beta:=unapply(h,n);

beta := proc (n) options operator, arrow; -6*(sin(P...

> plot(sum(beta(n)*sin(n*Pi*x/a),n=1..10),x=-10..10);

[Maple Plot]

Which looks very much like the odd periodic extension of f(x)!