An Introduction to Maple for Math 210
Department of Mathematics, Statistics, and Computer Science
University of Illinois at Chicago
Copyright © 1998 Paul Brown, Heidi Burgiel, Marc Culler
Maple is a registered trademark of Waterloo Maple Software.
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.4142135623733095048801689. 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 with basic
Maple syntax.
Your lab instructors and graduate assistants are here to help, so do not hesitate to ask for assistance if you get 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 can also start Maple by double-clicking any
icon which represents a Maple worksheet file. Finally, Maple will be
automatically started by the browsers in SEO 200 whenever you follow a
link on a web page that refers to a Maple worksheet (.ms) file. To
close the Maple program click on the "Quit" item on the Maple menu
which appears in the upper left of the screen whenever a Maple window
is active.
Now, start Maple by double-clicking the Maple icon. (To continue reading this web page, you will need to reactivate this window by clicking 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 worksheet window that opened when you started Maple. This window will contain a line with a ">" prompt at the upper left. Click with the mouse next to the prompt. After you click, the prompt will turn green, and a blinking cursor will appear, like so:
Type in the following command 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. Down at the bottom of the window, you will find several examples of the plot command in action. These samples are often the most useful component of the help page.
Expressions and Names
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 familiar clipboard
features 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 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
.
Working with Functions
Defining and Evaluating Functions
Our 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.
> D(f);
Note that the result is a new function, as it should be. We can also give a new name to the derivative, perhaps something suggestive like dfdx:
> dfdx := D(f);
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 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];
> 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);
In cases where we want a non-exact numerical answer, the Maple command "fsolve" will find an approximate root of the equation. If you would like to learn about "fsolve", use Maple's help facility (the "?" command) to read about it.
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);
In the SEO 200 Lab, the plots will pop up inside of the MaplePlot application. In MaplePlot, you can use the Inspector Panel (see the menu!) to manipulate the appearance of the plot.
An Example
Here is an example that combines all of the things we've learned up above.
Consider the polynomial
. 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
at critical points and the endpoints of the
interval. To find the critical points, we will differentiate
and then solve the equation
:
> 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 with checking the values of
at each point.
> f(criticalpoints[1]);
> f(criticalpoints[2]);
> simplify(f(criticalpoints[2]));
> f(criticalpoints[3]);
> simplify(f(criticalpoints[3]));
> 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