Maple topics discussed in this chapter are:
4.1 Saving and Including User Written Programs
4.2 Solving Ordinary Differential Equations
4.3 Using Maple to Change Units
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.
Part a of session 4.1 shows a new session that uses the restart command to clear anything that might have been created in a previous session. Several expressions are defined in the session and a procedure called trigl is also typed in. The session is then saved using ``Save As ...'' under the File menu with the name ex1.mws.
Part b of session 4.1 shows the result of using the Open option under the File menu to load ex1.mws. It would appear that the result is exactly like we had at the end of the Part a. As you can see at the end of Part b, however, none of the expressions are really set. The variable x does not have the value set for it in Part a. If we wish to use the values given to variables and procedures, we must execute the commands in the workspace. This can be done by starting with the first command and hitting the Enter or Return key to execute each command in the space. It can be done with the Execute command under the Edit Menu. Using the second approach you can execute all the commands in the session or ones in a block that you select.
Part c of session 4.1 shows that we can continue a session that is opened after it has been saved.
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 four different text files : Plain Text, Maple Text, LaTeX and HTML. For simple sessions it does not matter whether you specify Plain Text or Maple Text when you export a file: both give the same result. If we have a session in which two expressions have been defined such as:
Exporting this session in either text format will produce an ASCII file with the following in it:
This file may be reloaded in a new Maple session by selecting the Open option from the File menu. To see the file when we open it, the File Type must be changed from Maple Worksheet to Maple Text:
You also have to specify the type of text file:
The selection Maple Text will give a new session that you can execute as seen on the left in the next figure. The selection Text will produce just text in the Maple window as seen on the right in the same figure.
You must again move the cursor to the line you want executed and then type Return in the session. All of this suggests that we could simply edit a file to have in it:
f1:=sin(z)
f2:=z^2-1;
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. Part d of Session 4.1 shows the way the function fib may be loaded and used.
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 if...else....fi 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:
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. Part a of session 4.2 demonstrates the way dsolve may be used to solve one ordinary differential equation and produce a function that may be checked to see that it really satisfies the DE and any boundary conditions that may have been inposed.
Part b of session 4.2 shows how to solve two ordinary differential equations. The technique may easily be extended to more equations than two. The session also shows how Laplace transforms may be utilized in the process.
4.3 Using Maple to Change Units
The symbolic feature of Maple makes it simple to keep track of the units of quantities. We may also change those units by the procedure shown in session 4.3.
4.4 Use of Maple to Find Laplace Transforms
There are many tables of Laplace Transform that could 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 to obtain more information on these functions, search the on-line help.
Forward transform:
Fs:=laplace(ft,t,s);Inverse transform:
ft:=invlaplace(Fs,s,t);
In order to use either command, the package inttrans (for integral transforms) has to be loaded.
Some examples are shown in session 4.4.
4.5 Using Maple to Transform ODEs
A second way of using Laplace Transforms to solve differential equations is shown in session 4.5. 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 gives more insight into the use of transforms however and is included for that reason.