Teletype graphics for chemical education - Journal of Chemical

Producing graphics, particular plots, using features of BASIC. Keywords (Audience):. First-Year Undergraduate / General. Keywords (Pedagogy):...
0 downloads 0 Views 4MB Size
Leonard J. Soltzberg

Simmons College Boston, Massachusetts 021 15

Teletype Graphics for Chemical Education

T h e application of computer simulation in chemical education is a fertile area for development. The use of a computer to simulate a chemical system or an instrument can overcome such limitations as a lack of time for a n extensive series of laboratory experiments or a lack of mathematical sophistication on the part of students. A number of articles and notes describing digital computer simulation of chemical systems have appeared in recent issues of THIS JOURNAL.^ The development of computer time-sharing, the use of the language BASIC, and the availability of inexpensive teletype terminals promise to make such techniques widely available to educators in colleges and secondary schools. The effectiveness of computers in simulation is greatly enhanced when the computer output appears in graphic form. A device such as an on-line plotter or a cathode ray display can produce remarkable graphics. However, such devices are quite expensive a t present,% and their use requires relatively sophisticated software. Fortunately, the educator with only a teletype terminal a t his disposal can use the teletype to produce a range of quite serviceable graphics by a straightforward use of certain features of BASIC. The intent of this paper is to suggest a few approaches that will enable a person with some programming experience to incorporate graphic teletype output into simulation programs of interest in chemical education.

(c) an explicit function of two independent variables (contour plot), z = f(z,y).

I n general, any data points arising from an explicit, analytic relationship in Cartesian coordinates would constitute a Class I plot. Class I1 plots include polar coordinate plots, parametric equations, and experimental data. General considerations

Before considering the specific approaches to each of these graphic classes, let us examine the considerations common to producing any plot on the teletype. First, one must decide how the axes are to be chosen. A major limitation of the teletype as a graphic device is that the horizontal axis (paper width) is limited to 72 discrete positions; the vertical axis (paper length), while also discrete, is not limited in length. Thus, in plots having the general form y = f(x), the vertical axis would naturally he chosen to represent the independent variable to allow for varying the resolution of the plot. For consistency, we shall always choose the vertical axis to represent the graphic abscissa. Since the teletype can only represent discrete positions, the data to be plotted must he converted into a form which corresponds to these positions. This means the data must be integerized and scaled. This is easily accomplished with a program section of the following form

Types of Plots

From a programming point of view, it is convenient to identify two broad classes of teletype plots (I) Plots with point(s) plotted for every value of the graphic abscissa (11) Plots with point(s) plotted for not every velue of the graphic abscissa

The way in which one organizes the data to be plotted depends on the class into which the plot falls. Plots of Class I further divide into several distinct types, each best treated in a somewhat different fashion. These are a single explicit, analytic function, y = f(z) (h) several explicit, analytic functions, yl = f(z) Y* = g(z), etc.

(a)

See, for example, Reference (1-14). On-line plotters for timesharing use are available for around $3500. Cathode ray terminals with graphics capability cost $400W10,000. For clarity, we shall define (raw) data x and y as the value of z and Y as they arise from a computation or experiment. By graphic z and y, we shall mean those values of z and y corresponding to the discrete points on the teletype paper which are accessible to the teletype printing element. a

Here, Y2 and Y3 are the minimum and maximum values of the raw data y k 3 X2 and X3 are the minimum and maximum values of the raw data x's; and XI is the desired increment size (in terms of raw x values) for the abscissa. For simplicity, we shall assume that we input values for Y2, Y3, X2, X3, and XI, though a program could be constructed to find these limits automatically. Note that the integerizing and scaling procedure has been incorporated into the user-defined functions F N I and FNJ. A Single Anal*ic E

, ~

~~

curve ~f i~

~ ~~

~~

;~

~

~

We can now proceed to consider the various types of teletype plot, ~h~ simplest lcind involves a single ex. plicit, analytic function of the form Y = f(x). Such a function is inherently single-valued. An electron Volume 49, Number 5, May 1972

/

357

diffraction theoretical scattering curve, obtained from the Wierl equation, is such a case. Here, we wish to plot something similar to y = sin (r.x)/(r.r) versus r (for various trial values of r). For this kind of plot, it is most convenient to utilize the TAB feature of BASIC. The desired function is evaluated iteratively for successive values of the data I, and the resulting data y's are scaled, integerized, and used as the argument of a TAB function in the PRINT statement which types the plotting symbol. The plotting sequence (to follow the scaling and integerizing sequence) would be 1000 lOl0 1020 1030 1040 1050 1060 1070 1080

