INTRODUCTION TO MAPLE
I. ARITHMETIC, ALGEBRA, AND PLOTTING

Richard M. Felder
Department of Chemical Engineering
North Carolina State University

MAPLE is a powerful mathematical software package. It can be used to obtain both symbolic and numerical solutions of problems in arithmetic, algebra, and calculus and to generate plots of the solutions it generates.

This is the first of a set of tutorials that outline how to use MAPLE. In it, we outline basic MAPLE syntax and show how to solve sets of algebraic equations, generate data plots, and generate an annotated printout of the results. Realize, however, that we are barely tapping the power of this tool to perform almost any type of mathematical analysis you might ever need to perform. Once you have mastered the material in this tutorial, you may go on to the second one, which covers differentiation, integration, and solving ordinary differential equations.

If this is your first experience with MAPLE, simply reading the tutorial will do you very little good. If you want to be able to use MAPLE to solve mathematics problems, start the program with the commands in the "Getting Started" section and then carry out each step given in the subsequent sections. Bullets will mark the instructions you should follow and the commands you should type.

## OPTIONS

Get started. Review procedures for bringing up MAPLE on the EOS system, saving files, and getting help. Designed primarily for first-time users.

Go to the second MAPLE tutorial. (Differentiation, integration, ordinary differential equations.)

## GETTING STARTED

Open MAPLE. If you are on EOS (a UNIX-based operating system at North Carolina State University)

• Log on to your EOS account.
• Bring up the application menu(point to an open space on the desktop and click the middle mouse button) and choose Math and Statistics --> Maple(4)
(Alternatively, at the command prompt in the Xterm window, type add maple and then
xmaple &)

