Introduction to Maple in the Undergraduate Computer Lab by James A. Carlson and Jennifer M. Johnson Department of Mathematics University of Utah Salt Lake City, Utah 84112

September, 1995

Maple is a general purpose software package containing tools useful in the study of a wide variety of problems. We can use Maple like a calculator to carry out numerical computations or we can use the programming features of Maple to produce a more complicated sequence of computations. In addition to numerical computation, we can do symbolic or algebraic computations in Maple. This package has built-in functions for solving many kinds of equations, for working with matrices and vectors, for differentiation and integration. It also contains functions which allow us to create visual representations of curves and surfaces from their mathematical descriptions.

Using a software package like Maple gives new ways to think about problems. For example, we can use the computer to run numerical experiments. This means that we can not only look at more realistic versions of standard problems, but we can easily look at many more examples, in the process developing a better intuitive feel for the essential features of a general problem and its solutions. Often these experiments suggest further questions or interpretations that might otherwise go unnoticed. Patterns that appear in these experiments give insight into and motivation for the standard approaches to these problems — and frequently highlight the limitations of these methods.

In many situations the graphics features of Maple are a useful tool for visualizing the geometric ideas behind important definitions and standard techniques. Again, this goes a long way toward giving a better feeling for the essentials.

In the beginning computing can be frustrating because we are used to dealing with people, who are intelligent and cn undrstond whot u meen evn if it isn’t quite right from a literal point of view. Anyway, you will soon get past this stage as the right moves become automatic. Eventually computers will be smarter and better designed, and this will make life easier for all of us.

Getting Started

These notes were prepared for use on the uglab machines in the Math Department’s Undergraduate Computer Lab. The alpha machines and the ceslab machines are similar; to play it safe get acquainted with the system on the uglab machines — you can then easily figure out how to use the other computers in the lab. \underline{LOGGING IN}: This is what you do to start working on the computer. First you have to figure out your login name and your password. All student login names begin with the prefix c- and the rest of your login name is built from your initials as explained below. Your password is built from your initials and your social security number. Both Unix and Maple are case sensitive, so pay attention to upper and lower case in the examples:

\underline{NEW SYSTEM:} Here your login name is built from the c- prefix followed by the first and last letters of your last name, the first letter of your first name, and your middle initial. For example, if your name is \underline{K}arl \underline{F} red \underline{G}aus\underline{s}, then your login name is c-gskf. If in addition your social security number is 550-00-1234 then your password

    is gskf1234.

\underline{OLD SYSTEM:} Here your login name is built from the c- prefix followed by the first letter of your last name, the first letter of your first name, and your middle initial. For example, if your name is \underline{K}arl \underline{F} red \underline{G}auss, then your login name is c-gkf. If your social security number is 550-00-1234, then your password is gkf1234.

\underline{NOTE:} If there is more than one Karl Fred Gauss, then the login names will be c-gkf1, c-gkf2,... in the old system or c-gskf1, c-gskf2,... in new system. However, if the first Gauss’s social security number is 550-00-1234, then his initial password is still gkf1234 in the old system or gskf1234 in the new system.

Once you think you know your login name and password, look at the screen. If it is dark, jiggle the mouse. The screen should brighten, and a little rectangle with the word “Login:” should appear. Type your login name. Press return. Then type your password, and press return again. Repeat if this fails (You will get a message that Login failed). You may have misstyped your password or you may not have typed the correct login name. The lab TA on duty or your instructor can help you figure out your login name if you have trouble.

At a time other than this introductory session you should change your password using the passwd command. Don’t choose a word in the dictionary or a proper name. Composites of dictionary words, like strawdog are good. Even better is to use one or two upper case letters, e.g., strAwdog. For still more security, add some digits, e.g. strAwdog734. Note that it takes about 30 minutes for your new password to take effect. Also, you should be aware that if your password is not changed within the first two weeks of class, then your computer account will be disabled for security reasons.

