2008
Biological Simulation Techniques: The 4430graf.bas Program
As you work through the exercises in the textbook, you will find that many of
them require graphical output in the form of the familiar Cartesian X-Y plots.
The 4430graf.bas program is helpful in performing many of these exercises.
Appendix 2 of the textbook discusses some features of a graphing program
written in older dialects of BASIC. The graphing program made available to
you for Just BASIC is improved in several aspects over the program in the
text, as well as being written in a version of BASIC adapted to the Windows
operating system. Specifically, the Just BASIC version makes it possible to plot
several lines simultaneously. This can be a real time-saving feature in long,
complex simulations.
Drawing the Graph Axes: The initial part of 4430graf.bas remains about the
same as in the textbook. That is, the graph is drawn by setting dimensions of
the graph axes with xm, xn, ym, yn, and labelling the axes with x$ and y$,
followed by gosub [axes]. For example:
xm=50 : xn=0 : ym=20 : yn=-20
x$="Time" : y$="Biomass" : gosub [axes]
Plotting Single Points and a Single Line: The plotting of x-y data as simple
points involves specifying the values of X and Y, followed by gosub [plotXY]
to draw the point. This can be done as the values are being generated by the
program. For example:
x=t : y=m : gosub [plotXY]
Unfortunately, points plotted in this way appear as very small dots, particu-
larly when you print the graph on paper. This can be corrected by connecting
the points with a line.
You may instruct the program to connect the points with a line by inserting the
statement xa=1 just before the gosub [plotXY] statement. For example:
x=t : y=m : xa=1 : gosub [plotXY]
Essentially, this instructs the computer as follows: "Plot the x-y point, and
then connect it with a line from the previous x-y point (that was plotted with
a line)".
It is also possible to plot points with a small open square (box). This can be
done using the command xb=1 just before gosub [plotXY]. For example:
x=t : y=m : xb=1 : gosub [plotxy]
Similarly, points may be plotted with small open circles using xc=1 just before
gosub [plotXY]. For example:
x=t : y=m : xc=1 : gosub [plotXY]
It is possible to use two plotting methods in combination for a single point.
For example, the following statements would plot a circle with a small point in
the middle:
x=t : y=m : gosub [plotXY]
xc=1 : gosub [plotXY]
You could plot a series of boxes connected by a line by using the following
statements:
x=t : y=m : xb=1 : gosub [plotXY]
xa=1 : gosub [plotXY]
Plotting Multiple Lines: The 4430graf.bas program has the ability easily to
plot up to 10 lines simultaneously. It is generally better practice to use
lines for multiple plots on the same axes rather trying to use boxes and
circles for different quantities. The eye has a difficult time following
separated points when they intermingle.
Suppose you are calculating simultaneously the masses of two system components,
say MP and MQ, and you wish to plot them at some time t. This may be accom-
plished with the following statements:
x=t : y=mp : xa=1 : gosub [plotXY]
y=mq : xa=2 : gosub [plotXY]
Essentially, this instructs the computer as follows: "Draw the x-y point for
line #1, and then connect it with a line from the previous x-y point for line
#1. Next, draw the x-y point for line #2, and then connect it with a line from
the previous x-y point #2."
(In some rare cases, students want to interrupt the process of connecting of a
series of x-y points with a line. The plotting of line #2, for example, can
be interrupted by interpolating xd(2)=0 between xa=2 and gosub [plotXY]. The
new point for line #2 will be plotted, but will not be connected with the
previous point for line #2.)
Drawing a Straight Line: In some simulations, it might be necessary to
draw a straight line between two points on the graph as part of a construction
of the graph. For example, suppose the x-axis was labelled from -30 to +30,
and you wished to draw a vertical line at X=0. The following sequence would
accomplish this:
x=0 : y=yn : xa=1: gosub [plotXY]
y=ym : xa=1 : gosub [plotXY]
Labelling Graphs: Labels and other information can be included in the
graphical output as part of your programs. This is particularly useful if you
are plotting two lines on the same set of axes, and they need to be identified.
You will need to decide what you want to write on the output window and then
where you want to write it.
An example of putting a text label on a graph is found in the sample
4430graf.bas program:
x0=200 : y0=30 : xl$="Sine Curve" : gosub [Label]
The text message to write to the window is identified as the variable xl$ (with
a lower-case L). In the example above this is xl$="Sine Curve". The location
for starting to write the printed text is given by the variables x0 and y0. The
subroutine for writing the text in the window is started with gosub [Label].
Identifying the proper position in which to write the label can be a matter of
trial-and-error, until you are satisfied with the placement of the label. The
graphic window measures 480 units high (x-units) by 640 units wide (y-units).
The upper left corner of the screen has a position x0=0 and y0=0. The lower
right corner has th position of x0=480 and y0=640. Starting to write a label
in the exact middle of the window would used the labelling subroutine with
x0=240 and y0=320. You should be able to identify approximately the desired
location, and then if necessary proceed with trial and error.
flush - wait - end: Note that the sample program ends with the Just BASIC
commands #1,"flush", wait, and end. It is a good idea to include these at the
end of your own programs. Otherwise, Just BASIC may exhibit some erratic
behavior as you work through a series of exercises.
Copyright © Michigan Tech
All Rights Reserved - 08Apr08 Inquiries to
4430 instructor
|