Using Basic Maple Programming in Elementary Mathematics Courses

Miroslaw Majewski

Inter-University Institute of Macau

Current Internet address:
E-mail: majewski@julia.iium.edu.mo
WWW: http://www.iium.edu.mo/mirek/


ABSTRACT: In this paper, I show how useful an elementary knowledge of Maple programming can be. This is a level of programming that can be very easily achieved, even with high-school students. I demonstrate how writing Maple programs will help us to explore recursive functions and trace algorithms. I also show how to develop some constructions for discrete mathematics and geometry. Most of the presented examples are taken from my recent tutorials for high-school teachers of mathematics and the work of my students.

Introduction

Various mathematical programs offer an almost unlimited number of tools and options. We used to think that such programs should be as user-friendly as possible. At the same time, our knowledge of a computer program can be quite shallow. Usually we know how to solve an equation, transform a formula, and plot a graph by clicking a button. Sometimes, we also know a few basic commands, which allow us to obtain the same effects in a command line mode. This is often all we know about a computer package for teaching mathematics. We used to think that programs requiring use a programming language are too difficult to be used in teaching mathematics. This particularly concerns Maple 6 and Mathematica.

When looking at our teaching, we may notice that teaching mathematics requires many activities that are not available in point-and-click programs. For instance, while teaching mathematics, we have to explore algorithms, use recursive functions, or build complex constructions in 2D or 3D geometry. All these activities are built out of many steps, and their nature resembles writing a kind of a program. These kinds of activities can be practiced if we use a computer program where a programming language is available. One such program is Maple 6, where the programming language is very natural and quite easy to learn.

Introduction to the Maple 6 Programming Language

This short paper is not intended to be a Maple tutorial. I wish to mention here only a few features of Maple that can be of interest to educators. If you are going to use Maple for teaching mathematics in high school or university, the book by Cheung [1] can serve as a great and compact tutorial of Maple features.

Maple offers a number of commands and control structures on various levels. For teaching high school students or university introductory classes, you need only use a basic set of commands and control structures in the most simplified form. For teaching advanced topics or more sophisticated problems, you can go into a more expanded set of Maple operations. This way, you will need to introduce your students to only a very limited set of Maple commands and control structures. In my opinion, using about 30 commands and basic control structures in the most simplified form, you can successfully introduce most of the high school topics. For undergraduate university courses you may need a bit more. You also do not need to introduce all these elements in one lesson or lecture. You can introduce them gradually as you need them. For example, for solving equations, you can use the solve command as in the following examples:

> solve(x^2-2*x-5=0,x);

> solve(x^2+1=x^4,x);

If you wish to obtain results in a decimal form you may use the fsolve command or just convert the obtained result to decimal form using evalf:

> evalf(%);

Other useful Maple commands for introductory classes are: expand, simplify, factor, combine, diff (for differentiate), int (for integrate), plot, plot3d, subs (for substitute). At this level you can show your students how to assign a mathematical expression to a variable so they can use it later without retyping a formula.

> MyEquation:=(x^2-2*x-5=0);

> MyFunction:=x/(1+x);

>plot(MyFunction, x=-0.5..1);

Until now, I was following the traditional approach to using a computer program in teaching mathematics.  However you, as well as your students, may already feel that something is missing. For example, the commands solve and fsolve work like a black box. You drop into them any equation and in most the cases you get a satisfactory solution. But if something goes wrong, you do not know why, and sometimes even you may not notice that the obtained solution is wrong. For example why does the command

> fsolve(x^2+1=x^4);

produce only two solutions? Where are the two other solutions?

One of the most fundamental problems in high school mathematics is solving quadratic equations. By introducing one or two programming instructions, you can give your students a chance to explore methods of solving quadratic equation without using the black box - the solve command. Here you have a very simple example of Maple code that was done by a high school student and below it is the Maple output produced by this code:

> A:=1:

> B:=-1:

> C:=-5:

> det:=B^2-4*A*C;

 if det>=0 then

   x1:=(-B+sqrt(det))/(2*A):

   x2:=(-B-sqrt(det))/(2*A)

 else

   print("No real solutions")

 end;


The above example is very simple, and the instruction “if … then … else” was used here in a form similar to the Pascal syntax. The control structures in Maple have a very complex form, but you can always choose the form that is the most suitable for your students. For instance the general form of the loop instruction is:

