Lab #1: An Introduction to Maple for Math 210
Department of Mathematics, Statistics and Computer Science
University of Illinois at Chicago
by Paul Brown, Heidi Burgiel, Marc Culler, Heather Dye and John Wood
Copyright © 1999, The University of Illinois at Chicago
Maple is a registered trademark of Waterloo Maple Software
What is Maple?
Maple is a computer programming language that can perform a wide variety of symbolic and numerical computations. By "symbolic computations", we mean that Maple is capable of working with abstract symbols; it can solve equations, perform operations such as differentiation and integration, and deal with precise quantities, e.g., the exact square root of 2, as opposed to an approximation like 1.4142135623. Maple and other "computer algebra systems" (so-called because of their abilities to manipulate symbolic expressions) are widely used in academia and in industry. Engineers, economists, mathematicians, and scientists use systems like Maple to do computations which are too difficult or complex to tackle by hand.
While Maple is a full-featured programming language, we will not expect you to write Maple programs in this course. Rather you will use Maple interactively, as if it were a very powerful calculator.
This first lab project is in intended to help you become familiar with Maple's graphical user interface and to learn some of the basics of the Maple language.
Past experience shows that students benefit most from the labs when they work in groups and discuss the ideas with each other. Even if you choose to work alone, you should talk to your neighbor when you run into problems! Your lab instructors are here to help too, so do not hesitate to ask for assistance if you and your neighbor are both stuck.
Starting, stopping and getting help
In the SEO 200 lab you start Maple by double-clicking the icon on the right side of your screen which looks like:
You should now start Maple by double-clicking the Maple icon. (To continue reading this web page, you may need to reactivate this window by moving your mouse pointer inside it.)
Maple has a built-in help facility that includes synopses and examples of most commands, so whenever you get stuck or can't quite remember how something works, you can quickly access the information you need.
Locate the Maple window that opened when you started Maple. The smaller window named "Untitled(1)" is a
Maple worksheet
. This worksheet window will contain a line with a [> prompt at the upper left. Move your mouse pointer into the Maple window to activate it. You may want to click on the little button that looks like
to make your worksheet easier to read.
Type the following command in your worksheet and hit return:
> ? plot
A window with information about the Maple "plot" command should have popped up. You can use the slider and the arrows to scroll up and down in the help window. In the "Examples" section of the help page you will find several examples of the plot command in action. These examples are often the most useful components of the help page.
Close your Maple help window by clicking on the little button that looks like
and then selecting "Close" from the pop-up menu.
You can also access the help pages by clicking on the
menu at the top right corner of the Maple window. Try this now. Select "Topic Search..." from the menu and search for "plot3d". You should get the help page about the "plot3d" command. Close your help window, and we can go on to learn how Maple works.
Names and expressions
Expressions
To use Maple interactively you simply type a Maple "expression" into an "input region" on the worksheet. Maple evaluates the expression and prints the result below your input. For example:
> 2+2;
Notice the semicolon at the end of line. The semi-colon tells Maple that you're done typing the expression and that it can begin processing your input . Maple allows you to use long expressions that take many lines to enter. If you hit return without putting a semicolon at the end of the line, Maple will just wait for you to finish entering your expression.
A colon can also be used to indicate that an expression is complete. When you use a colon instead of a semicolon Maple evaluates your expression, but does not print the result. This is useful if the result is so long that it is not helpful to look at it. The graphical user interface allows you to edit lines using the mouse, the arrow keys on the keyboard, and the "clipboard" operations of cut, copy, and paste (available under the "Edit" menu tab). After editing a line, you may hit return at any point in the line.
Basically any mathematical formula which makes sense to you can be entered as a Maple expression. There are just a few mathematical conventions which are not followed in Maple. Since there is no "times" symbol on a computer keyboard, Maple uses the asterisk symbol ("*") to denote multiplication. The caret symbol ("^") is used to indicate exponentiation, i.e., that something is being raised to a power. One mathematical convention that is not followed in Maple is the one which allows us to write "4x" when we mean "4 times x". In Maple you must specify each multiplication. For example, here is how to enter a simple polynomial expression:
> z^2 + 2*z + 2;
Names
You may give a name to the result of any Maple computation by using the operator ":=". Actually, it is better to think of this operator as assigning a value to the name. For example, to assign the value 2 to the symbol "z", we would input:
> z:=2;
Once a value has been assigned to a certain name in Maple, the name will be replaced by its value whenever Maple encounters it in the process of evaluating an expression. For example, after entering z:=2, if we then enter the same polynomial expression in z that we entered above, this is what we see:
> z^2 + 2*z + 2;
The letter z has been replaced by the number 2 throughout our expression, and the result comes out to 4+4+2=10.
Simplifying Expressions
Maple knows how to simplify expressions using the rules of algebra. There are certain simplifications that Maple assumes you will always want done. For example:
> y + y;
Other simplifications are made only if you specifically request them by using the simplify command. Here are some examples:
> sin(Pi);
> sin(x)^2 + cos(x)^2;
> simplify( sin(x)^2 + cos(x)^2 );
Notice that we did not type sin^2(x) + cos^2(x). This is another of those rare cases where Maple does not follow a common mathematical convention.
Exercise. Open a worksheet in Maple, and use the simplify command to simplify (sin(x) + cos(x))^4 + (sin(x) - cos(x))^4.
Working with functions
Defining and Evaluating Functions
This course deals primarily with functions and their properties, so it is important to be able to work with functions in Maple. Maple uses the "->" operator to define functions. You should read "->" as "goes to". For example, to define the function f(x) = 2x , you would tell Maple to assign the name "f" to the function that uses the rule "x goes to 2 times x". Here is a slightly more complicated example:
> f := x -> 2*x^5+3*x-17;
Once you have defined a function with the "goes to" operation, you can use it in Maple just as you might expect:
> f(2);
> f(y);
> f(x+2);
Differentiating Functions
Maple can perform the standard operations of calculus on functions. To differentiate a function that you've defined with the "->" operator, use the "D" operator. You can also give a new name to the derivative, perhaps something suggestive like dfdx:
> dfdx := D(f);
Note that the result is a new function, as it should be. Now we can do the same sorts of things with "dfdx" as we could with "f":
> dfdx(2);
Maple can also do integrals, both definite and indefinite, but we'll be using mostly differentiation in Math 210.
Maple even knows the Fundamental Theorem of Calculus:
> G := x -> int (g(t),t=0..x);
> D(G);
If you have programming experience you may want to learn some of the finer points about working with functions and expressions.
Solving Equations
In Maple, as in mathematics, equations are legitimate expressions. (Maple is different from most programming languages in that it does not use the symbol "=" to represent the assignment operator; in Maple ":=" is used for assignment.) One of Maple's most useful capabilities is that it can solve equations:
> solve (x^2 + 7*x - 2 = 0,x);
The first argument of the solve command is the equation (written properly for Maple with *'s and ^'s), and the second argument is the variable that we want Maple to solve for. You will notice that Maple solved the equation exactly, and that the result contains expressions for both solutions, separated by commas. The commas indicate that the result is a list of expressions. It is useful to know how to work with just a single solution from the list. To do this we can assign a name to the list and then use square brackets to select individual elements. This is illustrated below.
> solnlist:= solve(x^2 + 7*x - 2 = 0, x);
> solnlist[1];
> solnlist[2];
Maple can also handle much more complicated equations:
> solve (x^3 + 3 = 4*x^2,x);
> solve (x^5 + 4*x^3 + 2*x^2 + 3*x + 5 = 0,x);
In the last case, Maple couldn't find a solution. (This isn't Maple's fault! Many polynomials of degree five and higher have roots which can not be expressed using roots and fractions.) Just the same, Maple can work with the "RootOf" just as though it was a solution of the equation:
> u := solve(x^5 + 4*x^3 + 2*x^2 + 3*x + 5 = 0, x);
> simplify (u^5 + 4*u^3 + 2*u^2 + 3*u);
Maple can approximate the solutions to this equation, it just can't find an exact answer. Below we use Maple's fsolve function to get approximate values of the roots of the polynomial.
> fsolve(x^5 + 4*x^3 + 2*x^2 + 3*x + 5 = 0, x, complex);
Graphing Functions
Later in the course, we will make extensive use of Maple's ability to draw graphs of functions of several variables. For this introduction, though, we will illustrate the use of Maple's "plot" command (which you read about in our first example of Maple's on-line help system) to graph a function of a single variable:
> plot(exp(-x^2), x=-3..3);
The function "exp" is the exponential function. (In Maple, the name of Euler's constant is E; you may use E^x in place of exp(x) if you wish.) The first argument in the "plot" command is the function to be plotted, and the second argument is the range over which to plot the function. The "-3..3" means to plot the function from -3 up to 3. To plot the function over the range from 0 to 5, we would have used:
> plot(exp(-x^2), x=0..5);
Notice that when you click on the plot new buttons appear on the tool bar at the top of the Maple Worksheet. You can use these new buttons to control the appearance of your plot. You can also resize the plot by dragging the eight control points on its bounding box. Experiment with the plot controls!
An Example
Here is an example that combines all of the things we've learned up above. Consider the polynomial x^4 + 2*x^3 - x + 1 . We will use Maple to find the maximum and minimum value of f on the interval from -2 up to 2 inclusive. First, enter f as a function in Maple:
> f:= x -> x^4 + 2*x^3 - x + 1;
Let's have a look at the graph to get an idea what to expect.
> plot(f(x),x=-2..2);
To compute the maximum and minimum values on a closed interval, we need to check the values of f(x) at critical points and the endpoints of the interval. To find the critical points, we will differentiate f(x) and then solve the equation f'(x) = 0 :
> dfdx:=D(f);
> criticalpoints := solve(dfdx(x) = 0, x);
Maple has given us three critical points, but we don't know whether or not those critical points fall into the interval between -2 and 2. We can check by using the Maple command "evalf" which computes approximate numerical values for expressions:
> evalf(criticalpoints[2]);
> evalf(criticalpoints[3]);
Both of the points lie within the interval, so we can continue by checking the values of f at each point.
> f(criticalpoints[1]);
> f(criticalpoints[2]);
> simplify(f(criticalpoints[2]));
> f(criticalpoints[3]);
> simplify(f(criticalpoints[3]));
Finally, we check the values of f at the endpoinsts of the interval:
> f(2);
> f(-2);
The value at the first critical point, 21/16, is larger than 1, so the smallest value of f(x) on the interval is 3/4 and the largest is 31. This agrees with what we saw on our graph.
Now it is time for you to try using Maple on your own. Follow the link below to the exercises to be handed for this lab.
Lab Assignment #1