\underline{THE LOCAL WINDOW:} after a delay, long or short depending on processor speed, system, load, phase of moon, etc., a window named local will appear. Notice that it has various parts: borders on the top (title bar), borders on the side (scroll bars), etc. \underline{THE MOUSE AND THE CURSOR:} When you move the mouse around, it moves a pointer on the screen. This is called the cursor. Move the cursor now. Notice how it changes shape as you move inside the window, then move outside to the “background.” Notice also how the cursor changes shape as you move it to various parts of the window. \underline{LOGGING OUT:} Just as important as logging in is logging out. You do this when you finish work (or when you leave the room). To log out, move the cursor to the background, press the left mouse button, and choose the last menu item: Exit X-Windows. Don’t do this now, but be aware of how it is done. You will need it later.

Basic Window Stuff

\underline{DISAPPEARING WINDOWS:} If you click on the title bar with the left mouse button it will disappear. Actually, it is still there in miniature form. Look at the top of the screen. Both on the left and right you will see little rectangles labelled local. Clicking on such a rectangle with the left mouse button causes the window to reappear. Practice making windows disappear and reappear.

\underline{TYPING:} To type something, the cursor must be inside a window. Put it inside the local window and type whoami and press return. Put it outside the local window (on the screen background) and type the same thing. Did you notice a difference? In the first case you successfully gave a command to the computer, who (of course) obeyed. Practice typing commands. \underline{MOVING WINDOWS:} Press on the title bar with the right mouse button, drag to a new position, and release. Practice! \underline{RESIZING WINDOWS:} Press on the square in the upper right corner of the window with the right mouse button, drag, and release. Note that you must enlarge the window before you can shrink it. In other words, to make a window more narrow, you must hold down the mouse and drag to make the window a bit wider than it was originally; then still holding down the mouse button, drag the mouse in the opposite direction until you have the desired width and then release the mouse button. Practice! You might also try what happens if you click the left mouse button on the other small square in the upper right corner of your window. What happens if your click on that box a second time?

Starting a Maple File

\underline{STARTING UP:} Move the cursor into the local window and type xmapleV3 & to open a Maple window. Presently the ghost of a new window will appear in a kind of “dotted outline form.” Move the mouse to position this window and click with left mouse button when satisfied. (Try to place windows so that part of the title bar is visible for each window.) \underline{GOING FROM ONE WINDOW TO ANOTHER:} Practice moving the mouse from the local window to the Maple window. Observe the change in appearance of title bar. A dark title bar indicates the active window. The active window can be behind another window. To bring a window forward, click on its title bar with the middle mouse button. Practice this. (Sometimes the mouse gets a bit wild and hard to control so you may end up with a window whose title bar is completely obscured. In that case, you can move and resize windows as explained above to get access to the title bar.) If you need help, ask! Getting used to the mouse and multiple windows can be very frustrating at first, especially if you are unwilling to get help when you need it. \underline{SAVING YOUR FILE:} At the top of your Maple window you will see several menus. Click on the File menu with the left mouse button. To name a Maple file click on the option Save As with the left mouse button. A Save Session window appears: If the box labelled Selection near the bottom of this window is empty and you want to save your Maple file under the name hw1 (for example), then click anywhere in the Selection box with the left mouse button and type hw1. Then press the return key or click on the Save box at the bottom of the Save Session window.

(It sometimes happens that the Selection box appears containing your full computer name. In that case you must place the cursor at the end of the name that appears in the box (after the /) and type your file name, etc.)

If this process was successful then the name hw1.ms should appear at the top of the Maple window. Maple adds the .ms extension to identify the saved file as a Maple file. To avoid headaches you should use some care in naming your files. Keep it simple. Use only letters and numbers in your file names. Don’t use any special symbols and in particular, avoid using spaces, periods, and hyphens.

All you have done so far is to save a blank file. As you continue working you will need to update your saved file. Do this by clicking on the File menu and then the option Save in that menu. Do this frequently. Power failures or Maple memory failures can occur without warning at any time. In that case you will lose all work done since the last time you saved the file.

