Curve Sketching

See all articles from ZX-Appeal Jan 90
Author(s): Alvin Albrecht
Publication: ZX-Appeal
Pub Details: ZX-Appeal Jan 90
Date: January 1990
Pages: 9-11

Type in the listing (omitting rem statements and/or the instructions -line 5 and lines 1000 and up- if you want to shorten the typing job) and, if you have CK Type, do a CK type check with the CK check output following the program listing. If you deleted the rem statements and/or the instructions the lines that contained a rem statement will have different CK outputs.

Before you save this program you should know that there is a bug in it. I didn’t realize this until after it was printed and the bug was so easy to correct that instead of reprinting the entire listing, I am providing the changes:

130 GO TO 185
184 PLOT x/xscale+127,
y/yscale+87
255 IF PEEK 23739=11 THEN
IF PEEK 23736+256*PEEK
23737=125 THEN GO TO 185
257 IF PEEK 23739=11 THEN
GO TO 184
260 BEEP .005,20:IF PEEK
23739=10 THEN GO TO 185

Now save with: SAVE “F(X)SKETCH” LINE 1

Run the program and read the instructions provided (If you omitted the instructions when you typed in the program, read them from the program listing @ line 1000). The following are some examples to make you better acquainted with how to use Curve Sketcher.

Set Range=+/- 2, Domain=+/- 2*PI, Function: f(x)=SIN X (enter SIN as a keyword. ie- as you normally would from basic)

Next graph the function three times with steps=40,20, and 10 without clearing the screen.

The graphs should look inaccurate (mildly put) but there should be an improvement as the step is lowered.

Now, clear the screen, set step=3, and graph the function. This is what the graph of SIN x should look like. A step of 3 is ideal for this function because it is not too slow and it is accurate.

It makes sense that if the step is lowered further an even more accurate graph would be produced (at the expense of a longer time to graph it) and this is true, but we run into different problems. Clear the screen again and set step=0.5 and graph the function. A more accurate graph is produced but it looks fuzzy.

Suppose we want to know the roots of the equation f(x)=SIN x (ie-for what values of x SIN x=0). Set step=3, clear the screen and graph the function again. Choose the “Coords” option from the menu by pressing “O”. SIN x=0 when the graph crosses the x-axis. Use the arrow keys (5 to 8 will move one pixel, CAPS + 5 to 8 will move 8 pixels at a time) to position the flashing dot on one of the points where the graph crosses the axis. The coordinates of that point are given in the bottom left corner. One of the roots is the x coordinate. If you have a calculator handy you can check this by entering x and take the SIN of that (don’t forget the 2068 works in radians) and the answer should come up very close to zero. The slight inaccuracy is due to the fact that each pixel width on the screen represents several x-values. The range of x values is determined by the horizontal scale. You must also be careful in selecting a suitable domain and range. Clear the screen again and try graphing this:

Domain=+/- 5, Range=+/- 3, Step=3, Function:

f(x)=(x+1)/(x*x-x)

Remember what this looks like, clear the screen, and graph the function with one change: Range=+/- 7. Notice the extra part of the curve? It wasn’t plotted before because the section of graphpaper we used the first time had only the x-axis from -5 to +5 and the y-axis from +3 to -3. The extra parabolla’s vertex is at -5.2 on the y-axis. This feature can be used to get close-ups of sections of curves for detail. Did you notice that the hump on the leftmost part of the graph was harder to see on the second graph?

One last example: Range=+/- 2, Domain=+/- 2, Step=5 Function: f(x)=LN x. Clear the screen and graph this. Why is it beeping at you? It’s encountering plenty of mathematical errors. The LN function is undefined for negative numbers but the program is attempting to calculate LN x for -2<x<2 (the domain). The beeping is over fairly quickly and the graphing didn’t take that long, but for more complicated expressions the computer may be wasting its time calculating futilely the values of a function at undefined points and this will cause a considerable delay in the graphing. To avoid this problem, we change Lstart. Lstart tells the computer from what PIXEL to start graphing the function from. When Lstart=-127 (the default value) it starts graphing from the left edge of the screen. If we set Lstart=0 it will graph from the centre (origin) of the graph. So to only graph the function for positive values of x, set Lstart=0. Do this, clear the screen, regraph the function, and verify that there was no beeping.

What if we want to start the graphing at a specific VALUE OF X. eg- we want to see the graph of LN x only where x>0.73. Set Lstart=0.73/xscale. Easy enough. (Try it if you like after clearing the screen and use Coords to verify the first point plotted has an x coordinate of close to 0.73).

That about wraps up my narrative. Hope you enjoy it.

Screen of f(x)=1/(x^3-4*x) graphed by the program, with the MENU line "(S)ee Parameters"
Screen of f(x)=1/(x^3-4*x) graphed by the program, with the MENU line "(S)ee Parameters"
   1 REM CURVE SKETCHING                 Dec 1989 Albrecht