FOR X = X 2 TO X3 STEP X1 LET Y - F N B X) LET Y = F N J ( ~ ) PRINT ".". IF Y>71 ?HEN 1070 IF Y < l THEN 1070 PRINT TAB(Y);"*"; PRINT NEXT X

(NOTE:the ( ' I ) and (") are equivalent on a teletype.) FNB is a user-defined function embodying the desired computation, in this case, the Wierl relation. Any explicit, analytic function could thus be plotted with this sequence. It is useful to have the abscissa and ordinate printed as part of the plot; statement 1030, above, prints the abscissa. To print the ordinate, we may add a suhroutine to be called prior to and following the plotting of the data points. Such a subroutine, together with the appropriate calling statements for the above example, is 900 GOSUB 9000 ZOO0 GOSUB SOW 8998 STOP 9000 PRINT " ". 9010 FOR I = 1 TO 71 9020 PRINT " " ; 9030 NEXT I 9040 PRIN9050 RETI

A theoretical scattering curve plotted by the ahove method is shown in Figure 1.

Figure 2.

R(r1 ond ?RaIrl for o 2s electron of a hydrogen atom.

alternative PRINT statements to he used for the various situations y~ > y,, y, = y,, y~ < y,. Such an approach is inelegant; and, for more than two functions, it is quite impractical. It is preferable to utilize a different plotting technique capitalizing on the "string variahle" ("literal variable") feature of BASIC. I n this method, each horizontal line of the plot is built up as a string variable, which is then PRINTed. For the wavefunction example, we would have LET M = 2 LET 2$="12" FOR X = X 2 TO X3 STEP FOR I = I TO ,72,, LET AI(I.1) = NEXT I LET Y ( l ) = F N A ( X ) LET Y(2) =FNB(X) LET J l = M FOR 1 - 1 TO M LET Y(1) =FNJ(Y(I)) IF Y(I)>71 THEN 1130 IF Y ( I ) < l ' T H E N 1130 IF AI(Y(I).Y(I))(I" THE ~ ~ T T ~ % < ~ p=Z%(I) Y ( l ) ) LET LET J1-J1-1 NEXT I LET J2-0 PRINT FOR 1 = 1 +O 71 IF J2-J1 THEN 1230 PRINT AI" -' IF AX1 I ) : LET $2'-3.. NEXT I PRINT NEXT X

".".

I n this example, FNA would be established (using a

DEF statement) as the radial part of the hydrogenlike

......................................... : Figure 1.

Eleetrcmn diffrostion theoreticalscattering curve for COz.

Several Analytic Functions Radial Wavefunction

It is frequently of interest to plot two or more functions on the same graph. Such a case might be the plotting of the radial wavefunction and the radial electron density function for a hydrogen-like atom. The case of two functions can he handled using the TAB function as above, but one must create several 358 / Journal of Chemical Education

wavefunction. FNB would be defined as (Xt2)* ((FNA(X)) t 2), the radial electron distribution function. The key line here is 1100, in which the data scaled and intergerized y value, Y(I), is used to specify the location for the plot symbol in the string variahle A$.4 The plot symbols can be any printable characters and are set up in the string variable Z$; we have chosen "1" to plot FNA and "2" to plot FNB. M, in the above sequence, is the number of functions to be plotted on the graph. J1 and J2 are used to keep track of the number of points plotted on the plot line in order to avoid wasting time in unnecessary teletype motion. A plot produced by this sequence is shown in Figure 2. 'In versions of BASIC allowing string arrays, the plot line A W (I),Y(I)) should simply be treated as s. string array, A$(Y(1)).

Optical Absorption Spedro

This same type of sequence can be used to plot several different explicit, analytic functions on the same graph. One need only change the value of M, specify the desired functional relationships, and expand Z$ to provide enough plot symbols. An interesting application of this multiple plot capability is the simulation of the absorption spectra of several (say, five) different equilibrium mixtures of two interconvertible species. This could be achieved by the following substitutions 980 LET M=5 990 LET Z$= 12345" 1040 1045 1050

from 1 to N which is used to select the appropriate plotting symbol. A$ is a string variable which contains the available plotting symbols, here chosen to be the twenty-six letters of the alphabet. To produce the plot, we must set up a loop on both independent variables, X and Y FOR X=XZ TO X3 STEP X I FOR Y = Y 2 TO Y3 STEP Y1 LET Z = F N Z ( X ) LET Zl=FNJ 2) IF ( A B S ( Z ~ - ~ N I ( Z ~ ) > N THEN I IF Z l > 7 l THEN 1070 IF Z l < l T H E N 1070 PRINT AS(Z1.21); GO TO 1080 PRINT " ". NEXT Y ' PRINT NEXT X

FOR I = 1 TO M LET Y = F N C ( I ) NEXT I

In this example, FNC would be defined earlier in the program as a sum of two displaced Gaussian functions of unequal amplitude, each corresponding to one of the species present. Each Gaussian is weighted by the mole fraction of the respective species, which is input by the student. A typical plot for this example is shown in Figure 3.

Figure 3. Simulated abwrption spectra of two-component equilibrium mixture. Mole fraction of component A [left-hand peak) varier from 0 to 1 in steps of 0.25.

Here, the user-defined function FNZ is used to evaluate Z = f(X,Y).= The scaled, integerized value (Zl) is then used in line 1060 to select one of N plot symbols. The step which gives the plot its "contour" character is line 1030, which suppresses the plot symbol if the function at that point differs by more than a specified amount (Nl) from the value of the nearest contour level. Figure 4 is a plot of the z = 0 section of the angular part of a 2p atomic orbital, produced by the above sequence.

Figvre 4. Contour plot of the z = 0 section of the angular port of o 2 p ~ hydrogenlike wavefunction. Zero-contour is plotted in " E r ; positive values ore indicated b y "F" through "I"; nogotire volver are plotted with "A" thmugh " D .

Contour Plots Angular Wovefunction

Non-Analytic and Non-Cartesian Functions

A contour plot of a function of two variables, z = f(x,y), involves a strategy similar to that used for the simple function y = f(x). The function is evaluated iteratively for successive values of x and y. The calculated value, z, will be indicated on the plot at the point (x,y) by an appropriately chosen plot symbol. Since y is now an independent variable and z is the dependent variable, we need the following additions and changes in the earlier scaliug/integerizing routines

NMR Spedrum

110 I N P U T YZ,Y3,Y1 115 INPUT 22.23 116 INPUT N.N1 140

LET

D=(Z3-22)/(N-1)

+ +

150 DEF FNI(Zl=(Z-ZZ)/D 1 160 DEF FNJ(Z)=INT((Z-ZO)/D 5)+1 9Q8 LET A$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Here, 22 and 23 are the minimum and maximum values of Z. N is the number of contour levels desired, and N1 is a tolerance value which determines the thickness of the plotted contours. The user-defined function FNJ converts the calculated Z into an integer

A more difficult plotting situation arises when we do not have points to plot for every value of the graphic abscissa (Class 11). A simulated nmr spectrum is such a plot.' In this kind of situation, all data points are calculated before any plotting is done, as opposed to the previous cases we have examined, in which the data points for a given data x are plotted before the next points are computed. Thus, for Class I1 plots, the data to be plotted will he generated and stored in a pair of arrays-X(1) and Y(1). For an nmr spectrum, the X(1) will contain the line positions (that is, the Although y is an independent variable in this case, it is still subject to the limitation of paper width. Therefore, care must be taken to choose Yl>(Y3-Y2)/70. For example, DEF FNZ(X)=X t 2-Y t 2 defines a hyperbolic paraboloid z = z2 - y'. No consideration of actual line shape is made here. Volume 49, Number 5, Moy 1972

/

359

transition energies calculated from the Hamiltonian eigenvalues), and the Y(1) will contain the corresponding line intensities. Since we will not have an X(1) corresponding to every value of graphic x, the program will need to scan the integerized X(1) for each successive value of graphic x to see whether there is an X(1) with that value. This is accomplished with the following steps 1000 FOR X=X9 TO X8 STEP -1

1010 PRINT ".": 1020 LET Y = O 1030 FOR I = 1 TO N 1040 IF X1I)ffX THEN 1060 1050 LET Y=Y+Y(I)T2 1060 NEXT I 1070 IF Y=O THEN 1110 1075 LET Y=INT(SQR(Y) 1110 PRINT 1120 NEXT X

+ .5)

Here, N is the number of nmr lines, and we assume that the X(1) and Y(1) have been scaled and integerized in a previous program step, as illustrated in earlier examples. Line 1010 prints the abscissa of the graph. An nmr spectrum is a somewhat peculiar plot in that left-to-right direction along the abscissa usually corresponds to decreasing abscissa values. Thus, line 1000 here is set up to scan in reverse from the upper limit to the lower limit, in contrast to previous examples. Another peculiarity is that,, within the resolution of the plot, two different transitions may correspond to the same graphic x. A simple, though arbitrary, solution is to let the plotted intensity in such a case be the square root of the sum of the squares of the overlapping intensities. Thus, line 1050 sums the squares of all intensities corresponding to a particular graphic x. When all the data x's have been scanned (lines 103W1060), we take the square root of the sum-ofsquares (line 1075) and plot the composite nmr line. Plotting is accomplished by insertion of the following lines 10% F O R I = l T O Y 1090 PRINT "a". 1100 NEXT I '

Because of the narrow linewidth, an nmr spectrum can be treated as a bar graph. Such a plot is most easily created by stepping across the teletype page with a FOR/NEXT loop, as in lines 1080-1100. A typical output is shown in Figure 5. Polor Coordinate Plot

tially single-valued; for each value of the graphic independent variable, such functions generate a t most one plotted value of the dependent variable. However, there are many functions of interest which are multivalued or appear multivalued when plotted on an inherently Cartesian medium such as a teletype. Examples of this type of graph are functions involving even powers of the dependent variable, plots of parametric equations, and polar coordinate plots of trigonometric functions. Let us consider the polar coordinate plot. Here, a set of data points arises from a function of the form r = f(0). To plot this on the teletype, we must first convert the (r,B) data to Cartesian form. This is easily accomplished 200 DEF FNX(R) =R*COSIT) 210 DEF FNYCR) =R*SIN(T) 220 FOR 230 LET 1 T9=T2 =0 TO T3 STEP TI 234 LET 240 250 260 270 280 290 300

