Advanced Maple Topics

Maple topics discussed in this chapter are:

4.1 Saving and Including User Written Programs
4.2 Solving Ordinary Differential Equations
4.3 Converting Expressions to Functions
4.4 Use of Maple to Find Laplace Transforms
4.5 Using Maple to Transform ODEs
Each topic will be treated in a brief fashion with examples to illustrate the main points. More information may be found in the Maple Tutorials and Reference Manual.

4.1 Saving and Including User Written Programs

If you want to keep some expressions that you have created in a session, you may do so by using the Save As button on the File menu. Then, when you want those expressions in another session use the File menu's Open option. File names must be specified in the appropriate menu slots when you use these operations.

Suppose you open a new Maple worksheet by selecting the New option under the File menu. Type the following:

Then use ``Save As ...'' under the File menu and type the file name as ex1 after clicking in the Selection box. You will find that you have created a file called ex1.mws that includes font information as well as the instructions in an unreadable form. This file may be reloaded into a Maple session by using the Open option in the File menu. After doing that, you may think that the two variables: f1 and f2 have been set, but this is not so. In spite of the fact that the session window shows:

If we move the cursor to the first line and hit four returns, we see that we have defined the variables and are back to the point where we were when we saved our session.

The file can also be saved as a text file by using the ``Export As'' option under the File menu. The Export option allows the user to select three different text files : Plain Text, Maple Text, and LaTeX. For the purposes of exporting text files to be used in later Maple sessions, try to use Maple Text. It opens the commands saved in the text file as Maple commands rather than plain text (as is the case with Plain Text).

This file may be reloaded in a new Maple session by selecting the Open option from the File menu. Under the Open Window box, the File Type must be changed from Maple Worksheet to Maple Text:

Just as when we opened the .mws file, you will find that the variables have not been defined. If you type f1; you will simply get back f1 rather than the expression for it. You must again move the cursor to the line you want executed and then type Return. The above suggests that we could simply edit a file to have in it:

By importing the text file as described above and executing those instructions to have f1 and f2 defined, you have created and executed a program. Programs are nothing more than sequences of instructions which are stored in a file. If we read them into a Maple session, the instructions may be selected and executed as stored. We need to understand how Maple handles several of the usual typical ``programming" procedures including:
1) insertion of comments
2) looping operations
3) decisions or choices
4) specification of arguments
Comments should always be used to explain what a procedure does. In Maple, comments follow the pound sign: #.

Maple provides both for and while loops just as in MATLAB. Sequences of statements included in such loops must be enclosed between do and Maple's end of do: od. For loops incorporate a counter variable with delimiters from, by, and to to set the range and step for the counter.

A simple example demonstrates generating a table of trigonometric values. We will first create the ``program'' in a Maple session. In order to associate arguments with the program, the proc heading must be used in the definition and the program must have an end command to terminate it. (See ? proc) The whole program could be typed in as a single line, but it is simpler to use lines to see the various parts. Type the following using Line Feed (Shift-Return) instead of Return.

It is not very pretty, but it works. We could also have stored the program in a file. If we had it stored in file trigl.

Then we could read the file in and execute it as before:

The if structure in Maple closes with fi. A simple version of a program to generate Fibonacci numbers was stored in file: fib. It was written to show the way if..then..else structures may be used:

It uses recursion to generate the integer that is the sum of the function at the two integers below the argument. The first two values of the function are defined to be 1. It uses if, then, else and fi coupled with a call of the same program to find the numbers. We can proceed by using the read command. This gives:

The while statement may be used to create loops if it is combined with do....od around a set of statements or it may be used with a for sequence. The may also include ``else if'' parts. The abbreviation for else if is elif.

4.2 Solving Ordinary Differential Equations

The command dsolve allows you to solve certain ordinary differential equations. The form is similar to the solve command:

dsolve(deqns, vars);

The first argument must include one or more differential equations while the second argument specifies the variables to be determined. The first argument may also specify initial or boundary conditions. Here are a couple of examples:

Differential Equations Example 1: One Linear ODE

Differential Equations Example 2: Two Simultaneous ODEs

There are also other ways to solve differential equations. For example:

4.3 Converting Expressions to Functions

In the second example problem on solving ordinary differential equations we found an expression that gave the solutions for the two unknowns: x1(t) and x2(t). If we need to have one of those solutions as a function to be used in further calculations, we should become acquainted with several new commands. The two that are most useful in this conversion task are op and unapply. First, the op command:

Let's see how we can use this to extract a part of the expression returned by dsolve:

Next the unapply command:

We can use unapply to convert x2e to a function:

4.4 Use of Maple to Find Laplace Transforms

There are many tables of Laplace Transform that might be used to transform equations (particularly differential equations) into forms which can be solved. However, it is generally quite tedious to look up the tables.

Maple has the ability to find both Laplace Transforms of functions and their inverses. We will demonstrate some of these and compare them with the entries in a table of Transforms. We will also give some of the limitations of the current version of Maple to find the both the transforms and their inverses. In order for more information on these functions, search the on-line help.

Forward transform:

Inverse transform:

In order to use either command, the package inttrans has to be loaded.


Here are some examples:

4.5 Using Maple to Transform ODEs

A second way of using Laplace Transforms to solve differential equations is shown in this section. The first way shown using the laplace option in dsolve is probably easier and should be followed in most cases. The direct use of transforms shown here gives more insight into the use of transforms however and is included for that reason.

Maple may be used to transform a differential equation, but some care must be taken to follow the conventions adopted for this procedure. We may define a differential equation with the diff function:

Continue on to Chapter 5
Return to Table of Contents