INTRODUCTION TO MAPLE
Teresa Dern Henson, Lecturer
Department of Mathematics
Naval Postgraduate School
September, 1995
SECTION I: ALGEBRA TOOLS
GOALS:
In this section you will learn how to
-
perform arithmetic operations with Maple,
-
define expressions and functions;
-
evaluate expressions and functions;
-
solve equations and systems of equations; and
-
simplify expressions.
REMEMBER: Every input line must end with either a semicolon (;) or
a colon (:). Use a semicolon if you want to see the output.
BASIC ALGEBRA WITH MAPLE
1. Arithmetic. Arithmetic operations are performed using the
symbols
+ plus; addition as in 3 plus 5: 3 + 5
- minus (hyphen); subtraction as in 3 minus 5: 3 - 5
* asterisk; multiplication as in 3 times 5: 3 * 5
/ forward slash; division as in 3 divided by 5: 3/5
^ caret(hat); raise a number to a power as in 3 to the fifth power:
3^5
Maple uses the standard order of operations:
-
Mathematical statements are read from left to right.
-
Multiplications and divisions are performed before additions and subtractions
and in the order in which they appear.
Example: To execute the mathematical statement 4 ¸
3 ´ 8 + 7 in Maple you would input
The operations would be carried out in the following order: first divide
4 by 3, multiply that result by 8, then add 7.
To override the order of operations you may group parts of the expression
using parentheses.
EXAMPLE: The expression
would be executed by first multiplying 3 and 8 together, then dividing
4 by that result and, lastly, adding 7.
2. Roots. In mathematics we express the notion of a root of a
number either with a radical sign (Ö )
or by means of a rational power. Thus, both
and
21/2 indicate the square root of 2; both
and 21/3 indicate the cube root of 2. In Maple we may use the
Maple function sqrt or the rational power, ½ to indicate the square
root of a number; for other roots we must use the surd function if we want
Maple to look for the principal real root of the number.
EXAMPLE: To enter
in
Maple, input
>sqrt(x-1); or >(x-1)^(1/2);
the use of parentheses
in the second version of the input.
EXAMPLE: To calculate
you will need to use the surd command. The command can be used only
after opening a library containing the surd function. This library need
be opened only once in a session. To open the library, input
Then, enter
to calculate the real cube root of -8. The output will appear as
to get the (expected) answer of -2, you need to use this command in conjunction
with the evalf command. See item 5 below.
If you input >(-8)^(1/3);
you will get the principal cube root of -8 over the complex number field,
which is a complex number.
If you wish to define
a cube root function, you should use surd to ensure evaluation and plotting
of the function over the real numbers. Maple can differentiate and integrate
such functions.
3. Functions and Expressions. In Maple, as in formal mathematics,
a distinction is made between functions and expressions. Maple input such
as
>f :=x^2; and >s := sin(t);
assigns the expression on the right-hand side of the := symbol to the variable
name of the left-hand side. The output would look like
>f := x^2; and >s := sin(t);
These expressions may be evaluated, plotted, differentiated, integrated,
etc. That is, they act like functions, but in a formal, algebraic sense,
they are not functions.
To define a function in Maple, you must define a "mapping". Mapping
notation denotes a correspondence between an independent variable, typically
called x (although any variable name is acceptable), and a functional
expression. Here is an
EXAMPLE: The function
would be formally defined as a mapping by writing
.
The Maple version of this statement is
>f := x -> sqrt(x^2+1); or >f := x -> (x^2+1)^(1/2);
The symbol -> is made by typing a minus sign followed by a "greater than"
symbol.
Function notation has the advantage that you may then use the familiar
form of functional evaluation; that is, having defined the function f
as above, to evaluate f at x= 4 you would input
On the other hand, once you have defined a function, you must use the f
(x) notation consistently when referring to the function.
For this reason, it is often much easier to use expressions instead of
functions.
4. subs. The subs command is used to evaluate an expression at
a chosen value of x. Multiple variables may be evaluated all at
once. To evaluate expr at the point x = a, input
a may be a number (real or complex) or it may be a symbolic expression.
If expr has been assigned to a variable named f, then
you may use
EXAMPLE: To evaluate x^2 sin
x at x = p /2, input
>subs(x= Pi/2, (x^2) * sin(x) );
In Maple, Pi is the irrational
number p , whereas pi is the Greek letter p
.
EXAMPLE: To evaluate
at
x = x – h, y = y - k, input
>subs( x = x - h, y = y - k,
x^2 + y^2 = a^2 );
5. evalf. The input
evaluates the expression expr using 10-digit floating-point arithmetic
(this is the default); the result will be accurate to 10-digits and will
be in decimal form. You can specify n-digit floating-point arithmetic
where n is any positive integer. The output will be accurate to
n digits.
EXAMPLE: To calculate p
(ten-digit accuracy), input >evalf( Pi );
EXAMPLE: To calculate
(5-digit accuracy), input >evalf( sqrt(200), 5 );
EXAMPLE: To calculate
,
input >evalf( surd(-8,3) );
6. eval. The input
evaluates the expression expr using exact arithmetic. When eval
is invoked, expressions like sin p/4 will
be evaluated exactly; for instance, if you input
the output will be
We know that
should be
.
To produce this form of the answer, follow the line just input with
This will cause Maple to evaluate the expression
exactly, producing the output
.
In contrast, if you use evalf instead of eval, then the output will
be a decimal approximation of
which is accurate to ten digits.
7. solve. Maple's solve function can be used to
-
solve an equation for one variable in terms of the others;
-
solve an equation in one variable;
-
solve a system of n equations in n variables.
When using solve to solve an equation in one variable, Maple attempts
to find the solutions algebraically. Therefore, solve works best with polynomial
and rational equations. See fsolve for an alternative equation solver.
The basic syntax for the solve command is
where eqtn is the equation to be solved and var is the variable
you wish to solve for. To solve a system of equations, the lists of equations
to be solved and the list of variables are enclosed in braces.
EXAMPLE: To solve the equation
for
b2 input
>solve( A=(b1-b2)/(h * x), b2
);
EXAMPLE: To find the points where the
parabola
and the straight
line y=x+1 intersect, input
>solve( {y = x^2 - x + l, y = x + 1}, {x,
y) );
solve works well in situations where you wish to solve an equation for
one variable in terms of another or for solving equations involving polynomials
of small degree. It is of limited utility when solving equations involving
non-polynomial expressions.
8. fsolve. Just as evalf calculates a quantity using floating-point
arithmetic, the fsolve function solves equations or systems of equations
using floating-point arithmetic. The basic syntax is similar to that of
solve
fsolve is more flexible than solve in that it allows the user to specify
an interval in which Maple should search for a solution by including an
optional argument in the form of an interval.
EXAMPLE: To solve the equation sin(x)
= x, input
>fsolve( sin(x) = x, x );
EXAMPLE: To solve the equation
you
must take into account the fact that this equation may have as many as
five real solutions. Since these solutions would be the x-intercepts
of the function
, plotting
this function can give you some idea where to search for solutions. You
can then specify the solution intervals in the fsolve function. In this
example, the plot of the function reveals that it has three real roots:
One ties between -3 and -1; one lies between -1 and 1; and the third lies
between 1 and 3. (Observe that the function is odd.) To find the three
roots you would input
>fsolve( x^5 - 4 * x^3 = 0 , x, -3 .. -1);
>fsolve( x^5 - 4 * x^3 = 0 , x, -1 .. 1);
>fsolve( x^5 - 4 * x^3 = 0 , x, 1 .. 3);
If the expression
has
been assigned to the variable f, then you can use the somewhat simpler
form
>fsolve( f(x) = 0, x, -3 .. –1);