A Sample Maple Session

The following examples give an introduction to a few basic Maple commands and further technical details of using Maple in the Undergraduate Lab.

Example: Find the point of intersection of the two lines defined by 3x + 2y = 5 and 2x + 4y = 5 in the plane. Use Maple to find the intersection algebraically and graphically.

Solution: The first line of your Maple window should contain the symbol >. This is called the Maple prompt. It is an indication that Maple is waiting for your next command. It is of course an easy matter to solve this system by hand, but for larger systems with more equations the help of a machine will be very welcome. To solve in Maple, we write:

      > solve( { 3*x + 2*y = 5, 2*x + 4*y = 5 } );

and Maple responds with this:

{x = 5/4, y = 5/8}.

Except for minor (but important) differences of punctuation, we formulate the problem for Maple in the same way as we do for a fellow mathematician. Note that we give the system of equations as a set: a list of items separated by commas and enclosed by curly braces. This tells Maple to apply the function solve simultaneously to both equations. Typically, a Maple command takes a set as its argument if the order of the elements is not important — in other words, since nothing about this problem changes if we give the equations in the other order, we give the list of equations as a set. The solution is also a set — its elements are the simplified equations.

Notice that Maple commands end with a semicolon. Maple is very finicky about such things. It will not carry out any of your instructions until you end the command properly.

For the most part, spacing is not important in a Maple command, although generous use of the space bar makes your commands much easier to read.

Often you will want to enter a command that is a small variation on an earlier command. In that case instead of retyping you can use the mouse to copy an old command and then paste it onto a new command line. Then you can move the cursor into the command and make modifications. COPYING AND PASTING WITH THE MOUSE:

• Press the left mouse button at the beginning of the text you want to copy.

• Hold the button down and drag the mouse until you have blackened the desired text. Release the mouse button.

• Move the cursor to the position where you want to paste. Click the left mouse button to mark this spot.

• Click the middle mouse button to paste. For example, notice that in our intersecting line problem Maple gave the result in

fractional form. If possible, Maple tries to use algebraic methods to find solutions exactly. If we rephrase the question using decimals instead of integers for even one of the coefficients, then Maple approaches the question very differently and we get answers in decimal form:

    > solve({3*x + 2*y = 5.0, 2*x + 4*y = 5});

gives (1.250000000, 0.6250000000) as the point of intersection.

Use the mouse to copy and modify the solve command to get an answer in decimal form as described above.

You can paste repeatedly—the copied text stays in memory and anytime you press the middle mouse button it will be pasted at the current cursor position. This can cause some problems for you if you accidentally hit the middle mouse button later on so you may want to copy some blank space to clear the copied text.

If you made some typing mistakes in your command you may have gotten a syntax error. This means that Maple doesn’t understand your command. With practice you will be able to use Maple’s error messages to get clues about what kind of mistake you have made but at first they will probably be a bit mysterious. For example, you may see that you left out one of the * symbols or that you have typed = instead of +. To correct this kind of error you need not retype the entire command. You can use the arrow keys to move the cursor back into the line and then use the backspace key to erase your mistake. Insert the missing symbol and press Return. Maple will attempt to carry out your instructions. (You don’t need to move the cursor to the end of the line. Maple will just look at the line the cursor is in.) Another common error is to forget to type the semicolon at the end of your command. You can move back into the line and add the semicolon as with the other typos. However, for a mistake like this you will probably have to enter the command twice before Maple gets it straight.)

Another way we could use the solve command is to solve the equation 3x + 2y = 5 for y as a function of x:

    > solve( 3*x + 2*y = 5, y);

We see that the line 3x + 2y = 5 is the graph of the function f_{1}(x) = −3x/2 + 5/2. Maple has a built-in command plot for producing graphs of curves in the plane. To obtain a graph of the line for x ∈ [−2, 2] we use the command:

    > plot( -3*x/2 + 5/2, x = -2..2);