TrZa3.14159*T9/360

eoolunle R =/(T) LET I=I+l LET X(I1 = F N X ( R I LET YII) =FNY(R) LET X(I)=FNI(X(I)) LET Yl1)-FNJ(Y(1)) NEXT T9 LET N = I

Here, R represents r, T9 represents B in degrees, and T is 0 in radians. T2 and T3, of course, are the lower and upper limits on 8 (degrees), and T I is the interval sise. FNX and FNY are user-defined functions for the transformation equations from polar to Cartesian coordinates s = r cos 8, y = r sin B FNI and FNJ perform the scaling- and integeriring as usual. The actual plotting again involves us in the situation where not every graphic x corresponds to a data x. Thus, we must scan the data x's for each successive value of the graphic abscissa, as was done for the nmr example. I n addition, there are likely to be more than one data y values corresponding to a given data x, since the function is probably multivalued in a Cartesian frame. Therefore, we need to collect all the y's corresponding to that particular z in order to build the output line. This may be done by translating each of these several y values, as i t is encountered, into an appropriately located plot symbol in a string variable, as in

All the functions dealt with so far have been essen-

. . Figure 5. Simulated nmr spectrum of acylonitrile, from literature shift and coupling constants Ill. Swoop is 300-400 Hz @ 60 MHz.

