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);
The period in the periodic extension is
> a:=3;
Plot the function to check that we have got it right
> plot(f(x),x=0..a);
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
Using the formulas of lecture 13 we find:
> alpha0:=int(f(x)/a,x=0..a);
>
> an:=(int(2/a*f(x)*cos(2*n*Pi*x/a),x=0..a));
The command unapply assigns a function
to the result
> alpha:=unapply(an,n);
> bn:=(int(2/a*f(x)*sin(2*n*Pi*x/a),x=0..a));
bn is actually zero as can be seen by using the identity
with
and
, and the
fact that
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);
> plot(g(x),x=-a..3*a,numpoints=500);
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;
We wish to express this in a series on the form
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);
To assign a function to the result:
> c:=unapply(g,n);
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);
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));
> 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(0.5);
> plot(Ref(x),x=-6..6);
Let us check that the imaginary part is zero as it should be!
> Imf(1);plot(Imf(x),x=-6..6);
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
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);
> beta:=unapply(h,n);
> plot(sum(beta(n)*sin(n*Pi*x/a),n=1..10),x=-10..10);
Which looks very much like the odd periodic extension of f(x)!