Application Center - Maplesoft

App Preview:

Classroom Tips and Techniques: Teaching Fourier Series with Maple

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

Image 

Classroom Tips and Techniques: Teaching Fourier Series with Maple 

 

Robert J. Lopez
Emeritus Professor of Mathematics and Maple Fellow
Maplesoft 

Initializations 

> restart

> with(plots); -1

 

 

Introduction 

After arriving at Rose-Hulman Institute of Technology in 1985, and before the advent of Maple in the classroom in 1988, I taught Fourier series in differential equations twice in a pencil-and-paper environment. Vivid memories of how ineffective the lessons on Fourier series were still remain.  I remember homework papers with a scattering of integrals, mostly incorrectly evaluated, a sum sign or two, and a lot of confusion as to what a Fourier series was supposed to represent. 

In the very first DE course taught with Maple I noticed a remarkable difference in the section on Fourier series.  Students would come to my office with some print-out in hand, and show me a computer-drawn graph of a function and a partial sum of its Fourier series.  The two curves did not agree, so the question I'd be asked was "From the discrepancy in the two graphs, can you help me determine what error I made in the calculation of the series?"  I was amazed.  We had progressed from meaningless scribbles on paper to real insight into the behavior of the Fourier series.  You can be sure this experience provided strong support for my conviction that a tool like Maple was essential for teaching, learning, and doing mathematics at the dawn of the 21Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi( century. 

In this month's column, we begin an examination of four different Maple packages that users have written to simplify the calculation and exploration of the Fourier series.  The earliest such package, presently available from the Maple Application Center (www.mapleapps.com) was written by Wilhelm Werner, and came to my attention in 1998.  It was recently revised and re-submitted to the Application Center.  A second package by Amir Khanshan was first described in the Proceedings of the July 2006 Maple Conference, Waterloo, Ontario, Canada.  A third package, also available on the Application Center, is due to Napasin Sonjampa, a student at King Mongkut's Institute of Technology North Bangkok.  The fourth and most recent package,  announced on the MaplePrimes site (www.mapleprimes.com) August 21, 2006, is by Karel Srot. 

Before exploring the functionalities provided by these packages, this month's column will show how the Fourier series can be computed and studied with just the tools in Maple itself.  This will set the background against which the "value-added" of any of these packages can be assessed.  The following formulas fix notation for the Fourier series of suitably restricted functions Typesetting:-mrow(Typesetting:-mi( defined on Typesetting:-mrow(Typesetting:-mi(an interval of length Typesetting:-mrow(Typesetting:-mn( 