In this command we see an example where spacing is important in Maple; no space is allowed between the dots in this command. A new window, called a plot window should appear in response to this command. This plot can be printed by itself or it can be pasted into your Maple window to be printed later (see below).

We have graphed the line given by the first equation and we can modify our work above to get a plot that contains both lines.

     > solve( 2*x + 4*y = 5 , y);

tells us that the second line is the graph of the function f_{2}(x) = −x/2 + 5/4 and we can tell Maple to graph f_{1} and f_{2} together:

    > plot( {-3*x/2 + 5/2,  -x/2 + 5/4}, x = -2..2);

Again notice the use of braces to tell Maple to apply the command plot to both functions. We see from the resulting plot that the lines meet at the point (5/4, 5/8) = (1.25, 0.625) as expected. Use the left mouse button to click on the intersection point; the coordinates of this point will then appear in the upper left corner of your plot window.

It is often a good idea to give your plot an explanatory title:

    > plot( {-3*x/2 + 5/2,  -x/2 + 5/4}, x = -2..2,
                title = ` the lines 3x + 2y = 5 and 2x + 4y = 5 `);

The quotation mark here is the backquote, located in the upper left corner of the lab keyboard; don’t confuse it with the single quote. Also, note that this command is too long to fit on a single line. To enter a multiline command in Maple just press the Enter key instead of Return when you finish typing the first line of the command. When the command is complete and you are ready for Maple to carry it out, press the Return key.

You should be aware that Maple chooses the scale on the x-axis and the y-axis independently. To force Maple to use the same scale on both axes you can choose the option Constrained in the Projection menu in the plot window. Another way to achieve the same thing is to specify the range of variation for y in your Maple command:

    > plot( {-3*x/2 + 5/2, -x/2 + 5/4}, x = -2..2, y = -2..2);

or to specify the scaling option in your plot command:

    > plot( {-3*x/2 + 5/2, -x/2 + 5/4}, x = -2..2, scaling = constrained);

PRINTING A PLOT:

• In the plot window click on the File menu with the left mouse button.

• Click on the option Print. A new menu will appear.

• Click on the option Postscript. A small window will appear.

• If you click on the box labelled Confirm or hit the return key you will create a printable file named plotoutfile.ps.

• To actually print the plot you must return to the local window and type print plotoutfile.ps . The plot will be enlarged to fill one standard size page.

To give the printable version of your plot a name of your own choosing – for example, intersection.ps – click in the box labelled Output Filename, backspace to erase plotoutfile.ps and type intersection.ps instead. Then press Return or click to Confirm. Then print by typing print intersection.ps in your local window. This is useful if you want to delay printing. Any time you save a plot under the name plotoutfile.ps you replace the last plot you did with the new plot. So if you want to save several plots for later printing you need to give the files different names. \underline{Remark:} The extension .ps after a file name should mean that the file is a postscript file. All your plot files and the printable version of your Maple file are of this type. It is very helpful to indicate the file type with an appropriate extension as part of the file name. \underline{PASTING A PLOT INTO YOUR MAPLE WINDOW:} Rather than printing the plot out separately you may prefer to include the plot in your Maple window and then print out your entire Maple session instead. This has the advantage that the plot appears with the commands that produced it. To do this

• Click on the Edit menu in the plot window.

• Click on the option Copy.

• Go to your Maple window and place the cursor anywhere in the line that contains the command which produced the plot.

• Click on the Edit menu in your Maple window and choose the option Paste. The plot should then appear in your Maple window. To continue working just use the arrow keys or click to move the cursor into the next command line. (It may happen that you don’t see a Maple prompt > at the end of your file. You can get a new prompt by going to the Edit menu and selecting the option Insert Prompt. This can also be achieved using key commands—hold down the “compose character” key (referred to as “Alt” in the instruction in the Maple menus) while you press the “I” key and a new prompt will appear below the line the cursor is in.)