You should eventually see a large window headed by "MapleV, Release 4," with a smaller window headed by "Untitled(1) - [Server 1]." The smaller window is the worksheet. The MAPLE command prompt
[ >
will show at the upper left of the worksheet.

When you are finished with the MAPLE session, you will leave the program by selecting "Exit" under the "File" menu (upper left of the MAPLE window). (Don't do it now.)

As with any other program application (word processing, spreadsheeting, etc.), you should get in the habit of saving your work frequently, so if the system crashes you can recover most of what you did.

• Choose "Save as" under the File menu. Make "tutorial1.mws" the name of your file.

This name should then appear at the top of your worksheet. As you work, frequently choose "Save" under the "File" menu or type CONTROL-s (hold down the CONTROL or CTRL key and type s) to save your recent work.

Getting help

• Go to the "Help" menu (it's at the top right of the MAPLE window) and select Contents.

You will see a number of topics in black lettering ("What's New," "Mathematics," etc.) and "Introduction" and "Worksheet Interface" in green lettering. You can click on any title with green letters to open that section of the Help file.

• Click on "Introduction" and read through the examples to get your first look at what MAPLE does.

• When you are finished, click on the upper left corner of the "Intro" window and select "Close" to get rid of the window.

• In the "Contents" window, click on the [+] next to "Mathematics."

A large number of green titles will appear, any of which can be clicked on for information about that topic.

• Click on the [-] next to "Mathematics" to eliminate the titles.

• Close the "Contents" window.

## MAPLE SYNTAX AND OPERATIONS

Commands.

A MAPLE command is a statement of a calculation followed by a semicolon (the result is displayed on the screen) or a colon (the result is stored but not displayed). Following are some commands followed by the displayed results. Enter the commands on your worksheet and verify the given results. When you get to "Save the file," select "Save" under the "File" menu or type CTRL-S.

• [> 4+5;

9

• [> 6^2;

36

• [> 3*4 + 7;

19

• [> 3*(4+7);

33

• Save the file

If you don't include a semicolon or colon at the end of a command, MAPLE will interpret the next command line as a continuation of the previous command.

Tip: Once you've done something in a program step (like redefine a variable), it stays done unless you change it again. Even if you click on an earlier step and re-execute it, the effect of the change remains. If a program does not seem to be working, before trying anything else re-execute all steps from the beginning by clicking on the Edit menu and selecting Execute -> Worksheet. (You can also select a set of commands and re-execute them by selecting Execute -> Selection.)

Arithmetic operations

The symbols +, -, *, /, and ^ (or **) denote addition, subtraction, multiplication, division, and exponentiation (4^2 = 4**2 = 42 = 16). When a string of operations are specified in a command, MAPLE first does exponentiations, then multiplications and divisions, then additions and subtractions. To change the order, use parentheses.

Editing commands.

If you make a mistake in a command (like forgetting a semicolon) or want to change a command, you can go back and edit the command with the cursor and mouse as you would a word-processor text.

Operating on the last result.

Use a quotation mark (") to refer to the result of the last calculation, and a double quotation mark ("") to refer to the next-to-last result. For example,

• [> 5*2;

10

• [> 7 + ";

17

• [> " * "";

170

Exact arithmetic and floating-point arithmetic

MAPLE calculates fractions (exact arithmetic) unless you specify that you want decimals (floating-point arithmetic) with the evalf function ("evalf" stands for "evaluate using floating-point arithmetic"):

• [> 25/27 + 3/51;

452
459

• [> evalf(25/27 + 3/51,3); ( or [> evalf(",3); )

0.985

• Save the file

The second argument in the evalf command (the 3 following the comma) specifies the number of significant figures you want in the result. If you omit this command, you will get ten significant figures.

On-line help

You can get help with MAPLE syntax by using the HELP menu, as described previously. If you have a question about a particular command, you can quickly get help by typing a question mark followed by the command name (no semicolon). For example,

• [> ? evalf

will open a window containing information about what the evalf function does and how to use it.

• Close the help window. Click on the little box at the upper left of the window and select Close.

Defining variables

Use the colon-equal symbol (:=) to define variables--that is, to assign values to them. Once you have defined a variable, simply typing its name will show its value, and using the name in a formula will cause the value to be substituted.

• [> A := 5; B := 2;

A := 5
B := 2

(Notice that you can string commands together on the same line.)

• [> A;

5

• [> 4*A/B;

10

• Save the file

Variable names are case-sensitive: A and a are two completely different variables.

Specifying the number of digits (significant figures) in your results.

When you do floating point arithmetic, MAPLE defaults to 10 significant figures:

• [> evalf(5/2);

2.500000000

You can change the default by setting a variable "Digits" equal to the desired number of significant figures.

• [> Digits := 4; (Make sure you capitalize the word Digits.)

Digits := 4

• [> evalf(5/2);

2.500

Built-in functions

MAPLE has many built-in mathematical functions including sin, cos, tan, exp (exp(x)=ex), ln, and log10. However, if you want to calculate the value of one of these functions, you must use the evalf command.

• [> ln(10);

ln(10)

• [> evalf(ln(10), 4);

2.303

Expressions and functions

An expression is a string of constants, variables, and mathematical operators (+   -   *   /   ^   =   ln   sin   ...). The following are all expressions:

1/2
3*x^3 - 4*ln(x)     [3x3 - 4ln(x)]
5*x^2 - 2*y^2 = 3*cos(x*y)     [5x2 - 2y2 = 3cos(xy)]

A function is a relationship for a variable (the dependent variable in terms of one or more other variables (the independent variables). The following are each functions:

y(x) = 3*x + 2   [--> y(1) = 5]
z(x,y) = 2*x/y   [--> z(5,3) = 10/3]

MAPLE handles expressions and functions in completely different ways, which can lead to a great deal of confusion when relations like this are encountered:

y = 6*x^2 - 3*x + 4

This could be an expression relating x and y or the definition of y as a function of x. To do anything to or with this relation (like substituting a value of x into it, or solving it for one variable in terms of the other one, or differentiating or integrating it), you must know whether the relation is an expression or a function.

In the next two sections we show how expressions and functions are defined and evaluated. In the remainder of the tutorial we will continue to make the distinction between the two types of relations. If you forget the difference, refer back to this section.

Defining expressions and substituting values into them

Use the colon-equal (:=) to define an expression, and the subs function to substitute a value into it.

• [> f := x^2;

f := x2

• [> f;

x2

• [> subs(x=5, f);

25

Typing [> f(5); will not work for the latter calculation. If you want to use functional notation like that, you need to use the next method to define a function.

Defining functions and substituting argument values into them

Use the colon-equal (:=) and the minus-greater than (->) symbols to define a function, and functional notation to substitute a value into it.

• [> g := x -> 1/(x+1);

g := x -> 1/(x+1)

• [> g(x);

1/(x+1)

• [> g(4);

1/5

• Save the file

Converting expressions into functions and vice versa

We noted that the previously defined expression f = x2 cannot be treated as a function.

• [> f(5);

x(5)^2

You can convert the expression into a function of x, however, by using the unapply command.

• [> f := unapply(f,x);

f := (x -> x)2

• [> f(5);

25

You can also take the function f = x2 and convert it back into an expression.
• [> f := f(x);
f := x2
• [gt; subs(x=5,f);
25

## SOLVING ALGEBRAIC EQUATIONS

Defining equations

Equations are expressions, so we can use the colon-equal (:=) to define equations involving one or more variables. (This will be the first step in getting MAPLE to solve the equations.)

Suppose equations relating two variables are

2x1 - 5x2 = 12
12x1 + 4x2 = 17

To enter and store these equations for subsequent processing, enter the commands

• [> eq1 := 2*x1 - 5*x2 = 12:
• [> eq2 := 12*x1 + 4*x2 = 17:

(Don't forget the multiplication symbols.) We used colons instead of semicolons in these commands, so MAPLE does not reproduce the equation definitions.

• Save the file.

Solving sets of linear algebraic equations.

Suppose you wish to solve the pair of equations defined above for the variables x1 and x2. Use the solve (exact arithmetic) or fsolve (floating point arithmetic) command as follows: (Type these commands carefully; if you make the slightest mistake, like omitting a comma or typing the wrong kind of brace, MAPLE will give you nothing but heartache.)

• [> sol := solve({eq1, eq2}, {x1,x2});

• [> sol := fsolve({eq1, eq2}, {x1,x2});

sol := {x2 = -1.618, x1 = 1.956}

(There does not seem to be any way to predict which variable MAPLE will print first.)

The argument of the solve or fsolve command consists of two sets of curly braces enclosed in parentheses. (If you only have one equation in one unknown, you don't need the braces.) The first set of braces contains the equations to be solved or the symbols for them, separated by commas; the second set contains the set of unknown variables to be solved for.

Extracting components of a solution.

The solution set sol generated with the fsolve command in the previous step contains two components: the equation x1 = 1.9559, and the equation x2 = -1.6176. You can extract either of these equations or only their right-hand sides as follows (the example shows the procedure for the first element).

• [> sol[2];

x1 = 1.956

• [> rhs(sol[2]);

1.956

• Save the file

You could also use the lhs function to extract the left-hand side of the equation.

Specifying search ranges for unknown variables in nonlinear equations.

The procedure for solving nonlinear equations with solve or fsolve is the same as that outlined above for linear equations. A difference in the possible outcome is that the procedure used by MAPLE (or any other nonlinear procedure) may not converge to a solution, even if one exists, or it may converge to a solution that is mathematically correct but physically meaningless (like a negative mass fraction).
To improve the odds of success in solving nonlinear equations, specify in the solve or fsolve command a range of values for each unknown variable within which the desired solution must fall. The more you can narrow the search range, the better the chances for convergence.

For example, if a variable x is the solution of the equation x = e-x, and you know the solution must be a positive fraction (0<x<1), you can proceed as follows:

• [> eq := x = exp(-x);

eq := x = e(-x)

• [> sol := fsolve(eq, x, x=0..1);

sol := .5671

(Since there is only one variable, you could have omitted the "x=" in front of the range 0..1.)

The procedure is the same if you have simultaneous nonlinear equations, except that now you need to enclose the equations, the variables, and ranges for each variable within curly braces. For example, suppose you want to find the solution of the equations

sin(x+y) -yex = 0
x2 - y = 2

and you know that because of the physical meanings of x and y the solutions must fall in the ranges -1<x<1 and -2<y<0. The solution program would be as follows (Type carefully!):

• [> f := sin(x+y) - y*exp(x) = 0:

• [> g := x^2 - y = 2:

• [> solfg := fsolve({f,g}, {x,y}, {x = -1..1, y = -2..0});

solfg := {x = -.6687, y = -1.553}

• Save the file

If you omitted the search ranges for x and y, the procedure would have converged to a different solution that satisfies the equations but has no physical significance. (Try it and see.)

If you want to specify a semi-infinite range for a search, you can use infinity or -infinity as one of the limits of the range.

Finding roots of polynomials.

Suppose you want to solve the equation

6x4 - 7x3 + 6x2 - 1 = 0

You know that the equation has four roots (solutions), which may be real numbers or complex conjugate pairs. You can find the roots either exactly or with floating-point arithmetic using the solve or fsolve command. In what follows, I is the MAPLE symbol for .

• [> p := 6*x^4 - 7*x^3 + 6*x^2 - 1 = 0;

p := 6x4 - 7x3 + 6x2 - 1 = 0

• [> sol := solve(p, x);

• [> solf := fsolve(p, x, complex);

solf := -.3333, .5000 - .8660I, .5000, .5000 + .8660I

If you had left off "complex" in the last command the program would have generated only the real roots.

## PLOTTING

Accessing the MAPLE plotting package

• [> with(plots);

This command makes a wide range of plotting functions available for use. Their names should appear on the screen in response to this command. We will use the "plot", "textplot", and "display" routines in the package.

Plotting tabular data

The specific volumes of saturated steam at several temperatures are listed below:

 T(oC) 9.7 20.4 31 41.5 51.1 V(m3/kg) 108.7 56.4 31.1 18.1 11.5

To use MAPLE to generate a plot of V vs. T, we use the following commands:

• [> VTdata := [[9.7, 108.7], [20.4, 56.4], [31.0, 31.1], [41.5, 18.1], [51.1, 11.5]];

VTdata := [[9.7, 108.7], [20.4, 56.4], [31.0, 31.1], [41.5, 18.1], [51.1, 11.5]];

• [> plot(VTdata, T=0..60, V=0..120, style=point);

• Save the file

The first command generated a list (an array enclosed in square brackets) consisting of the [T,V] data points, each point enclosed in its own square brackets. The second command told MAPLE to plot the points, specifying the labels and ranges for the abscissa and ordinate axes, and to show but not connect the data points. (If the "style=point" option had been omitted, the points would have been connected with line segments.)

There are ways to change the axis labels, the symbols and colors of the data points, and other characteristics of the plot. We will hold off on giving you some of these options until we discuss plotting functions.

The command to generate plots of two functions on the same graph has the form

[> plot( [f1(x), f2(x)], x=a..b, options); (Don't type this now)

where

f1 and f2 are either MAPLE functions or functions you defined using (:= ->)
a and b are the desired boundaries of the abscissa range
options are statements of how you want the plots to appear (details follow).

If you only want to plot one function, you can omit the square brackets enclosing the function labels.

For example, to generate plots of f(x) = sin(x) and g(x) = 0.25x-1 vs. x from x=0 to x=4, the commands would be

• [> f := x -> sin(x);

f := sin

• [> g := x -> 0.25*x - 1;

g := x -> .25x - 1

• [> plot( [f(x), g(x)], x=0..4, color=[red,blue], style=[point, line]);

(We are omitting the MAPLE output here but you should see it on your terminal screen.) You could also have added a range for the ordinate axis (e.g. y=-1..1); by omitting it, you allowed MAPLE to do its own scaling.

• Save the file

Plot options

Following are some but not all of the options you can include in a plot command. For a more detailed list, enter [> ? plot and click on the "plot(options)" link.

What follows is for your information only. Don't do anything with it now.

color = c, where c may be any of most common colors and some not so common (coral, sienna, etc.) To see a list of colors, enter [> ? plot,color (don't do it now).

style = s, where s may be "point" or "line" (line is default). "Point" shows closely spaced points, "line" shows line segments. When a function is plotted in the line style, the line segments are usually small enough to make the curve look continuous.

labels = [xlabel, ylabel], where the two arguments in square brackets are the labels you want for the abscissa and ordinate. The defaults are the variable names.

linestyle = n, where n is usually 0, 2, or 3 but may be any other integer. The value of n specifies the type of line you want plotted when style=line. n=0 (default) is a solid line, n=2 is dots and dashes, and n=3 is dashes.

symbol = s, where s may be circle, POINT, box, cross, or diamond (default).

title = t, where t is a character string. The default is no title.

Inserting text on a plot

You can use the "textplot" command to insert text on a plot. In this case, we will label the two curves on the graph we just generated by inserting "f(x)" centered at the point (x=0.8, f=0.5) and "g(x)" just above and to the left of the point (x=2, g=-0.5). We first define a variable to represent the graph.

• Click just to the left of the word "plot" in the plot command.

• Type

fggraph := [so that the command reads "fggraph := plot([f(x),g(x)],..."]

and replace the final semicolon with a colon. Hit "Return."

In the textplot commands that follow, enclose the texts to be inserted [f(x) and g(x)] in forward apostrophes (`) (located under the ~ character on most keyboards), as opposed to ordinary apostrophes (').

• [> labelf := textplot( [0.8, 0.5, `f(x)`]);     (Don't forget to use forward apostrophes.)

labelf := textplot([.8,   .5,   f(x)])

• [> labelg := textplot( [2, -0.5,   `g(x)`],   align={ABOVE,LEFT});

labelg   := textplot(2,   -.5,   g(x)],  align = {ABOVE, LEFT})

• [> display ([fggraph, labelf, labelg]);

You should see the same graph with the plot labels inserted. Notice how f(x) is centered on the point specified in the first textplot command (this is the default alignment), and the lower right corner of g(x) is located at the point specified in the second textplot command. It usually takes several trials to get the text exactly where you want it.

## GENERATING A REPORT

### Insert text in the worksheet.

• Insert a new paragraph and title at the top of the worksheet.

• Click on the bracket ( [ ) to the left of the first command line ("4+5;"). The bracket should be highlighted.
• Click on the Insert menu and select Paragraph -> Before. A line should open up before the command line.
• On the new line, type (without hitting the "Return" key)

MAPLE TUTORIAL I

• At the left of the third line from the top of the big MAPLE window (the "context bar"), find a horizontal window with the word "Normal" in it and a triangle just to the right of the word. This is the "style droplist." The window to the right of the style droplist is the "font droplist," and the one to the right of that is the "font size droplist."

Click on the triangle to the right of the style droplist to bring up a list of predefined styles. Then click on Title. Your title should now be centered, underlined, and in large type.

• Put your name (or names and group number) as the author(s) of the report.

• With the cursor at the end of the title, hit the "Return" key. (The word "Author" should come up in the style droplist.) Type your name(s) or, if applicable, group number, colon, and last names separated by commas. Don't hit "Return" yet.

• Change the style of this line from author to Heading 1 (click on the style droplist button as before). Then use the font size droplist to change the size from 18 point to 14 point. Then click on the "Centered Text" button (second from the right on the context bar).

What you typed should now be boldface and centered, in larger type than normal size but smaller than in the title style. Hit the "Return" key. You should now be back in Normal style.

• By clicking and dragging the mouse, highlight the region from the
"4 + 5" command through the "? evalf" command.
• Click on the Format menu and select Indent. A little square with a minus sign in it should appear.
• Click to the right of the little square and type Command syntax and arithmetic
• Click on the little square to omit the section (except the heading) from the screen display. Then click on the square again (which should now have a plus sign in it) to display the section again.

• Define sections for the remainder of the worksheet

• Using the same sequence of steps (highlight region to be indented, indent using the Format menu, type heading), define a section containing the commands from "A := 5;" command line to the "f(5)" command line and the "25" output line that follows it. Give this section the heading "Defining variables, expressions, and functions." Save.
• Define a section containing the commands from " eq1 := ... " command line to the "solf := ..." command and the output line that follows it, giving it the heading "Define and solve sets of algebraic equations." Save.
• Define a section containing the commands from "with(plots)" through the final chart. (You may have to do some juggling to highlight this entire area, perhaps starting at the bottom and moving up with the pointer.) Give it the heading "Generate and label plots of tabulated data and functions." Save.

Printing

When your program is complete, you can edit it to get rid of unwanted command lines, and then print it.

• Select "Print" under the "File" menu, note the name of the file that will be created when you execute the print command (tutorial1.ps--change it if you want to), and then click on "Print."

• Go into your terminal window and issue the command to print the file

## Exit from MAPLE.

• Select Exit under the File menu.

Final Notes

Document written by R.M. Felder, October 1998.

For more detailed information on how to use Maple to perform calculations, solve equations, and plot functions, you may later want to check out these Web sites:

http://www.indiana.edu/~swinfo/math/maple
http://www2.ncsu.edu/eos/info/math/maple_info/www/MA141Contents.html

Comments or questions? You can send mail to Dr. Felder at felder@eos.ncsu.edu