Typesetting:-mrow(Typesetting:-mi(  

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(  

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(  

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( 

In addition to fixing notation, let's also fix our terminology.  The general trigonometric series containing both sine and cosine terms will be called the sine-cosine series, while the series that contains just sine (or cosine) terms will be called the sine (or cosine) series, respectively.  The series whose coefficients are Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( will be called the exponential series.  In light of the Euler formulas Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi(the coefficients of the exponential and sine-cosine series are related by Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(and Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(

Finally, we note that on the interval Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn(, a function can have any number of Fourier representations.  First, there are the sine and cosine series representations, depending on how the function is extended to Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo(, odd in the first case, even in the second.  Moreover, each non-symmetric extension to Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo( results in a different sine-cosine series, as does taking Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(

Columns in succeeding months will address the user-contributed packages at length. 

Fourier Series in Maple 

Assumptions could not be applied to Maple variables in 1988.  This feature did not arrive until the early 1990s when the assume command became available.  Without the assumption that the series index is an integer, Maple's expressions for Fourier coefficients, computed by direct integration, will generally contain terms like Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( the first of which should simplify to 0, and the second, to Typesetting:-mrow(Typesetting:-msup(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo(.  But the assume command has continuing after-effects, often to the confusion of the user, as we show with Example 1.  In Example 2, we explore what happens in Maple when the generic coefficient is produced by an integration formula that has inherent restrictions on its applicability.  In Example 3, we explore the periodic extension of Typesetting:-mrow(Typesetting:-mi(and the convergence of a Fourier series to Typesetting:-mrow(Typesetting:-mi( and to its periodic extension.  In Example 4, we obtain a Fourier sine series and draw a bar graph of the coefficients Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(, calling this the spectrum of frequencies determined by this series.  In Example 5, we compare the exponential series with the sine-cosine series. 

Example 1 

Obtain a Fourier sine-cosine series for Typesetting:-mrow(Typesetting:-mi(

Compare functionality of assume and assuming commands 

Example 2 

Obtain a Fourier sine series for Typesetting:-mrow(Typesetting:-mi(, a series for which the generic coefficient is undefined for a specific value of the index. 

Example 3 

Obtain the Fourier series for Typesetting:-mrow(Typesetting:-mi(and graph a partial sum on Typesetting:-mrow(Typesetting:-mo(.  Compare the function, the Fourier series, and the periodic extension of the function. 

Example 4 

Obtain the Fourier sine series for Typesetting:-mrow(Typesetting:-mi(and graph its spectrum of frequencies. 

Example 5 

Compare exponential and sine-cosine series for Typesetting:-mrow(Typesetting:-mi( 

Table 1  Summary of examples implemented in Maple 

 

Example 1 

Begin with the assumption 

> assume(n, integer)
interface(showassumed = 1); -1

 

and the definitions 

> f := 1+x
L := 3

 

1+x 

3 (3.1.1)

 

Then write the following integrals for the coefficients.  

> `#msub(mi(
`#msub(mi(
`#msub(mi(

 

1/3*Int(1+x, x = -3 .. 3) 

1/3*Int((1+x)*cos(1/3*n*Pi*x), x = -3 .. 3) 

1/3*Int((1+x)*sin(1/3*n*Pi*x), x = -3 .. 3) (3.1.2)

 

There are three immediate advantages to this approach.  First, the formulas for the coefficients are typed in exactly as they might appear in a text, minimizing the chance for errors and confusion.  Second, writing the inert form of the integrals gives the user a chance to notice (or find) data-entry errors.  Third, these formulas can be "reused" by a copy/paste operation and a redefinition of just Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi(

Evaluating these integrals leads to the coefficients 

> `#msub(mi(
a := value(`#msub(mi(
b := value(`#msub(mi(

 

2 

0 

6*(-1)^(1+n)/(n*Pi) (3.1.3)

 

and hence to partial sums of the form 

> `#msub(mi(

 

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

 

The deleterious effect of the assume command is now seen when, for example, we generate a sequence of the first few coefficients Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(

> seq(b, n = 1 .. 5)

 

6*(-1)^(1+n)/(n*Pi), 6*(-1)^(1+n)/(n*Pi), 6*(-1)^(1+n)/(n*Pi), 6*(-1)^(1+n)/(n*Pi), 6*(-1)^(1+n)/(n*Pi) (3.1.5)

 

The assume command attached its assumptions to the variable Typesetting:-mrow(Typesetting:-mi(, which then became Typesetting:-mrow(Typesetting:-msup(Typesetting:-mi(a variable distinct from the Typesetting:-mrow(Typesetting:-mi( used in the seq command, and not directly addressable from the keyboard.  Everything containing Typesetting:-mrow(Typesetting:-mi( that was typed as input to Maple after the assumption was made is "contaminated" with Typesetting:-mrow(Typesetting:-mi(  Removing the assumption from Typesetting:-mrow(Typesetting:-mi( with  

> unassign('n')

does not change Typesetting:-mrow(Typesetting:-mi( to Typesetting:-mrow(Typesetting:-mi( in the expressions for the integrals or the coefficients, as we see from 

> `#msub(mi(
b

 

1/3*Int((1+x)*sin(1/3*n*Pi*x), x = -3 .. 3) 

6*(-1)^(1+n)/(n*Pi) (3.1.6)

 

Although the tendency for the tilde (~) to look like a minus sign can be overcome by a setting in the Tools/Options dialog, or even with an interface option entered from the keyboard, the inherent distinction between Typesetting:-mrow(Typesetting:-mi( and Typesetting:-mrow(Typesetting:-mi( remains exceedingly problematic for students, with relief available only with the assuming command that appeared in the late 1990s. 

Let us show how the Fourier series can be computed in Maple without having to invoke the assume command.  Recall, we have removed the assumption on Typesetting:-mrow(Typesetting:-mi(, but need to re-enter all other expressions containing it.  The integrals defining the coefficients are 

> `#msub(mi(
`#msub(mi(
`#msub(mi(

 

1/3*Int(1+x, x = -3 .. 3) 

1/3*Int((1+x)*cos(1/3*n*Pi*x), x = -3 .. 3) 

1/3*Int((1+x)*sin(1/3*n*Pi*x), x = -3 .. 3) (3.1.7)

 

and their values are 

> `#msub(mi(
a := `assuming`([value(`#msub(mi(
b := `assuming`([value(`#msub(mi(
 

 

2 

0 

6*(-1)^(1+n)/(n*Pi) (3.1.8)

 

Alternatively, we could have used the syntax 

> simplify(value(`#msub(mi(

 

6*(-1)^(1+n)/(n*Pi) (3.1.9)

 

but the assuming command proves to be much more useful in a wide spectrum of Maple calculations. 

The first few Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi('s are then  

> seq(b, n = 1 .. 10)

 

6/Pi, -3/Pi, 2/Pi, -3/2/Pi, 6/5/Pi, -1/Pi, 6/7/Pi, -3/4/Pi, 2/3/Pi, -3/5/Pi (3.1.10)

and the first 50 partial sums of the Fourier series are generated by the loop 

> for k to 50 do f || k := 1/2*`#msub(mi(

where we have suppressed what turns out to be a large output.  Figure 1 displays these partial sums via an animation. 

> p[1] := display([seq(plot(f || k, x = -L .. L, color = red), k = 1 .. 50)], insequence = true); -1
display([p[1], plot(f, x = -L .. L, color = black)])
 

 

Plot
 

 

Figure 1   Convergence of first 50 partial sums to Typesetting:-mrow(Typesetting:-mi( 

 

And yes, we deliberately avoided the complication of defining the zeroth partial sum to be Typesetting:-mrow(Typesetting:-mfrac(Typesetting:-msub(Typesetting:-mi( by adjoining this constant term to each of the first 50 partial sums containing trig functions. 

Example 2 

Using the revised formalism detailed in Example 1, we obtain a sine series for 

> f := cos(2*x)^2

 

cos(2*x)^2 (3.2.1)

 

on Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mn(where  

 

> L := Pi

 

Pi (3.2.2)

by writing 

> `#msub(mi(

 

2*Int(cos(2*x)^2*sin(n*x), x = 0 .. Pi)/Pi (3.2.3)

and computing 

> b := `assuming`([value(`#msub(mi(

 

-2*(n^2-8)*(-1+(-1)^n)/(Pi*n*(n^2-16)) (3.2.4)

The experienced eye can see that Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( is not defined by this expression.  Typically, students do not realize this, and proceed to write 

> sum(b*sin(n*Pi*x/L), n = 1 .. 6)

 

Error, (in NumericRange) summand is singular in the interval of summation

 

for a partial sum, and are perplexed by the error message so generated.  Of course, the correct procedure is to write the separate integral 

> `#msub(mi(

 

2*Int(cos(2*x)^2*sin(4*x), x = 0 .. Pi)/Pi (3.2.5)

 

with value  

> `#msub(mi(

 

0 (3.2.6)

 

Then, a partial sum of the sine series is obtained with syntax such as 

> `#msub(mi(

 

28/15*sin(x)/Pi-4/21*sin(3*x)/Pi+68/45*sin(5*x)/Pi+164/231*sin(7*x)/Pi+292/585*sin(9*x)/Pi (3.2.7)

That this partial sum approximates Typesetting:-mrow(Typesetting:-mi( faithfully is demonstrated in Figure 2 where Typesetting:-mrow(Typesetting:-mi( is in black, the partial sum is in red. 

> plot([f, `#msub(mi(
 

 

Plot
 

 

Figure 2   

In black, the graph of Typesetting:-mrow(Typesetting:-mi(in red, the graph of a partial sum of the Fourier sine series for Typesetting:-mrow(Typesetting:-mi( 

 

Example 3 

The periodic extension of a function Typesetting:-mrow(Typesetting:-mi( whose rule is Typesetting:-mrow(Typesetting:-mi( and whose domain is Typesetting:-mrow(Typesetting:-mi(is the periodic function Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi(of period Typesetting:-mrow(Typesetting:-mn(, with rule Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi( and domain Typesetting:-mrow(Typesetting:-mo(where Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mo( is the greatest integer function. 

This definition can be implemented in Maple by using floor as the greatest integer function.  For example, let Typesetting:-mrow(Typesetting:-mi(with domain Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi(  If we write 

> f := proc (x) options operator, arrow; x^2 end proc; -1
('f')(x) = f(x)

 

f(x) = x^2 (3.3.1)

 

the rule for Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi( becomes 

> f(x+2*floor(`*`(1-x, 1/2)))

 

(x+2*floor(1/2-1/2*x))^2 (3.3.2)

 

because Typesetting:-mrow(Typesetting:-mn(  Figure 3 verifies that f(x+2*floor(`*`(1-x, 1/2))) reproduces Typesetting:-mrow(Typesetting:-mi( periodically. 

> p[2] := plot((x+2*floor(1/2-1/2*x))^2, x = -5 .. 5, discont = true, color = red); -1
p[3] := plot(f(x), x = -1 .. 1, color = black, thickness = 3); -1
display([p[2], p[3]], scaling = constrained)
 

 

Plot
 

 

Figure 3   In black, Typesetting:-mrow(Typesetting:-mi(; in red, its periodic extension 

Denote the Fourier series of Typesetting:-mrow(Typesetting:-mi(by Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi(.  (The function Typesetting:-mrow(Typesetting:-mi( has rule Typesetting:-mrow(Typesetting:-mi( and domain Typesetting:-mrow(Typesetting:-mfenced(Typesetting:-mrow(Typesetting:-mi()  If Typesetting:-mrow(Typesetting:-mi( is suitably continuous, and Typesetting:-mrow(Typesetting:-mi(then Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi(the periodic extension of Typesetting:-mrow(Typesetting:-mi(.  Thus, under the right conditions, the Fourier series represents Typesetting:-mrow(Typesetting:-mi( on its domain, and outside its domain represents the periodic extension of Typesetting:-mrow(Typesetting:-mi(

Indeed, the Fourier series for Typesetting:-mrow(Typesetting:-mi( reduces to a cosine series because Typesetting:-mrow(Typesetting:-mi( is an even function.  The coefficients are defined by the integrals 

> `#msub(mi(
`#msub(mi(

 

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

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

 

whose values are 

> `#msub(mi(
a := `assuming`([value(`#msub(mi(

 

2/3 

4*(-1)^n/(n^2*Pi^2) (3.3.4)

A partial sum for this cosine series, namely, 

> F := 1/2*`#msub(mi(
 

 

1/3-4*cos(Pi*x)/Pi^2+cos(2*Pi*x)/Pi^2-4/9*cos(3*Pi*x)/Pi^2+1/4*cos(4*Pi*x)/Pi^2-4/25*cos(5*Pi*x)/Pi^2+1/9*cos(6*Pi*x)/Pi^2-4/49*cos(7*Pi*x)/Pi^2+1/16*cos(8*Pi*x)/Pi^2-4/81*cos(9*Pi*x)/Pi^2+1/25*cos(10...
1/3-4*cos(Pi*x)/Pi^2+cos(2*Pi*x)/Pi^2-4/9*cos(3*Pi*x)/Pi^2+1/4*cos(4*Pi*x)/Pi^2-4/25*cos(5*Pi*x)/Pi^2+1/9*cos(6*Pi*x)/Pi^2-4/49*cos(7*Pi*x)/Pi^2+1/16*cos(8*Pi*x)/Pi^2-4/81*cos(9*Pi*x)/Pi^2+1/25*cos(10...
(3.3.5)

 

is graphed in Figure 4. 

> plot(F, x = -5 .. 5, scaling = constrained)
 

 

Plot
 

 

Figure 4    

For Typesetting:-mrow(Typesetting:-mi(the Fourier cosine series Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi(the periodic extension of Typesetting:-mrow(Typesetting:-mi( 

 

 

If Typesetting:-mrow(Typesetting:-mi( has jump discontinuous, or if Typesetting:-mrow(Typesetting:-mi(then at each point its Fourier series converges to the average value of the limits from the left and right.  Since this is not easily shown with a partial sum, it is difficult to get Maple to demonstrate that at discontinuities the Fourier series converges to a point midway between the jump. 

In any event, this means the statement "The Fourier series of Typesetting:-mrow(Typesetting:-mi( converges to the periodic extension of Typesetting:-mrow(Typesetting:-mi(" is not necessarily a true statement.  The periodic extension of Typesetting:-mrow(Typesetting:-mi( will reproduce any jump discontinuities but the Fourier series will converge to a point midway between any jump.  Therefore, in general, Typesetting:-mrow(Typesetting:-mover(Typesetting:-mi(the difference being the behavior at any jump discontinuities. 

Example 4 

The Fourier sine series for Typesetting:-mrow(Typesetting:-mi( is easily obtained if we write 

> f := x
L := Pi

 

x 

Pi (3.4.1)
 and the integral 

> `#msub(mi(

 

2*Int(x*sin(n*x), x = 0 .. Pi)/Pi (3.4.2)

whose value is 

> b := `assuming`([value(`#msub(mi(

 

-2*(-1)^n/n (3.4.3)

 

Figure 5 then contains a bar graph of the coefficients Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( plotted against Typesetting:-mrow(Typesetting:-mi(.  As such, it is a representation of the spectrum of frequencies in the sine series for Typesetting:-mrow(Typesetting:-mi(a visual representation of the coefficients that multiply each sinusoid in the Fourier series. 

> plot(eval(b, n = floor(n+1/2)), n = 0 .. 10.5, filled = true, scaling = constrained)
 

 

Plot
 

 

Figure 5   Spectrum of frequencies in the sine series for Typesetting:-mrow(Typesetting:-mi( 

 

Example 5 

If the rule for Typesetting:-mrow(Typesetting:-mi( is given by   

> Typesetting:-mrow(Typesetting:-mi(

 

piecewise(x < 0, 1+x, (x-1)^2) (3.5.1)

 

and the domain is the interval Typesetting:-mrow(Typesetting:-mo(so that Typesetting:-mrow(Typesetting:-mi(, obtain its Fourier exponential series and compare the resulting Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( to the coefficients of its Fourier sine-cosine series.  Figure 6 provides a graph of the function Typesetting:-mrow(Typesetting:-mi( 

> plot(f, x = -1 .. 1, scaling = constrained)
 

 

Plot
 

 

Figure 6   Graph of Typesetting:-mrow(Typesetting:-mi( in Example 5 

 

The coefficients Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( are given by the integrals 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-mi(

 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( 

1/2*Int(piecewise(x < 0, 1+x, (x-1)^2)*exp(-I*n*Pi*x), x = -1 .. 1) (3.5.2)
 whose values are 

> `#msub(mi(
c := `assuming`([evalc(value(qc))], [n::integer])

 

5/12 

(-1/2*(-1)^n+1/2)/(n^2*Pi^2)+1/(n^2*Pi^2)+I*(1/2/(n*Pi)-1/2*(n^2*Pi^2-2+2*(-1)^n)/(n^3*Pi^3)) (3.5.3)

 

The coefficients in the sine-cosine series are given by the integrals 

> Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(
Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(

 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( 

Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi( 

Int(piecewise(x < 0, 1+x, (x-1)^2)*sin(n*Pi*x), x = -1 .. 1) (3.5.4)

 

whose values are 

> `#msub(mi(
a := `assuming`([simplify(value(`#msub(mi(
b := `assuming`([simplify(value(`#msub(mi(

 

5/6 

-((-1)^n-3)/(n^2*Pi^2) 

2*(-1+(-1)^n)/(n^3*Pi^3) (3.5.5)

 

To establish that Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(Typesetting:-mrow(Typesetting:-msub(Typesetting:-mi(, we interrogate Maple as follows. 

> is(`#msub(mi(
`assuming`([is(a = 2*Re(c))], [n::integer])
`assuming`([is(b = -2*Im(c))], [n::integer])

 

true 

true 

true (3.5.6)

 

A partial sum of the Fourier exponential series, and the corresponding partial sum of the sine-cosine series are given respectively by 

> FC := `#msub(mi(
FSC := 1/2*`#msub(mi(

 

5/12+1/4*exp(-2*I*Pi*x)/Pi^2+(2/Pi^2+I*(-1/2/Pi+1/2*(Pi^2-4)/Pi^3))*exp(-I*Pi*x)+(2/Pi^2+I*(1/2/Pi-1/2*(Pi^2-4)/Pi^3))*exp(I*Pi*x)+1/4*exp(2*I*Pi*x)/Pi^2 

5/12+4*cos(Pi*x)/Pi^2-4*sin(Pi*x)/Pi^3+1/2*cos(2*Pi*x)/Pi^2 (3.5.7)

 

That these two expressions are identical is shown by an application of Euler's formulas, implemented in Maple via 

> evalc(FC)

 

5/12+4*cos(Pi*x)/Pi^2-4*sin(Pi*x)/Pi^3+1/2*cos(2*Pi*x)/Pi^2 (3.5.8)

 

Legal Notice: The copyright for this application is owned by Maplesoft. The application is intended to demonstrate the use of Maple to solve a particular problem. It has been made available for product evaluation purposes only and may not be used in any other context without the express permission of Maplesoft.  

Image