




























































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!





























































































ii •
Maplesoft, Maple, and Maplet are all trademarks of Waterloo Maple Inc.
©cMaplesoft, a division of Waterloo Maple Inc. 2005. All rights re- served.
The electronic version (PDF) of this book may be downloaded and printed for personal use or stored as a copy on a personal machine. The electronic version (PDF) of this book may not be distributed. Information in this document is subject to change without notice and does not rep- resent a commitment on the part of the vendor. The software described in this document is furnished under a license agreement and may be used or copied only in accordance with the agreement. It is against the law to copy the software on any medium except as specifically allowed in the agreement.
Windows is a registered trademark of Microsoft Corporation. Java and all Java based marks are trademarks or registered trade- marks of Sun Microsystems, Inc. in the United States and other countries. Maplesoft is independent of Sun Microsystems, Inc. All other trademarks are the property of their respective owners.
This document was produced using a special version of Maple that reads and updates LATEX files.
Printed in Canada
ISBN 1-894511-76-X
This manual introduces the basic MapleTM^ programming concepts, such as expressions, data structures, looping and decision mechanisms, proce- dures, input and output, debugging, and Maplets.
Audience
As a Maple user, you may have only used Maple interactively, written Maple programs, or programmed in another computer language. You should be familiar with the following:
Worksheet Graphical Interface
You can access the power of the Maple computation engine through a vari- ety of user interfaces: the standard worksheet, the command-line^1 version, the classic worksheet (not available on Macintosh ©r), and custom-built MapletTM^ applications. The full Maple system is available through all of these interfaces. In this manual, any references to the graphical Maple
(^1) The command-line version provides optimum performance. However, the worksheet interface is easier to use and renders typeset, editable math output and higher quality plots.
Conventions • 3
Writing a Maple program can be very simple. It can be as straightforward as placing proc() and end proc around a group of Maple commands. However, using the Maple programming language, you can write Maple procedures that perform complex operations. Writing code in Maple does not require expert programming skills. Because Maple has a large library of routines, writing useful programs from these powerful building blocks is easy. Unlike traditional program- ming languages, with the Maple programming language you can perform complicated tasks by using a single Maple library routine. In addition, you can use the Maple programming language to automate long or repetitive sets of instructions. Ninety percent of the thousands of routines in the Maple language are Maple programs. You can examine these Maple programs and modify them to suit your needs, or extend them so that Maple can solve new types of problems. You can learn the fundamentals of the Maple programming language and write useful Maple programs in a few hours, rather than the days or weeks that it often takes to learn other languages. This efficiency is partly a result of the fact that Maple is interactive. This interaction makes it easier to test and correct programs.
Examples Perform the examples shown in the manual as you read. To strengthen your knowledge, try variations or refer to the related online help page(s).
Troubleshooting At the end of most chapters, there is a Troubleshoot- ing section that lists common errors encountered while performing the examples in the chapter. See this section if you receive an error that you do not understand.
Exercises Develop solutions to the problems posed in the Exercises at the end of each chapter. This consolidates and extends your learning.
4 • Preface
Customer Feedback
Maplesoft welcomes your feedback. For suggestions and comments related to this and other manuals, email [email protected].
6 • Chapter 1: Introduction to Programming in Maple
The user interface handles the input of mathematical expressions and commands, the display of output, and the control of the Maple work- sheet environment options.^1 The basic Maple system, the kernel, is a relatively small collection of compiled C code. When a Maple session is started, the entire kernel is loaded. It contains the essential facilities required to run Maple and per- form basic mathematical operations. The components include the Maple programming language interpreter, arithmetic and simplification routines, print routines, memory management facilities, and a collection of funda- mental functions. Its small size ensures that the Maple system is compact, portable, and efficient. In this guide, the kernel routines are referred to as built-in routines. The library contains the majority of the Maple routines. It includes functionality related to calculus, linear algebra, statistics, graphics, and many other topics. The Maple library consists of individual routines and packages of routines. These routines are accessed and interpreted by the Maple system as required. As a result of this modularity, the computer consumes resources proportional to only the facilities that are used, en- hancing the efficiency of the system. All library routines, which are im- plemented in the high-level Maple programming language, can be viewed and modified by users. Therefore, it is useful to learn the Maple program- ming language so that you can modify existing Maple code to produce customized routines.
1.2 Maple Statements
The Maple software runs on many different platforms. Depending on the platform, you use its specialized worksheet interface or command-line in- terface. In both cases, when a Maple session is started, the Maple prompt character (>) is displayed.
This prompt character indicates that Maple is waiting to receive input in the form of a Maple statement.^2
(^1) For more information about the Maple user interface and worksheets, refer to the Maple Getting Started Guide or Maple User Manual. (^2) Throughout this book, the Maple notation (or one-dimensional) input format is
1.2 Maple Statements • 7
There are many types of valid statements. Examples include statements that request help on a particular topic, display a text string, perform an arithmetic operation, use a Maple library routine, or define a procedure.^3 Most Maple statements must have a trailing semicolon (;) or colon (:). If you enter a statement with a trailing semicolon, for most statements, the result is displayed. However, if you enter a statement with a trailing colon, the result is computed but not displayed.
(^) 2 + 3;
5
(^) 2 + 3:
To view an online help page for a particular topic, enter a question mark (?) followed by the corresponding topic name. For example, ?procedure displays a help page that describes how to write a Maple procedure.^4 This type of Maple statement does not have a trailing colon or semicolon.
The following statement returns a string. The text that forms the string is enclosed in double quotes, and the result (the text string) is displayed because the statement has a trailing semicolon. In the second example, no result is displayed because the statement has a trailing colon.^5
(^) "Hello World";
“Hello World”
(^) "Hello World":
used to enter Maple statements. For more information on starting a Maple session, toggling between Maple notation (the default on most platforms) and standard math notation, and managing your files, refer to the Maple Getting Started Guide and Maple User Manual or enter ?managing at the Maple prompt. (^3) For more information about statements in Maple, see chapter 3. (^4) For more information about getting help in Maple, refer to ?help and ?HelpGuide. (^5) For more information about strings in Maple, see chapter 2 or refer to ?string.
1.3 Procedures • 9 103993 16551
Once a value is assigned to a name, for example, the value assigned pre- viously to a, you can use the name as if it were the assigned object. For example, you can use the Maple library routine evalf to compute a floating-point (decimal) approximation to 103993/33102 divided by 2 by entering the following statement.
(^) evalf(a/2);
You can use the Maple library of routines, introduced on page 6, for many purposes. For example, you can find the derivative of an expression by using the diff command.^9
(^) diff(x^2 + x + 1/x, x);
2 x + 1 −
x^2
1.3 Procedures
This section formally introduces the concept of procedures in Maple. For more information about procedures, see chapter 6.
A Maple procedure (a type of program) is a prearranged group of state- ments processed together. The easiest way to create a Maple procedure is to encapsulate a sequence of commands, which can be used to per- form a computation interactively, between the proc(...) and end proc statements.
(^9) For more information about the Maple library routines, refer to the Maple User Manual or the online help.
10 • Chapter 1: Introduction to Programming in Maple
Entering a Procedure Definition The following procedure generates the string “Hello World‘. Enter this procedure in a Maple session by entering its definition on one line.
(^) hello := proc() "Hello World"; end proc;
hello := proc() “Hello World” end proc
For improved readability, enter the procedure on multiple lines: hold Shift and press Enter at the end of each line.^10 Indent lines in the procedure by using the spacebar. When you enter the last line, which contains end proc, press Enter.
(^) hello := proc() (^) "Hello World"; (^) end proc;
hello := proc() “Hello World” end proc
To run this procedure, enter its name followed by a set of parentheses and a semicolon. Enclose any input to the procedure—in this case none— between the parentheses and delimited (separated) by commas (,).
(^) hello();
“Hello World”
The next example is a procedure that uses the evalf command.
(^) half := proc(x) (^) evalf(x/2); (^) end proc;
half := proc(x) evalf(1/ 2 ∗ x) end proc
This procedure requires one input, x. The procedure computes the approximation of the value of x divided by 2. A Maple procedure re- turns the result of the last executed statement. Since evalf(x/2) is the last calculation performed in the procedure half (in fact, it is the only calculation), the approximation of x/2 is returned.
(^10) For more information, see Unexpected End of Statement on page 17.