Physics 210 Maple Introductory Labs

Table of Contents

      
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

1 Introduction to xmaple & maple

1.1 Preliminaries

  1. Open a terminal window, and within it change to your home directory, create a sub-directory called maple and cd to it

    % cd
    % pwd
    /home/phys210d
    
    
    % mkdir maple
    % cd maple
    % pwd
    /home/phys210d/maple
    
  2. IMPORTANT!

    Install the course-default maple style file as follows:

    % cp ~phys210/maple/phys210.mw .
    % ls -lt
    

In what follows we will start xmaple from the command-line; it can also be launched by clicking on the Maple 18 icon on the bottom panel.

1.2 Initial configuration of xmaple

IMPORTANT!

Performing the following configuration steps is crucial to ensure, for example, that the format of your answers for Homework 2 is "correct"; i.e. it will make the input/output format and general appearance of your maple worksheets the same as mine.

Please ask for help should you have any problems making the appropriate settings.

From the command-line of the terminal window that you have opened, execute xmaple in the background, so that you can continue to type commands in the terminal window as necessary: remember, to background any command simply append an ampersand (&) to the end of the command line.

% xmaple &

Be sure to type xmaple, not maple. We'll get to maple later.

The main Maple window should open, and within it the so called home page or home worksheet should appear.

Let's proceed to the configuration per se

Step 1

From the top menu bar:

Step 2

The Options panel should still be visible:

Step 3

The Display tab should still be open:

IMPORTANT!

Now click Apply Globally

Step 4

From the top menu bar:

Step 5

1.3 Using xmaple to do mathematical computations interactively and saving them as a maple worksheet

Restart xmaple, i.e. return to the terminal window and enter

% xmaple &

Let's take a quick tour of some of the important widgets on the interface and keyboard shortcuts.

These include:

  1. Menus
  2. Icons
  3. Others

Now, click on the New Works... icon.

The xmaple window that appears should look like mine: if it doesn't, you may need to repeat the above configuration steps and/or ask for assistance.

Now, try the following calculations, and note that with the configuration we have set, every maple statement (command) should end with a semi-colon(;)

In many instances, if you forget to enter the semi-colon, maple will do so for you and emit a warning, but get in the habit of including them yourself, since adherence to correct syntax will be essential once we start programming.

In the following, > denotes the maple prompt: you should enter only the text that follows the prompt (as we have done when entering Linux commands).

In addition, lines beginning with # below are comments, and are not to be entered.

Observe what the output is after you execute each statement.

> 1 + 2;

> 75 - 3;

> 5*3;

> 120/2;

> 100!

> length(%);

Note the behaviour of the arrow keys in the interface: the up-arrow, for example, does not retrieve previously typed commands. Rather the arrow keys relocates the cursor in the worksheet, and you can edit separate commands, then hit enter to reexecute. We need to be careful in general when using this editing facility: if commands that follow what we have edited, depend on the calculation that we have modified, there values will not be automatically updated. For that it is always safest to reexecute the entire worksheet using the !!! icon (or Edit -> Execute -> Worksheet from the menus).

> 1000!;

> length(%);

> 1/2 + 1/3;

> Pi;

# Evaluate Pi as a floating point number using the 'evalf'
# procedure.

> evalf(Pi);

# The optional second argument to evalf is the number of decimal places
# to display (and, internally, to evaluate the expression to)

> evalf(Pi, 100);

> evalf(Pi, 1000);

> evalf(Pi, 10000);

# Maple knows about all of the standard elementary functions, as well
# as many of their properties

> cos(Pi);

> cos( evalf(Pi, 10) );

# What's the difference between the output from the last two
# statements?

# Some basic operations from calculus ... diff() takes derivatives,
# int() computes anti-derivatives (as well as definite integrals)

> diff( cos(x) , x );

> int( x^2 , x );

1.3.1 Saving your work as a Maple worksheet