2 POKE 23658,8
3 DEF FN r(x)=INT (x*1000)/1000
5 GO SUB 1000: REM instr
10 LET xscale=1: LET yscale=1: LET z=1: LET a$="x": LET lstart=-127
15 GO SUB 500: REM Draw Axes
20 GO TO 400: REM Menu
98 REM
99 REM Draw Graph
100 REM
101 ON ERR GO TO 250
102 PRINT #0;AT 0,0;"One x=";FN r(10*xscale),"One y=";FN r(10*yscale),
105 LET plot=0: LET xlast=PI: LET ylast=PI
106 FOR l=lstart TO 127 STEP z
107 LET x=l*xscale
110 PRINT #0;AT 1,0;"WORKING @ ";FN r(x),
112 LET y=VAL a$
115 IF ABS (y/yscale)<=87 THEN GO TO 150
119 REM
120 REM Not OK to plot
121 REM
125 IF plot THEN LET plot=NOT plot: DRAW (x-xlast)/xscale,(y-ylast)/yscale
130 GO TO 180
149 REM
150 REM OK to plot
151 REM
155 IF plot THEN DRAW (x-xlast)/xscale,(y-ylast)/yscale
157 IF NOT plot AND xlast=PI THEN LET plot=NOT plot: PLOT x/xscale+127,y/yscale+87
160 IF NOT plot AND xlast<>PI THEN LET plot=NOT plot: PLOT x/xscale+127,y/yscale+87: DRAW (xlast-x)/xscale,(ylast-y)/yscale
179 REM
180 REM Continue
181 REM
185 LET xlast=x: LET ylast=y: NEXT l
190 ON ERR RESET : RETURN
249 REM
250 REM Non-permissable value found in calculations or out of bounds in plotting errors
251 REM
255 IF PEEK 23739=11 THEN IF PEEK 23736+256*PEEK 23737=160 THEN PLOT x/xscale+127,y/yscale+87
257 IF PEEK 23739=11 THEN GO TO 180
260 BEEP .005,20: IF PEEK 23739=10 THEN NEXT l: GO TO 190
265 IF PEEK 23739=21 THEN PAUSE 0: GO TO 190
 270 LET x=x-xscale: GO TO 112
399 REM
400 REM Menu
401 REM
405 LET b$="MENU (S)ee Parameters -->"
410 GO SUB 450: GO TO 410+5*(g$=CHR$ 13)+190*(g$="S")
415 LET b$="Change: (D)omain (R)ange (S)tep (L)start (F)unction -->"
417 GO SUB 450: GO TO 417+283*(g$="D")+313*(g$="L")+333*(g$="R")+383*(g$="S")+433*(g$="F")+8*(g$=CHR$ 13)
425 LET b$="(C)lear Screen C(O)ords (G)raph Function -->"
427 GO SUB 450: GO TO 427+523*(g$="C")+543*(g$="G")+443*(g$="O")-22*(g$=CHR$ 13)
449 REM
450 REM Wait for key
451 REM
455 PRINT #0;AT 0,0;b$
460 IF INKEY$<>"" THEN GO TO 460
465 LET g$=INKEY$: IF g$="" THEN GO TO 465
470 RETURN
499 REM
500 REM Draw Axes and Label Scale
501 REM
505 CLS : PLOT 0,87: DRAW 255,0: PLOT 127,0: DRAW 0,175
510 FOR l=7 TO 247 STEP 10: FOR m=86 TO 88: PLOT l,m: NEXT m: NEXT l: FOR l=7 TO 167 STEP 10: FOR m=126 TO 128: PLOT m,l: NEXT m: NEXT l
515 RETURN
598 REM
599 REM View Parameters
600 REM
605 PRINT #0;AT 0,0,,,,: LET b$="PARAMETERS Domain=-"+STR$ (FN r(xscale*127))+"<x<"+STR$ (FN r(xscale*127))
610 GO SUB 450: IF g$<>CHR$ 13 THEN GO TO 610
612 PRINT #0;AT 0,0,,,,: LET b$="Range=-"+STR$ (FN r(yscale*87))+"<y<"+STR$ (FN r(yscale*87))+CHR$ 23+CHR$ 0+" Step="+STR$ (FN r(z))+CHR$ 23+CHR$ 16+" Lstart="+STR$ FN r(lstart)
615 GO SUB 450: IF g$<>CHR$ 13 THEN GO TO 615
620 PRINT #0;AT 0,0,,,,: LET b$="f(x)="+a$
625 GO SUB 450: IF g$<>CHR$ 13 THEN GO TO 625
630 GO TO 400
697 REM
698 REM Enter New Domain
699 REM

Continued next issue.

The printed line-length table (with the continuation) gives more bytes than show in print for lines 1, 405, 605, 730, 870 and 1000, probably invisible colour or control codes.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.