360

/

Journal of Chemical Educofion

;g ......................................... Figure 6.

Polar coordinate plot of r = sin 28.

This statement places the symbol "*" in the string position corresponding to the value Y(I).4 When all the y values for the particular graphic z have been found and marked in the string, the string can be PRINTed, thus producing a line of plot. The plotting operation would be structured as follows

previous line 300. A plot of the function r shown in Figure 6.

=

sin 2.4 is

Summary

The TAB and string or literal variable features of BASIC, combined with appropriate structuring of one's data, afford convenient means of producing plotted output on an ordinary teletype terminal. Such techniques are applicable to many examples of interest in chemical education. The requisite programming features are available on most popular time-sharing computers." Acknowledgment

The author wishes to thank Dr. James U. Piper of Simmons College for permission to include the nmr program in this discussion. Literature Cited

Lines 1030-1060 scan the data x's and build up the plot line as the string variable A$. Lines 108&1130 print the plot line. J1 and 52 are used, as in the radial wavefunction example, to save plotting time; and N, the number of points to be plotted, comes from the

(1) Wuxma. C. L., a m K m ~ ~ s n s ~ sC.~ E.. w . J. Cerx. Eouc., 43, 10 (1966). (2) WALTON.J. S.. AND RI~.N. WM. M.. JR., J. CBEM.EDOC.,46, 334 (1969). (3) The "Computer Instruction Iasue," J. Cnex. Eouc., 47, 87-166

(9)

(10)

In all of the foregoing examples, the a c t u d computational steps have been omitted for brevity. Full program listings are mailable from the author, except for the nmr program, which involves lengthy solution of the eigenvalue problem.

,. .-,. ,.O,",