Lesson 3: Writing a Procedure
One of the best methods to learn how to use a programming language is to study examples and then copy and modify some short programs containing relevant code. This lesson not only provides building blocks for more detailed programs, it also gives you rapid, hands-on experience in using some of the powerful mathematical routines in PV‑WAVE IMSL Mathematics and PV‑WAVE IMSL Statistics. Note that PV-WAVE IMSL Mathematics and PV-WAVE IMSL Statistics are only available if you purchased PV-WAVE Advantage.
Since this lesson is designed to demonstrate the powerful mathematical and statistical capabilities of PV‑WAVE IMSL Mathematics and PV‑WAVE IMSL Statistics, detailed explanations for some of the more “basic” commands are presented elsewhere in this tutorial.
The exercises in this lesson show you how to execute sample programs. All the programs referenced in this lesson are located in:
(WIN) <RW_DIR>\docs\tutorial\code\advantage
(UNIX) <RW_DIR>/docs/tutorial/code/advantage
Where <RW_DIR> is the PV-WAVE installation directory.
All the examples are written in ASCII text and have a *.pro extension. The examples can be printed, edited, and read.
In this lesson, you will do the following:
Create an Executable Program File
PV‑WAVE executable programs (*.pro files) are text files that can be created from within PV‑WAVE or with a text editor. Using a text editor, you can compose and edit the file. When you create the file from PV‑WAVE, you have the advantage of having each line compiled as you enter it.
You do not need to exit PV‑WAVE to use the text editor. You can open the text editor in another window, or you can use the SPAWN command within PV‑WAVE. For example, on a UNIX system, you could begin the file within PV‑WAVE by entering:
$ vi filename
If PV‑WAVE is not running, restart it now to clear the memory buffer and to begin the lesson using PV‑WAVE’s default settings for character size, colortable, etc.
You also need to start PV‑WAVE IMSL Mathematics to run the following example. To do this, type the following command:
@waveadv
This exercise creates a program, which you will save as quadrature.pro, that uses the PV‑WAVE IMSL Mathematics quadrature function, INTFCN, to evaluate the following integral:
Look at the program you will be typing (step 2 below). Note that the integrand is singular at both end points. The PV‑WAVE IMSL Mathematics function INTFCN can handle endpoint singularities, and also allows you to specify internal singularities as well. First, the value of the integral is computed and compared to the exact value. This information is returned in PV‑WAVE when the program is run. Next, the function ln sin(x) in the interval x = [0.01, π – 0.01] is plotted in a PV‑WAVE window.
To create the executable file, do the following:
1. Open a text editor in another window. When you type the commands shown here, pay careful attention to the punctuation marks and special characters.
2. Type or paste the following program into a text editor.
FUNCTION f, x
RETURN, ALOG(SIN(x))
END
PRO QUADRATURE
exact = -!Pi * ALOG(2)
result = INTFCN("f", 0, !Pi)
PRINT, 'Computed answer = ', result
PRINT, 'Exact answer = ', exact
PRINT, 'Relative error = ', ABS((exact - result)/exact)
x = .01 + FINDGEN(101)/100*(!Pi - .02)
y = f(x)
PLOT, x, y, Title='!17Graph of ln(SIN(x))', $
XTitle='x', YTitle='y', CharSize=2, Thick=2
END
3. Save the program as quadrature.pro.
Compile and Run Your Program
After saving the file you are now ready to compile and run the program.
To compile and run the quadrature program, do the following:
1. At the PV‑WAVE prompt, type:
cd, 'mydirectory'
where mydirectory is the pathname to the directory where quadrature.pro file resides. PV‑WAVE moves to your directory.
2. To compile the quadrature.pro file, type:
.RUN quadrature
The .RUN command compiles a program, returning error messages if syntax errors are present. You do not need to issue the .RUN command for a compiled program unless you want to force recompilation.
Two messages appear, telling you PV‑WAVE compiled the module F and the module quadrature.
If you made any errors when you typed the file, you will probably receive an error message that will aid you in locating the error.
If you made any errors, return to the text editor and correct them, save the file, then recompile.
3. To run the quadrature.pro program, type:
quadrature
The program runs, printing the following to the PV‑WAVE console or home window:
Computed answer = -2.17758
Exact answer = -2.17759
Relative error = 6.56925e-007
4. Close the window by entering the following:
WDELETE
The plot window closes.