|★ APPLICATIONS ★ DIVERS ★ GRAPH PLOTTER|The Amstrad User) ★|
|Graph Plotter||Applications Divers|
If maths was not your forte at school, you may be inclined to mutter somewhat unkindly about that heading and who is to blame you. Nonetheless, fun can be had with mathematics. That is the theme of the book, "Fun Mathematics on your Microcomputer", by Czes Kosniowski (Cambridge University Press, Cambridge, 1983, ISBN 0 521 27451 6). I bought the book a while back and while delving into its pages came upon the author's program for plotting graphs. To quote the author: "The BASIC on computers varies from one machine to another and writing a general program is quite hard. When it comes to a graphics program the situation is almost impossible." He therefore presents three versions. One for computers which understand an implementation of Microsoft BASIC (with hints for conversion), one for the Vic 20 and one for the Sinclair Spectrum. "Well", thought I, "I can get that up and running on my Amstrad" and embarked on the project It did not take long to realise that the program was, of necessity, written to suit the lowest common denominator (and very common it seems to be too!) A few pages on and he was at it again, writing programs to plot polar graphs. It soon occurred to me that one program could be written to plot both types of programs, using the CPC464's superior graphics and BASIC. From that moment on the program took on a life of its own, but I must thank Kosniowski for the inspiration. DESCARTES VS THE POLE No it's not the bill for World Championship Wrestling! There are two ways of plotting a graph. The first, attributed to Descartes, uses a horizontal and a vertical axis. Any point on a flat surface (plane) can be described in terms of measurements along these axes with just a pair of numbers (x,y), x is measured from left to right along the horizontal axis and y is measured vertically upwards. Negative numbers are measured in the opposite direction. The pair (x,y) are called co-ordinates, or cartesian co-ordinates.
The second method of locating a point in our plane is by means of polar co-ordinates. Here we have only one axis, with a point on it called the pole. Our point is now represented by a pair of numbers (r,z), where r is the distance to the pole and z is the angle, measured anticlockwise, between the axis and a line from the pole to this point. Confused? Good, because there is a little more to come when we get to plotting our graphs.
I certainly hope it does! Actually the word, function, is used to describe expressions, such as y-x+1, y=sin(x), and r-sin(z), and we say that y is a function of x. In Locomotive BASIC there is a method of DEfining a FuNction for later use in a program.
A function which involves polar coordinates is called a polar function (and you thought it was a mid-winter celebration in Antarctica!)
The interesting part of functions is not all the boring theory, which I have kept as short as possible, but the graphs that we can draw from them. A simple way to see the difference between cartesian and polar co-ordinates is to plot the graphs of the similar functions, x-1 and r-1. The first produces a straight line parallel to the x axis, but one unit (measured on the y axis) above it. The axis itself is x=0. Ah, but r=l produces a circle!
To add even further convolutions to our polar graphs, we can multiply the angle z by a factor, say d, and I have made provision in the program to do just that.
A couple of things that microcomputers do not like when it comes to calculating the value of functions are division by zero and very large numbers. In the program I have tried to circumvent Arnold's protests by the use of a number of statements to which the program is diverted by ON ERROR GOTO. If you have the misfortune to discover a circumstance I missed, add another suitable statement after line 2350 and let us all know what was required, please!
There is one "bug" in the program. In graphs of functions like l/xA2 the graph disappears into the wild blue yonder and then re-emerges almost directly opposite. What is happening, of course, is that division by zero is approached, giving the function a value too large to be plotted. After the point of division by zero is passed, the value of the expression, whilst still large, becomes opposite in sign. The vertical line at the point of division by zero, which the graph approaches but never touches within a finite distance, is called an asymptote and Arnold cheerfully draws it in, if you use the D option. In any one graph, it is quite easy to write a statement to prevent this. For example:
175 IF abs(v)>=250 THEN PLOT u,v, 1 :goto 200
will prevent it happening in equation 1. However, the dysfunctional consequence (I spent years studying the jargon - I've got to use it on someone!) is that other graphs will be interfered with. If anyone comes up with a general expression that appears to work in all cases, please let me know! Of course the "if" statement in line 175 could be altered so that it only applies to equation 1, but what happens then if I make equation 5: l/(x^3-l>? HOW IT WORKS Listing 1 is a loader screen, which not only gives you something to look at while the main program (listing 2) is loading, but also shows off just a couple of the CPC464's graphic features. These are the ability to change the colours on the screen and the ability to mix text and graphics by printing text at the graphic cursor position. LINES 250-310: Initialise the screen setting all four inks to black and defining the windows used. LINES 350-390: Set the transparent mode and origin for the text "plotting". LINES 40-170: Complete the screen and turn off the transparent mode. LINE 210: Loads and runs the main program. Main Program
LINES 2310-2340: Error handling. LINE 60: Call &BB4E initialises the text VDU in the same way as a "cold start". This clears the loader screen when first encountered and the graph drawing screen when you elect to have "Another Go" (line 1590 on). Call &BB03 resets and clears the keyboard buffer.
LINES 1700-1730: Initialise the first screen, setting paper and pen to blue and assigning values to key variables. LINES 1770-1960: The first screen displays a brief explanation of the program and invites the user to choose mode and type of graph. Note line 1960, which ensures that the keyboard buffer is clear before proceeding, could be replaced with call &BB03. LINE 80: Branches to the information screen for the chosen type of graph. LINES 590-770: The second screen lists the 8 graphs which can be plotted. If amending the program to include your own choice of functions, substitute your choice for one of the 8. LINES 780-790: These subroutines enable the user to select one of the functions, assess the appropriate scale for the screen plot, plot the graph and finally invite the user to have another go.
LINES 830-970: The third screen performs the equivalent functions for polar graphs. This time there are 7 choices.
LINES 980-990: These subroutines enable the user to make a choice not only of function, but also the five constants a,b,c,d and e, which are added into and/or multiplied by the elements of the various functions and, as mentioned, used to vary the angle z. By varying these, equation 2's simple circle becomes a familiar logo. (Try a=0, b=0, c=0, d=1 and e=3). They also assess the correct scale, plot the graph and invite the user to continue. Other lines of interest are lines 1080 and 1220 which select the appropriate definition for the function from lines 1360-1570. The latter are where you make your other sustitution if using the program to plot a graph of your own choice.
With the polar graphs you have a choice of plotting or drawing (lines 2220 on), but for the other graphs you have a third choice, that of shading in the area below the graph (lines 2090 on).
i: Control variable in the for.... next loop.
r,z,x,xx,y,yy,u,v: The first six are used in the functions from which the graphs are plotted, whilst u and v are used for the points to be plotted.
a,b: Used firstly as the lower and upper limits of the range which is to be plotted for ordinary graphs. For polar graphs a,b,c,d and e are used as explained earlier.
st: Step variable in for...... nex loops. dgs: Number of degrees being plotted in polar graphs.
p$: Reqtrred option from (p)lot, (d)raw or s(hade).
The program will plot any range of values of x in the ordinary graphs. Try -50 to 50, -10 to 10, -2 to 2 and -1 to 1. If any of these ranges give a hint of an interesting area of the graph, zero in on it, by choosing the appropriate range.
With the polar graphs an almost infinite variety of patterns can be created. Try the so-called standard plot. Then try a=l, b=l, c=l, d=l and e=l. (Why doesn't that give a graph in the case of function 7? If you apply a little algebra, you will see that the variable, z, is eliminated, leaving nothing to graph!). A third choice is a=2, b=2, c=3, d=4 and e=l and finally try a=2, b=2, c=3.1, d=3 and e=3. By the time you have tried all of those, you will have an idea of the effect of changing these numbers. Then do some experimenting.
Use AUTO when typing the program in. To make it more compact, type instead of the REM and ' lines. These lines will then be omitted, eliminating more than 50 lines. Also omit the spaces used to indent wrap-around lines. If the graph seems to get "stuck", a touch of the ESC key will jump you forward to the "Another Go?" option. Have fun!