Your plot will take up about half a page in the printed version. Often you won’t need the plot to be so large. You can make the plot smaller (see directions above for resizing a window) before you paste it into your Maple file.

To get rid of a plot window click on the File menu in the plot window and choose the Exit option. If you change your mind and you want to get the plot back, just move the cursor into the command line that produced the plot and press Return. \underline{USING MAPLE HELP FILES:} In the example above we saw two ways to use the solve command in Maple. Are there others? We can ask Maple for more information about the solve command by typing ? solve or ?solve after the prompt. If we do this a help window will appear, containing a lot of (probably) incomprehensible information about the command solve. If you scroll to the end of the help window then you will see a list of examples. At first these examples won’t be very easy to use but as you get more familiar with Maple these will become quite useful for figuring out how to get Maple to work for you.

So far we have looked at lines in the plane and we’ve seen an example of two useful Maple commands—plot and solve. Next we look at some three dimensional examples.

Example: Let P be the plane given by the equation 3x + y − 2z = 2. Use Maple to plot this plane.

Solution: Maple has a built-in command plot3d for drawing surfaces in 3-space, but we have to describe the surface in the terms Maple expects. Although we can specify our plane with a linear equation as above, the command plot3d will not accept this description of P (we would get a syntax error). However, this plane can also be described as the graph of a function of two variables, namely as the graph of f(x, y) = (3x + y − 2)/2. From this description we can get a plot of P if we specify a range of x-values and of y-values over which to graph the function f:

    > plot3d( (3*x + y - 2)/2, x = -3..3, y = -3..3);

This command produces a 3d-plot window containing a graph of P, but without axes it is difficult to get any information from this picture. We cannot see how the plane lies in 3-space. At the top of the 3d-plot window are several menus. Choose the option Normal in the Axes menu. The plane disappears and is replaced with a box. Click anywhere in the plot window with the middle mouse button to redraw the plane P, now with axes. It is still difficult to interpret this picture because it is difficult to see how the axes as drawn relate to the plane. Try some other choices of axes style from the menu. The options Boxed and Framed are often better choices for surfaces. We can tell Maple to include axes by adding an option to our plot3d command. (This is a good chance to practice using the mouse to copy and modify an earlier command.)

    > plot3d( (3*x + y - 2)/2, x = -3..3, y = -3..3, axes = boxed );

The surface in this case is very simple, but we also need to understand how this 2dimensional object sits in 3-space. To get a better understanding of this we can turn the surface and look at it from different angles. Click on the plot with the left mouse button. The box should reappear. Hold down the left mouse button and drag to move the box. Click with the middle mouse button to redraw the plot. Experiment with different views of the plane. Experiment with the choices in the other menus as well. Recall that the option Constrained in the Projection menu forces Maple to use the same scale on all the coordinate axes.

Another important feature of Maple, assignment statements, are illustrated next.

Example: Use Maple to solve the system

\begin{aligned}3x + y − 2z & = 1 \\ 2x − 3y + 5z & = 2 \\ 4x − 5y − 3z & = 3. \end{aligned}

Algebraic Solution: We can define these equations in Maple, using the symbol := (read “gets”) to assign a name to each of them for future reference.

       > e1 := 3*x + y - 2*z = 1;
         e2 := 2*x - 3*y + 5*z = 2;
         e3 := 4*x - 5*y - 3*z = 3;

Then to solve the system:

    > solve({e1, e2, e3});

\underline{Warning:} Assignment statements are the source of two very common and hard-to-spot syntax errors — typing = instead of := and typing : = instead of :=. Geometric Solution: We can plot these three planes together, but we have to describe each plane as the graph of a function of two variables:

    > f1 := solve( e1, z);
      f2 := solve( e2, z);
      f3 := solve( e3, z);
    > plot3d( {f1, f2, f3}, x = -2..2, y = -2..2, axes = boxed);

In our examples we have seen a few of the built-in features of Maple. For a list of the functions available in Maple type

    > ?library