|for <name>||from <expr>||by <expr>||to <expr>||while <expr>|do <statement sequence> end do;

However, you can use it in the form “for … from … to … do … end do;” or “while … do …end do;” or even in a very short form “do…end do;”. Finally, instead of using “end do;” and “end if;” etc. you can use just “end;” like in the programming language Pascal. This feature makes Maple syntax easy to adjust to anybody’s needs. All the syntax examples mentioned here are for Maple 6. Earlier versions of Maple used slightly different syntax, but also with great flexibility.

At this stage of Maple knowledge, you can introduce Maple procedures to your students and you get an unlimited source of examples of functions. You are not limited to functions of one or two variables. For example, with a Maple procedure you can define a function that for any given quadratic equation will produce a set of its solutions or an empty set. This can be done as follows:

> CRoot:=proc(A,B,C)

 local det;

 global x1, x2, sol;

 det:=B^2-4*A*C;

   if det>=0 then

     x1:=(-B-sqrt(det))/(2*A):

     x2:=(-B+sqrt(det))/(2*A):

     sol:={x1,x2}

   else

     x1:=NULL:

     x2:=NULL:

     sol:={}:

     print("No real solutions")

   end;

 end:

Now you can use CRoot  in a similar manner to the operation solve.

> CRoot(1,-3,2);

In further calculations you can refer to the obtained set of roots or to each root separately by calling appropriate variables:

> sol;

> x1; x2;

The above examples show that the concept of programming in Maple can be easily introduced to university students as well as to some high school classes. Some university courses will especially benefit from a knowledge of Maple programming. You can find it useful while teaching discrete mathematics, finite mathematics, number theory or numerical methods.

Exploring Algorithms with Maple

Algorithms are a major part of mathematics. Most standard mathematical processes are algorithmic. Finding the greatest common divisor of two integers, finding a root of the equation with the use of a numerical method, calculating the determinant of a matrix, solving a system of linear equations, etc. are just algorithms used to produce a mathematical result. While teaching mathematics, we concentrate on teaching these algorithms rather than obtaining concrete solutions. Thousands of examples in thousands of problems books are written just in order to practice specific algorithms. Thus, using the Maple programming language, students can practice how the given algorithm works and learn its tiny secrets – secrets that are often missed in the traditional approach.

While teaching discrete mathematics, I often use the Euclid algorithm to calculate the greatest common divisor of two integers. This algorithm is quite simple. However, for some students it can be quite difficult unless I will give them an opportunity to explore its nature by writing it in the form of a Maple procedure. Here are three examples showing how students implemented this algorithm.

> Euclid1:=proc(a,b)

  local u,v;

  u:=a; v:=b;

  while u<>v do

    if u>v then u:=u-v else v:=v-u end

  end;

  return u;

  end:

> Euclid1(71755875,61735500);

> Euclid2:=proc(a,b)

  local m, n, temp;

  m:=a; n:=b;

  while n>0 do

    temp:=(m mod n);

    m:=n;

    n:=temp

  end;

  return m

  end:

> Euclid2(71755875,61735500);

> Euclid3:=proc(a,b)

   if a=b then return a else

      if a>b then return Euclid3(a-b,b) else

         return Euclid3(a,b-a)

      end

   end

 end: 

> Euclid3(71755875,61735500);

A number of other algorithms can be implemented using the Maple programming language. For instance, iterative or recursive algorithms for sorting, searching lists and other algorithms used in discrete mathematics can be easily implemented this way.

Exploring Recursion with Maple

Recursive operations are difficult for beginners. Even experienced mathematicians often prefer to use more complicated iterative algorithms instead of applying simple recursive algorithms. Let’s consider an example of a recursive function and see how it can be implemented in Maple.

Problem: The sequence {x(n)} is given by equations: x(1)=1, x(2)=2  and x(n)=3x(n-1)-2x(n-2) . Construct an algorithm to find x(99).

Solution: Using Maple “if..then..else” you can define a very simple procedure MySeq that for a given integer n will produce the n’th term of the sequence.

> MySeq:=proc(n)

   option remember;

   if n<3 then

     n

   else

     3*MySeq(n-1)-2*MySeq(n-2)

   end

 end proc:

Now you can test how the procedure works:

> seq(MySeq(i),i=1..15);