Perform the following steps

  1. Click on the floppy disk (save) icon

  2. Navigate to your maple subdirectory

  3. Type first or first.mw in the File Name box (the extension .mw stands for Maple Worksheet)

  4. Ensure that the Files of Type menu selection is Maple Worksheet (.mw)

  5. Click Save

  6. Verify that the worksheet has been created: In the terminal window (or a new one if you didn't run xmaple in the background) execute

    % cd ~/maple
    % ls
    first.mw phys210.mw
    

    And you should see first.mw in the listing. As usual, if you don't, ask for help.

Continuing ...

We can get help for any procedure or a general topic using ?, and in this case, there is no need to terminate the statement with a semi-colon.

Example: Note that it may take some time or the help facility to start.

> ?cos

and you should see a help window (facility) that you can familiarize yourself with as you work with xmaple.

Now, use the menu selection File -> Exit (keyboard shortcut Alt-F4) to quit xmaple Click No when asked you whether you want to save changes.

The interface should disappear (including the help window).

1.3.2 Loading a previously created worksheet

You can now restart xmaple from the command-line and load the saved worksheet.

First be sure the working directory is still ~/maple, and that the worksheet is there.

% cd ~/maple
% pwd
/home/phys210d/maple
% ls
first.mw  phys210.mw

Start xmaple in the background using first.mw as an argument ...

% xmaple first.mw &

... and observe that the commands you previously typed interactively appear. You can now add/remove commands, and save your work by overwriting first.mw, or by creating a new worksheet.

IMPORTANT!

When you open a previously created worksheet, Maple does not automatically (re-)execute all the commands that are contained within it. Again, this must be done explicitly via clicking on the !!! icon (or Edit -> Execute -> Worksheet from the top menu bar.) You will typically want to perform such reexecution for several reasons, including to ensure that variables that you have defined in the worksheet actually are assigned the values that you want.

Also note that you can open a worksheet by invoking xmaple without any arguments and then using File -> Open

Exit xmaple again.

1.4 Using command-line maple to perform calculations interactively

Start command-line maple (not xmaple) from the command-line in a terminal window.

% maple

You should see something like this

    |\^/|     Maple 18 (X86 64 LINUX)
._|\|   |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2014
 \  MAPLE  /  All rights reserved. Maple is a trademark of
 <____ ____>  Waterloo Maple Inc.
      |       Type ? for help.
>

Type the following commands and note the output from each

> integrand := cos(x)^3 * sin(x)^2;
> ans1 := int( integrand , x );
> ans2 := diff( ans1 , x );
> res1 := integrand - ans2;

Note that the result is not 0 as one might expect, so try simplification.

> res2 := simplify(res1);

And you should see that the simplification results in 0.

Quit maple ...

> quit;

1.5 Preparing maple commands in a text file (script file) to be read into a xmaple or maple session.

Within your new maple directory (~/maple), and using kate (or gedit, etc.), create a new file mapletest using

% cd ~/maple
% kate mapletest &

Now enter the following text in the file and save it. Note that any line that starts with a # is a comment (just like it is for the bash). For the purpose of this exercise, you don't have to enter any comments, but in practice, it is a good idea to at least minimally comment/document what you are doing, and you will be asked to provide such in-code documentation for some of the maple homework problems.

Note that the 5 commands are in fact identical to those we just entered in the interactive exercise.

# My first maple script file!

integrand := cos(x)^3 * sin(x)^2;

ans1 := int( integrand, x );

ans2 := diff( ans1, x);

# In principle, this (res1) should be 0 ...

res1 := integrand - ans2;

# Use simplify to make it so ...

res2 := simplify(res1);

Make sure the file is saved as mapletest in your maple directory.

In the shell window, verify that the file containing the commands has been created, and is in the correct location

% cd ~/maple
% ls
first.mw  phys210.mw   mapletest

1.6 Executing the commands in a script file using command-line maple

Start maple (not xmaple) from the command line

% maple

The maple read command tells maple to redirect its input from the file specified as its argument. Note that some maple commands, including read, do not have to have their argument(s) enclosed in parentheses (but using parentheses is allowed).

Type the following at the prompt (don't forget the semi-colon).

> read mapletest;

You should see the following output: as usual, if you don't, ask for help!

                                        3       2
                         integrand := cos(x)  sin(x)

                           4                     2
        ans1 := -1/5 cos(x)  sin(x) + 1/15 cos(x)  sin(x) + 2/15 sin(x)

                  3       2             5                     2              3
ans2 := 4/5 cos(x)  sin(x)  - 1/5 cos(x)  - 2/15 cos(x) sin(x)  + 1/15 cos(x)

     + 2/15 cos(x)

                  3       2             5                     2              3
res1 := 1/5 cos(x)  sin(x)  + 1/5 cos(x)  + 2/15 cos(x) sin(x)  - 1/15 cos(x)

     - 2/15 cos(x)

                                   res2 := 0

Observe that the maple commands (statements) that are read from the file do not appear on the screen, only the results.

Again, note how upon simplification res2 evaluates to 0, verifying that the derivative of the anti-derivative is the original function, as it should be.

Now exit maple ...

> quit;

... and you should be returned to the command-line prompt.

1.7 Executing the commands in a script file using xmaple

From within your ~/maple subdirectory, invoke xmaple from the command line.

First make sure that the working directory is ~/maple

% cd
% cd maple

Start xmaple

% xmaple

Enter the following at the prompt

> read mapletest;

This will again execute the commands contained in the file mapletest.

Save your calculations as the worksheet file mapletest.mw using the procedure described above for first.mw

Quit xmaple, start it up again, and this time, use the

or, from the menus

to locate, load and then execute the commands in mapletest.mw (How do I do the last step again? I.e. the execution?)

1.8 Final remarks

We have seen that there are two basic ways that we can do calculations in xmaple/maple.

  1. Typing expressions interactively.

  2. Preparing commands in a text file, then having maple execute them using the 'read' statement.

As we have already discussed in the context of gnuplot, the second approach is very useful and powerful. This is especially the case when programming in Maple since you will always want the procedures/programs to be stored in text files. This will enable you to modify and debug your Maple code more readily than from within the worksheet interface.

2 Adding annotations to a worksheet

This information will also be included in the Homework 2 handout.

2.1 Reinstall default style file

First, let's reinstall the course default maple style file so that your annotations will use the same font as my worksheet.

% cd
% cd maple
% cp ~phys210/maple/phys210.mw .
cp: overwrite './phys210.mw'? y

Start xmaple from the the command line as usual:

% xmaple &

Type in the following three Maple commands:

> a := evalf(Pi/8);
> diff(cos(tan(x)), x);
> plot(sin(x));

To create annotations, use the three icons located roughly under the Drawing label on the top menu bar.

From left to right, with the corresponding hover text emphasized, these are:

  1. An upper case T: Insert plain text after the current execution group

  2. An icon representing the Maple prompt: Insert Maple Input after the current execution group

    NOTE: Execution groups can also be inserted before and after the cursor using Ctrl-k and Ctrl-j, respectively, or via (Insert -> Execution Group -> ... from the top menu bar.

  3. An icon that resembles an hourglass---hovering the mouse over it displays the text Enclose the current selection in a document block, or create a new one

To insert a comment, position the cursor to the immediate right of the prompt on the line before the location where you want to do the insertion.

Click on the hourglass icon, and then on the T.

You can then type in plain text to create the annotation. Once the note (section heading, comment etc.) has been inserted, you can alter its appearance (font, font size, style etc.) by sweeping the text and using the icons and pulldowns immediately above the main input/output area.

Note

You can use Ctrl-z to undo changes.

2.1.1 Inserting annotations at the beginning of the worksheet

Since text blocks are always inserted after the line on which the cursor is positioned, it is slightly troublesome to begin a worksheet with an annotation.

One way to do so is as follows. First, insert an execution group before the initial command of the worksheet by positioning the cursor beside the corresponding prompt and typing Ctrl-k.

Using the above prescription, insert the text that is to appear at the beginning of the worksheet after the new execution group. Now delete the execution group before the annotation by repositioning the cursor beside the corresponding prompt, pressing and holding the Ctrl key and pressing the Delete key two times.

2.2 Annotation exercise

Exit the xmaple session (you do not have to save the worksheet), re-start xmaple, and retype the above three commands. Annotate the commands using the following descriptions. In each instance insert the comment (annotation) before the corresponding command, and ensure that when you are done that there are no command prompts before the first annotation.

Assign a value to a name

Compute a derivative

Make a simple plot

For the first annotation, use the procedure described above to insert the text at the top of the worksheet.

Exit xmaple: you do not need to save the worksheet, but you may if you wish.

3 Free time to work on homework as necessary.