------------------------------------------------------------------------- PHYS210 MAPLE 1 LAB SUMMARY Introduction to use of xmaple & maple ------------------------------------------------------------------------- ************************************************************************ 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 In the exercises that follow, we will start xmaple from the command line; it can also be run via the launcher (use Dash to locate, drag-and-drop to launcher) ************************************************************************ 2) Initial configuration of xmaple ************************************************************************ IMPORTANT: Performing the following configuration steps is CRUCIAL to ensure, e.g., that format of answers for Homework 2 is correct (will make input/output format same as notes/worksheets covered in class) ... please ask for help should you have any problems making the appopriate settings. From the COMMAND LINE of the terminal window that you have opened, execute % xmaple BE SURE TO TYPE 'xmaple', NOT 'maple' The main Maple window should open, and within it a panel named 'Startup' should appear STEP 1 - Uncheck the 'Show this dialog on setup' box in the 'Startup' window, then click 'Close' STEP 2 The 'Quick Help' panel should appear - Uncheck the 'Show on new Documents' box, and close the panel (x at upper right) --- in the future, should you want to access 'Quick Help', press F1 STEP 3 From the top menu bar: Choose Tools -> Options ... (Options panel appears) - Click on 'Interface' tab, select 'Worksheet' from 'Default format for new worksheets' menu STEP 4 Options panel should still be visible - Click on 'Display' tab, select 'Maple Notation' from 'Input display' menu STEP 5 Display tab should still be open - Uncheck 'Show equation labels' ------------------------------------- IMPORTANT: NOW CLICK 'Apply Globally' ------------------------------------- STEP 6 Exit xmaple using 'File -> Exit' from the top menu bar NOTE: For steps 3 and 4, the initial settings are 'Document' and '2-D Math Notation', respectively) ************************************************************************ 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 The xmaple window should look like mine: if it doesn't, repeat the above configuration steps and/or ask for assistance. Try the following calculations, and NOTE THAT WITH THE CONFIGURATION WE HAVE SET, EVERY MAPLE STATEMENT/COMMAND MUST END WITH A SEMI-COLON. In some 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 xmaple 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(%); # When using the factorial symbol (!), it is not necessary to end # the command with a semi-colon, but you can if you wish > 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 ); #------------------------------------------------------ # BEGIN: SAVE YOUR WORK AS A MAPLE WORKSHEET #------------------------------------------------------ Use 'File -> Save As' to find the directory ~/maple, then type first or first.mw in the 'File Name' box (the extension .mw stands for 'Maple Worksheet') Ensure that the 'Files of Type' menu selection is 'Maple Worksheet (.mw))' Click 'Save'. Verify that the worksheet has been created. In a new terminal window (don't close the one that you used to start 'xmaple') execute % cd ~/maple % ls first.mw And you should see first.mw in the listing #------------------------------------------------------ # END: SAVE YOUR WORK AS A MAPLE WORKSHEET #------------------------------------------------------ # Continuing ... # Get help for any procedure or a general topic using '?' (another # exception # to the rule that all maple statements should end # with a semi-colon) # Example ... It will take quite a bit of time for the help # facility to start up > ?cos # and you should see a help window (facility) that you can familiarize # yourself with as you work with xmaple # Use File -> Exit to quit xmaple ... Answer 'No' to the prompt # asking you whether you want to save changes # ... the GUI should disappear (including the help window) 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 Start xmaple using first.mw as the 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. This must be done explicitly via 'Edit -> Execute -> Worksheet' from the top menu bar. You will typically want to perform such reexecution for several reasons, including ensuring 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' If you wish, you can try some examples of your own later in the lab, or at your own convenience. ************************************************************************ 4) Using command-line maple to perform calculations interactively ************************************************************************ Start command-line maple from the command line in a terminal window % maple You should see something like this |\^/| Maple 16 (IBM INTEL LINUX) ._|\| |/|_. Copyright (c) Maplesoft, a division of Waterloo Maple Inc. 2012 \ 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; ************************************************************************ 5) Preparing maple commands in a text file to be read into a xmaple or maple session. ************************************************************************ Within your new 'maple' directory (~/maple), and using your text-editor of choice (probably gedit), create and save the file 'mapletest', having contents given by the lines between the two dashed lines that follow. For the purpose of this exercise, you don't have to enter the comments---i.e. any lines that begin with '#'---in the file, so there are only 5 lines (commands) that you need to type. BE VERY CAREFUL TO ENSURE THAT EACH LINE/COMMAND ENDS WITH A SEMI-COLON (;). Also note that the 5 commands are identical to those we just entered in the interactive exercise. Make sure the file is saved as 'mapletest' in the 'maple' directory. ------------------------------------------------------------------------- ####################################################### # This is a simple file containing Maple commands that # can be read into a Maple session (either the command- # line or GUI-version) ####################################################### ####################################################### # Compute a reasonably nasty indefinite integral # (anti-derivative) and assign the result to the # variable 'ans1'. Note the use of ^ for exponentiation, # and ensure that each Maple command ends with a # semi-colon, so that each command is properly # terminated and so that you will see the results # as they are executed. Also note that when computing # anti-derivatives Maple does NOT include the arbitrary # constant that typically appears; i.e. it chooses # the specific anti-derivative for which the arbitrary # constant of integration has been set to 0. ####################################################### integrand := cos(x)^3 * sin(x)^2; ans1 := int( integrand, x ); ####################################################### # Differentiate 'ans1' and assign the result to 'ans2' ####################################################### ans2 := diff( ans1, x); ####################################################### # Verify that 'ans2' and 'integrand' are equal ... You # might think that the following should evaluate # to 0 ... ####################################################### res1 := integrand - ans2; ####################################################### # ... but it doesn't! So try simplifying the last # result ... ####################################################### res2 := simplify(res1); ####################################################### # Bingo! ####################################################### ------------------------------------------------------------------------- Verify that the file containing the commands has been created, and is in the correct location % cd ~/maple % ls first.mw mapletest ************************************************************************ 6) Executing the commands in the file 'mapletest' 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 enclosed in parenthesis (but using parenthesis 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. % ************************************************************************ 7) Executing the commands in the file 'mapletest' using xmaple, and saving them as a worksheet ************************************************************************ From within your ~/maple subdirectory, invoke xmaple from the command line. 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 File -> Open browser to locate, load and execute the commands in mapletest.mw ************************************************************************ FINAL NOTE: 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. The second approach is especially useful when writing Maple procedures (i.e. programming in Maple), since you will generally *want* the procedures/programs to be stored in text files, so that you can modify/debug your maple code easily. ************************************************************************