Doing Exact Arithmetic with Maple
Using Maple to do numerical computations is very straightforward. Just enter the numerical expression and end the line with a
semicolon
. Pressing
[Enter]
will then execute the line and the result will be displayed in blue in the center of the screen.
Example 1
A simple calculation has been entered on the next line. Click anywhere in the red line and press
[Enter]
.
Each red input line is "live" and can be modified at any time.
Change the "4" in the line above to an "8" and press
[Enter]
.
Notice how the blue output is automatically updated to display the new result.
Example 2
For our next example let's calculate
.
Unlike your calculator, Maple gives you the exact answer to this problem, all 83 digits worth!
Example 3
Maple can calculate with fractions without converting to decimals:
Example 4
To enter the square root of a number use
sqrt
:
Notice that Maple has simplified
but has left the answer in exact form. In
the next section you will learn how to get a decimal approximation for
this number.
Example 5
Maple has all of the important mathematical constants built in. To enter
type Pi.
Notice that an asterisk * is required to indicate multiplication.
Again Maple carries out the calculation but leaves the answer in exact form.
Example 6
Unlike your calculator, Maple gives you the exact answer when applying trigonometric functions.
To get the inverse sine of a number use the
arcsin
function:
If you ask Maple to calculate a value that is
undefined
it will respond with an error message:
Error, (in tan) numeric exception: division by zero
Example 7
To enter the natural exponential function
in Maple, type:
exp(x)
.
And to get the number
by itself, type:
exp(1)
.
Example 8
To enter the absolute value function
in Maple type:
abs(x)
.
For example, this function at
yields
Note that Maple gives the correct, exact answer for the following since
.
Example 9
Maple has many
special purpose commands for working with numbers. You will learn these
as you need them in your math course. Here is one last example for now.
If we have an integer and want to factor it into primes we can use
Maple's
ifactor
command. Feel free to experiment by changing the number.
Example 10
There may be times
when you want to enter more than one command on a single line. This is
okay to do in Maple, just be sure to end
each
command with a semicolon. It also helps to put spaces between the commands. When you press
[Enter]
all of the expressions are executed and the results are listed, in order, in a single output field.
> |
sin(Pi/3); cos(Pi/3); tan(Pi/3);
|
To display the commands in a single execute group but on separate lines, press
[Shift] + [Enter]
. The result will be the same.
> |
sin(Pi/3);
cos(Pi/3);
tan(Pi/3);
|
Example 11
To calculate and display a sequence of numbers use the
seq
command. Here we calculate the squares of the first 100 natural numbers.
Numerical Approximations using the
command
Recall that in the previous
section we asked Maple to add three fractions and the result was also
displayed as a fraction. This sort of exact arithmetic is very useful
but there are times when we prefer an answer in decimal form. The Maple
command
evalf
performs this task for us.
Example 1
Compare the results of the next two lines.
Example 2
Assigning a name to
the result of a calculation makes it easier to use that result in a
subsequent calculation. To assign a name we use a
colon
followed by an equal sign ( i.e. name :=
result ; ) . On the next line we have assigned the letter k to
the original output above. Then we apply
evalf
to k.
Important Maple Note
: Maple is case sensitive. So for example Maple considers k and K to be different variables..
By the way, you can also use words as variable names.
Example 3
If we want fewer or more digits of accuracy than the default number which is 10 digits we can add an extra argument to the
evalf
command as shown below.
To see four digits of w:
To see forty-five digits of w:
Example 4
If you enter
numbers with a decimal point Maple automatically gives decimal results.
Compare the results of the two lines below.
Here is another example:
Example 5
We can apply the
evalf
command to a sequence of numbers. Below we
first generate the exact square roots of the first 10 natural
numbers, then apply evalf to get decimal approximations.
> |
result := seq(sqrt(k),k=1..10);
|
Maple Shortcut: Quick reference to the last chronological output
There will be many
times when using Maple that you will string together a sequence of
computations. Rather than giving a name to each result as you go along,
you can use the percent sign (
%
) to refer to the
chronologically
last expression computed by Maple.
Note, however, that this device can lead to great confusion, and is not generally recommended.
Here are some examples of how it works.
For more on using the ditto symbol see
Exercise 1.4
below.
Warning:
Use of
%
should be avoided as much as possible, as it may make the worksheet unreadable.
Exercise 1.1
Use Maple to calculate the number
.
Student Workspace 1.1
Answer 1.1
Exercise 1.2
Calculate
to 18 digits.
Student Workspace 1.2
Answer 1.2
Exercise 1.3
Find a numerical approximation for the expression :
Student Workspace 1.3
Answer 1.3
> |
answer := (3+Pi)/(7-sqrt(13));
|
Exercise 1.4
The percent sign (
%
) is a handy shortcut but it can occasionally lead to some unexpected results.
Here is an example.
First execute each of the next three lines. You should be able to predict the result in advance.
Now go back and re-execute the last line (i.e.,
%+10;
). Note that the output changes from
to
Can you explain why?
Student Workspace 1.4
Answer 1.4
The ditto symbol (
%
) represents the
chronologically last number calculated by Maple
. So after you executed the first three lines
%
=
. The second time you executed the line
%+10;
Maple added 10 to
.
To avoid this sort of confusion assign names to each output:
Clearing Variables
Assigning new values to existing variables
Once you have
defined a variable, Maple will remember its value during your entire
working session. If you want to overwrite the variable with a new
value, you can simply make a new assignment.
For example each assignment below redefines the value of the variable
. (Note: to check the current value for a variable just type it followed by a semicolon.)
Assign
the value 56.
Notice the value of
, 56, after the assignment.
To overwrite
with a new value, make a new assignment using
:=
.
You'll notice
now has the new value you assigned.
Clearing one variables at a time: the
unassign
command
Sometimes you will want to "clear" a variable in memory so that you can use it in a new situation.
Here is an example. First we assign
the value 65.
Now assume that we start a new problem and want to enter the general algebraic expression
and assign it the name w. If we just enter this, Maple automatically substitutes the previous value for
.
In order to get x to be a general variable again we must first "clear" (i.e. erase from Maple's memory) our earlier value for
. This is accomplished by using
unassign
command. Note that we use single quotes here.
Execute the next two lines to see how this works.
Clearing all variables at once: the
restart
command
The
restart
command will clear Maple's memory of all
definitions that you have made. It is like starting a new Maple
session. If you are starting a completely new problem you can use the
restart
command to guarantee that there are no
leftover definitions from your earlier work. Before you execute
the second line below, quickly predict the output.
You probably remembered that p was 4 and
had been reassigned to be the variable
, but you may not have remembered that
was assigned the value
. That's why it's a good idea to use
restart
to remove all definitions at once.
(As you work through this tutorial you will notice that we start
most new sections with a
restart
command.)
Now check the values of variables.