Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 11 / NOVEMBER 1983 / PAGE 264

Easy plotting on the TRS-80 Model 100 and NEC 8201. David H. Ahl.

Do you want a simple program to show off your new TRS-80 Model 100 or NEC PC-8201? Would you like to learn easily how pixel addressing works? Or would you like to explore some mathematical functions without doing any calculations and hand work?

If you answered "yes" to any of these questions, then key in this short 27-line program on your Model 100 or 8201 and have some fun while you learn. Plot Any Function

Programs to plot any function have been around for 15 years or so, but many of them are oriented to hard copy listings in which the x-axis runs in the vertical direction of the paper feed on the printer. For computers without the ability to address individual screen locations easily, these programs are still quite useful.

However, when a computer offers pixel addressing by means of a PSET, LOCATE, or PLOT COMMAND, it seems that one ought to take advantage of this capability and orient plots so that the x-axis is horizontal.

This program is one of 50 programs that appear in my new series of Ideabooks for seven different computers. It started off as a conversion of a plot-any-function program that I wrote in 1971, but wound up bearing very little resemblance to the original. Fitting The Plot On The Screen

The screen on the Model 100 and 8201 is 240 pixels in width by 64 pixels high (see Figure 1). The standard computer convention for numbering the pixels in the vertical direction is from the top to the bottom, just the opposite of what you learned in algebra and geometry. Hence, we are faced with two problems in plotting a function: scaling it to fit on the screen (240 X 64) and orienting it "correctly" (bottom to top).

Since the x-direction has far more pixels than the y-direction, 240 vs. 64, it seems sensible to plot every point in the x-direction and let the y points fall where they may. As we will see later, this is not always satisfactory, but for the most part this approach produces acceptable results.

Although we will have the computer do some of the scaling, we will allow the user to specify upper and lower limits for the values of x and y (inputs in lines 30 and 40). Scaling in the x-direction takes place in ines 50 and 60. DX is the distance between the upper limit (XU) and lower limit (XL) of x values. This distance, DX, is then divided by the 240 horizontal pixels to produce a horizontal scaling factor which is also used as a horizontal step (SP).

This horizontal step, SP, when used in the FOR loop starting in line 100, produces 240 equal steps between the lower and upper limits of x.

A similar scaling factor for the y-direction (SY) is computed in lines 70 and 80.

Line 90 simply clears the screen and causes the label line at the bottom to disappear. We are then ready to enter the function plot loop between lines 100 and 160.

Each x-value to be plotted (XP) is computed in line 110. If all our plots started with a lower limit of 0, the plotted x-value would be simply the actual x-value divided by the scaling factor. However, to allow for plots that start above or below 0, the lower limit of x (XL) must be subtracted from the actual x-value and the result divided by the scaling factor.

The function we wish to plot is stated in line 120 in the form, y equals any function of x. Several examples are given later.

In line 130, the y-value to be plotted (YP) is calculated. To compensate for the computer screen pixels being numbered from top to bottom instead of bottom to top, the computed value is subtracted from 64. this effectively inverts the plot.

The PSET (XP,YP) statement in line 150 turns on the LCD pixel at x-coordinate XP and y-coordinate YP. Since we allow the user to specify the upper and lower limits of both x and y, it may happen that one or more y-values are outside of the dimensions of the screen. The x-values, of course, cannot be off the screen since we are using XL and XU as the right and left edges of the plot.

Y-values that exceed the screen dimensions by a small amount should be plotted as the top or bottom line of the screen instead of having the program halt with an error message. Hence, line 140 converts negative plot values (above the top of the screen) into a zero (top row of pixels). In the positive (lower) direction, the Basic language is somewhat forgiving and accepts y-values up to 255, but plots them as though they were 63 (bottom row of pixels).

Line 170 tests whether the y-axis is on the screen by checking if the lower and upper limits of x are the same sign. If the axis is on the screen, line 180 determines where it is; it is then plotted in lines 190 to 210. The same calculations and plot are performed for the x-axis in lines 220 to 260.

Line 270 simply keeps the program in a loop so the plot stays on the screen until the program is interrupted by pressing the BREAK key on the Model 100 or the STOP key on the NEC 8201. What To Plot?

This program will plot any function that can be specified in terms that can be interpreted by the Basic language. In the program, this is done in line 120. Although relatively few functions are directly implemented in Basic, virtually any function can be represented by combinations of the Basic functions. For example, if you are interested in inverse hyperbolic functions, you will have to translate them into functions which are implemented in the Basic language.

This, incidentally, is not as difficult as it may sound. Many reference books and computer manuals have tables of derived functions. For example,

Some of these derived functions are quite intriguing and may pique your curiosity. For example, try plotting some Bessel functions or Laplace transforms.

On the other hand, you can learn a great deal from experimenting with other, straightforward functions, while producing some fascinating displays. Here are some functions that work well:

For best results, the limits of trigonometric functions are generally multiples of pi (3.14159). Limits for other functions are best found by experimentation.

This list of functions barely scratches the surface of possibilities. You will find that some functions produce problems in the program. For example, a function that has x in the denominator will cause a divide-by-zero error if x is zero. Why, then, doesn't the last equation fail since x passes through zero? Because the way the steps occur, the expression is evaluated just below and just above zero; with other limits it might possibly blow up.

The second suggested set of limits for the sine curve (0 and 314) produces an interesting plot. Since 314 is more than the number of horizontal divisions (240), the plot "misses" many critical values. As a result, the ten normal sine curves that should appear between 0 and 314 (pi times ten), appear to be ten interlocking and overlapping sine waves, each one exactly 2-1/2 normal periods in length. Can you explain this phenomenon? Incidentally, the plot can be improved slightly by making the lower and upper y limits, -- 1.1 and 1.1. You will find this is often true, i.e., slight variations in the limits of x and y produce more (or less) pleasing plots.

This type of curve, in which a great deal is happening in the y direction relative to the x direction, illustrates the problem mentioned earlier. You will recall that we said that, "since the x-direction has far more pixels than the y-direction, 240 vs. 64, it seems sensible to plot every point in the x-direction and let the y points fall where they may." It should now be obvious that this may not always produce the best possible plot because of many missing vertical points. For most plots, however, we feel justified in sticking with our first decision of plotting 240 horizontal points. Making All Plots Fit

It is possible to add a routine to the program to make all plots fit within the y screen dimensions. First, we must eliminate line 40 asking for the upper and lower limits of y. Instead, we must write a routine to insert between lines 60 and 70 to evaluate y over the entire range of values for x, saving the maximum and minimum values. These two values then become the lower limit (YL) and upper limit (YU) of y. We leave it to the reader to write this routine. Hint: it can be done in five lines.

This program can be "cleaned up" in other ways. Nevertheless, in its present form, you should find it educational, interesting, and entertaining.