Classic Computer Magazine Archive CREATIVE COMPUTING VOL. 9, NO. 1 / JANUARY 1983 / PAGE 202

Three ways of looking at a function. (computer graphics) James Fuller.

Three Ways of Looking At A Function

Anyone with scientific or technical interests needs to have an intuitive feeling for the behavior of different kinds of mathematical functions, and the typical shapes of their plots. This is one of the barriers which keep many people from an enjoyment of science, preventing them from taking pleasure in what is, when you think about it, the art of our time.

It used to be that the only way to acquire such an easy familiarity was to sit down with a pencil and graph paper, heave a sigh, and start plotting points.

Even with a calculator this is slave labor. So while most people (the ones who paid attention in high school, anyway) can recognize simple linear, exponential, and quadratic plots and the garden-variety trigonometric functions, if they encounter an unlovely object such as

f (X) = cos(4X) + (20/(X2 + 3))

it's back to graph paper.

For anyone with a personal computer, however, this is no longer true. The computer will take over the dull, repetitive task of plotting points, and the user is free to pay attention only to what the function actually does. He is also encouraged to experiment with the effects of making minor or major changes in the formula, and to plot it in different coordinate systems.

The Applesoft Basic program in Listing 1 can create three kinds of high-resolution plot of a given function. It can plot points in Cartesian or polar coordinates, or it can create a solid of revolution in three simulated dimensions.

Cartesian coordinates need no introduction and little discussion. They are used daily to plot everything from the third world birthrate to the speed with which Brand X aspirin enters the bloodstream.

When Descartes warped the future of Western thought by inventing the graph, this is what he invented. In this system a point is uniquely located in two dimensions by giving its coordinates on a horizontal and a vertical axis.

There is another way to locate a point uniquely in a plane, though this method is not in such common use. In the polar coordinate system we draw a line between the point of origin of the system and some other point of interest, and specify where that point is by giving its distance from the origin and the angle the line forms with the horizontal axis. Thus point p in Figure 1 can be located either by giving values for X and Y or by giving length r and angle (theta). The translation between the two systems involves the elementary trigonometric relations

sin = Y/r and cos = X/r

or, rearranged,

X = r(cos ) and Y = r(sin )

Polar coordinates are especially appropriate for graphing periodic functions such as sines and cosines. Complex combinations of simple periodic functions can result in some remarkably beautiful plots. Figure 2 polar plot of

f(X) = cos (7* sin (2x))

A solid of revolution is created by rotating a two-dimensional figure about an axis to create a three-dimensional surface. A circle rotated about a line passing through its center creates a sphere, a right triangle rotated about one of its legs creates a cone, and so forth.

The program presented here takes a function plotted in Cartesian coordinates and creates a solid of revolution by rotating it about the Y-axis. The program then displays the result on the screen in a way that gives a convincing illusion of a three-dimensional object. The viewer seems to be looking at it from above and somewhat to one side. Portions which he would not be able to see from this vantage point are eliminated.

Running The Program

To run the program, the user must first translate the function he wants to examine into Basic and type it into line 170:

170 DEF FN R(Q) = (user's formula, in terms of Q) The plotting routines can call it from this point whenever it is needed.

After the equation is entered and the program is started, a self-explanatory series of prompts appears, asking the user to choose a Cartesian, polar, or three-dimensional plot. If one of the first two is chosen, further questions appear asking about the function increment (how much distance between plotted points), whether the axes should be expanded or compressed, and whether the origin should be moved.

It is worth mentioning that whole number increments are not always best. For example, the function f(X) = 1/(X-5) will give a divide-by-zero error where X = 5 if increment 1 is chosen. If increment 1.1 is chosen instead, X never lands precisely on 5 and the error is avoided.

Other prompts appear if the user asks for a three-dimensional plot. These plots have a habit of running off the top or bottom of the screen. If the user finds this is happening, he can enter a value which moves he plot up or down the screen, and another which expands or compresses it in the vertical direction.

Once all the questions are answered, the program assumes control and produces the desired plot in accordance with the instructions the user has given it. Figures 3, 4, and 5 show the three types of plots the program can create of the function

f(X) = cos(X) + cos(2X) + cos(5X).

Altering The Program

The program has another use besides examining the graphs of complicated equations. It can also create some very elegant graphics, which may be appreciated for their own sake. The slight alterations

690 FOR G = 0 to 36000 STEP I DEL 750, 760



will cause the computer to draw a line between each two consecutive points plotted in polar coordinates. This will have little effect if the points are close together (that is, if the function increment is small.) On the other hand, large increments often produce remarkable results. With an increment of 1, the function f(X)=X/ (one version of Archimedes' spiral) produces the plot shown in Figure 6. The plots in Figures 7, 8, and 9 were produced by increments of 122, 145, and 88.5 respectively.

As the program is printed here, the equation f(X)= .25-sin (2X) gives the plot shown in Figure 10 (increment = 1, scale X = scale Y = 6). If line 660 is changed to read FOR G = 0 TO 3960 STEP I and the increment is set at 55, the result (Figure 11) resembles the calcium skeleton of some delicate, microscopic sea creature. The effect is particularly striking on a color monitor or TV.

Many other equations yield similarly beautiful results. (Try equations like f(X) = .5--sin(X), with G stepping to 10700 by increment 143, or f(X) = cos(4X), with G stepping to 1800 by increment 25.) The knowledge that such rewards are waiting to be stumbled upon is enough to encourage endless experimentation.

The 3-D Plotting Module

The 3-D plotting module of the program also yields graphic rewards to the persistent heaper-upper of equation factors. Pleasing examples are shown in Figure 12.

f(X)= .5(16 - X2) + 1/(X + 4)

and Figure 13

f(X) = cos(4X) + (20/(X2 + 3))

--the ugly-duckling equation with which we began.

In addition, "wrong' variables can be pulled out of their proper positions and plugged in to produce plots which clearly are not solids of revolution. Figure 14,

f(X) = cos(2X) + cos((X + BB)/16)

is such a plot.

The 3-D plotting module is slower than the others, simply because of the very much greater number of points which must be plotted to give a finished display. If the user is just searching for neat graphics, a cruder but faster trial version can be created by changing line 1430 to read FOR BB = -AA TO AA STEP 5.

The two short programs is Listings 2 and 3 are provided so that interesting plots can be saved and retrieved later rather than having to be recreated from scratch every time. After a plot is complete and the program has ended, clear the screen by typing TEXT and a carriage return. (Don't be alarmed--the picture has not been destroyed. It is still in memory although it no longer appears on the screen.) Then load and run the program SAVE PICTURE, which will save the plot on disk as a binary file under a name of your choice. The program DISPLAY PICTURE, similarly, will retrieve a saved plot from the disk and display it on the monitor screen.

The program needs only a few detailed comments. For the polar plot routine, the value G in line 690 is ; r may be found using the Pythagorean theorem by taking X and Y, defined in lines 710 and 720, and setting r = X2 + Y2. The result will be that r = f(T) = f(G/57.296), where f is the function the user has defined. (Note that T is in radian measure; this is accomplished in line 700. The Apple and many other small computers do their trigonometry in radians, not degrees.)

For the Cartesian plot module, the values of X and Y are found in lines 910 and 920. Other manipulations merely squeeze these values onto the Apple screen. "Invisible' points in three-dimensional plots are removed by lines 1610-1650, which change the plotting color to black, drop a black line from each plotted point to the bottom of the screen (thus erasing any previously plotted white points directly beneath it), and restore the plotting color to white again.

The program has been written for legibility and clarity, at a slight sacrifice in speed. Most lines contain only one

The program could be developed in several ways. It would be interesting and not difficult to include scaling routines which would allow plotting on logarithmic and semilog axes as well as on linear ones.

Using shape-table methods, a graphic character set could be defined and used to label the axes.

Finally, I have seen published explanations of ways to enter a function from the keyboard in response to a prompt, while the program is running--something Applesoft does not ordinarily allow. Inclusion of such a routine would make it unnecessary to change line 170 whenever you want to plot a new function.

Photo: Figure 1.

Photo: Figure 2.

Photo: Figure 3.

Photo: Figure 4.

Photo: Figure 5.

Photo: Figure 6.

Photo: Firuge 7.

Photo: Figure 8.

Photo: Figure 9.

Photo: Figure 10.

Photo: Figure 11.

Photo: Figure 12.

Photo: Figure 13.

Photo: Figure 14. program command. The FOR-NEXT loops which do the actual plotting have been indented to make their operation as understandable as possible, and to make it easy to translate them into other flavors of Basic. Of course, if you have an Applesoft compiler, you can compile the program into machine language, which will run much faster.