If you wish to obtain the terms of the sequence in a more convenient form, you can introduce arrays and ask Maple to develop an array containing the terms of the sequence. It can be done like this:

> MN:=array(1..1000):

  MN[1]:=1:

  MN[2]:=2:

  for n from 3 to 1000 do

    MN[n]:=3*MN[n-1]-2*MN[n-2]

  end:

Now, when Maple has produced the array with 1000 elements of the sequence, you can use them in further calculations.

> MN[99];

This way you can model and explore various recursive operations in many areas of mathematics.

Visualization of 2D and 3D objects with Maple

Maple can plot graphs of various objects including graphs of functions, implicit equations, lines, points and other objects of 2D and 3D geometry. Such a graph can be plotted on the computer screen or assigned to a variable for further use. For instance, let us consider a surface given by the equation . You can assign its plot to a variable and display it on the screen at any time. Here is how it works:

> with(plots):

> MyPlot:=plot3d(sin(x^2+y^2),x=-3..3,y=-3..3):

 display(MyPlot, axes=boxed);

Having declared another plot or a number of plots of various types you can display them together using command:

> display({Plot1,Plot2,Plot3},[plot formatting options]);

The opportunity to display various objects together in one picture can be used to model a number of interesting investigations in various areas of mathematics (see [2]). For instance, suppose that you have a 2D curve given by parametric equations x=sin(2t) and y=cos(t) and transformation X=x+7, Y=y+3. you can easily show how the curve looks and where is located after performing this transformation. Here is the Maple code.

> x:=sin(2*t): y:=cos(t):

 plot1:=plot([x, y, t=-Pi..Pi], color=blue):

 X:=x+7:

 Y:=y+3:

 plot2:=plot([X,Y,t=-Pi..Pi], color=red):

 display({plot1,plot2});

Maple has a separate package, called “geometry” that can be used for modeling geometric constructions. Here is an example showing how to create excircles for a given triangle, i.e. circles tangent to one side of a triangle and to the extensions of the other two sides.

> with(geometry):

 point(A,-1,1): point(B,2,1): point(C,0,4):

 triangle(T,[A,B,C]):

 line(L1,[A,B]): line(L2,[A,C]): line(L3,[B,C]):

 excircle (Circles,T):

 draw({op(Circles),L1,L2,L3},printtext=true);

The geometry library is one of the most underestimated Maple libraries. The above example shows that it is definitely worthwhile exploring it.

WWW and Virtual Reality with Maple

Nowadays, documents published on the Internet are part of the teaching resources that students can use for learning various disciplines. Web pages, in many situations are more convenient than printed documents. On them you can use a lot of colors without bothering about the cost of printing; you can use animations, and other elements illustrating mathematical concepts. In Maple you can save any Maple worksheet as an HTML file. Such a file after a few modifications can be placed on the web. Maple will save all graphs as GIF files and animations as animated GIFs.

Maple contains another option that is even more exciting than static web pages. With Maple you can create virtual worlds out of the mathematical objects you generate. A plot can be saved as a VRML file and linked to a web page. Objects saved in VRML files can be manipulated by the use of a mouse. You can zoom objects, rotate them, etc. Below is a screen shot of a Maple-generated Klein bottle, saved as a VRML file. Using the controls at the bottom, a VRML user could actually "fly through" the bottle and view it from the inside.

Final comments

I have been teaching discrete mathematics for a very long time. I was able to model many topics in this discipline with Pascal or using Excel spreadsheets. However, there were always topics that were very difficult or even impossible to illustrate. After switching to Maple I found that I could quite easily demonstrate most of the hard topics with the use of simple Maple programs. Moreover, Maple is interactive and its programs do not require compiling. In Pascal, for example, compiling and debugging programs was a time consuming process. The flexible programming syntax in Maple gives students more freedom in expressing their ideas. In Pascal, even a small change of syntax produces a syntax error. Modeling operations on lists, sets or graphs in Pascal required introducing pointers and dynamic structures, which was quite difficult for first year students. In Maple I was able to work with sets, lists and graphs without bothering how to implement them. In consequence, tutorials with Maple were far more productive than similar tutorials with Pascal and Excel.

Literature

[1] Cheung C-K., Keough G. E., May Michael – Getting Started with Maple, John Wiley & Sons, Inc., New York, 1998.

[2] Klimek G., Klimek M. – Discovering Curves and Surfaces with Maple, Springer, New York, 1997.