In addition to this standard library of functions there are specialized packages of functions for working with special topics— for example there is a collection of functions, called the linalg package for working with matrices and vectors and there is also a collection of plotting functions in the plots package. We will frequently use both these packages. To use the functions in these packages you must load them into Maple’s working memory at the beginning of your Maple session using the with command.

    > with(linalg);   # the warning you see is normal --- ignore it
    > with(plots);

For more information type

    > ?packages

Often it will take more than one session at the computer to complete your analysis. You will need to save your work and come back to the lab and open it up again to continue working. Often it will be useful to take with you a printed copy of your Maple session so you can think about what you have done so far and how you want to continue when you come back to the lab or so that you can talk to your instructor or your lab partner about any questions you may have.

\underline{EDITING YOUR MAPLE FILE:} Before you print your Maple session you should clean it up a bit. In the Edit menu of your Maple window you will find some useful commands for this purpose. For example the option Delete Cursor Region will delete whatever line the cursor is in. Note that holding down the “control” key while you hit the “delete” key will have the same effect. This gives you a quick way to erase your mistakes.

Also in the Edit menu you find the option Delete by Type. Choosing this option brings up another menu. Most of these choices are rather drastic and self-explanatory. Try the option Delete all Separators.

It is also a good idea to include some explanations with your work. There are two ways to do this. First you can use comments. In any command line, Maple ignores anything typed after the symbol #. So inserting a line like

    > solve( 3*x + 2*y - 5*z = 1, z); # Get z in terms of x and y:

makes your work much easier to follow. Notice that you can move anywhere you like inside your Maple window with the arrow keys or the mouse and the option Insert Prompt in the Edit menu allows you to insert a command line.

Also in the Edit menu you find the option Insert Text. This allows you to type some explanations into your Maple file. However, editing is very crude and there is no way to get decent looking mathematical formulas with Maple’s text mode so if you want to say something complicated you may be better off with a handwritten note. Also be careful using Delete Cursor Region with text. Maple will delete the whole section of text rather than the line the cursor is in. (Try Display Region Boundaries in the View menu of your Maple window.) PRINTING YOUR MAPLE SESSION:

• Click on the File menu and choose the option Save to update your file.

• Click on the File menu; again choose the option Print. A Page Setup Dialog window will appear.

• Click on the box labelled OK at the bottom of this window to create a printable version of your Maple session and store it in a file named hw1.ps (assuming that your Maple file is named hw1.ms as in the example above).

• To actually print your Maple session, you must go to your local window and type

        print hw1.ps.

Alternatively, you could click on the option Printer Command in the Page Setup Dialog window to send your Maple session directly to the printer.

Go to the File menu and choose the option Exit to end your Maple session.

Picking Up Where You Left Off – Your Second Maple Session

In your work above you created a Maple file and saved it under the name hw1. Our task now is to reopen that file and get back to where we left off.

• Go to your local window and open a Maple window.

• Click on the File menu with the left mouse button and select the option Open. A Load Session window will appear.

• In the middle-right of this window you should see a list of Maple files, including your file hw1.ms. Click on this file name with the left mouse. Then press the Return key or click on the box labelled Load.

Your Maple window should now look like it did the last time you saved your file hw1. However, you are not quite ready to continue working. For example in your first session you made some assignments (using :=). Maple displays a record of these commands but it does not remember any of the assignments you made last time. Also, if you loaded a package of functions using with, the command will be there on your screen, but Maple will not have any of the functions of that package in its working memory.

The easiest way to get Maple to remember what happened last time is to go to the beginning of your file and hit the Return key until you get to the last line of the file. (You might want to use the arrow keys to skip the lines that displayed plots or requests for help files). This will ensure that Maple’s memory of your last session is restored. The most important lines to re-enter are the lines where you loaded a package or read in a file (using the command with or read) or lines where you made assignments (using :=). \underline{Remark:} You will find it inconvenient to work with a Maple session file that is more than a few pages long. It is a good idea to start a new Maple file when you begin a new section of problems.