In most cases, we can get a program which plots a function by modifying the lines from 100 to 140 in the following.
Example 1. Plot y=(1+1/x)x 100 DEF f(x)=(1+1/x)^x 110 LET left=-5 120 LET right=5 130 LET bottom=-1 140 LET top=9 150 SET WINDOW left, right, bottom, top 160 ASK PIXEL SIZE (left, bottom; right, top) p, q 170 DRAW axes 180 FOR x=left TO right STEP (right-left)/(p-1) 190 WHEN EXCEPTION IN 200 PLOT LINES: x, f(x); 210 USE 220 PLOT LINES 230 END WHEN 240 NEXT x 250 END
ASK PIXEL SIZE finds the numbers of pixels in the rectangle specified by the coordinates of diagonal vertices. |
When we plot a function whose change in values is violent, we need to increase the increment in the FOR loop. 100 DEF f(x)=SIN(1/x) 110 LET left=-4 120 LET right=4 130 LET bottom=-4 140 LET top=4 150 SET WINDOW left, right, bottom, top 170 DRAW axes 180 FOR x=left TO right STEP (right-left)/5000 190 WHEN EXCEPTION IN 200 PLOT LINES: x, f(x); 210 USE 220 PLOT LINES 230 END WHEN 240 NEXT x 250 END
|
Sometimes techniques in Examples 1 or 2 do not work. 100 DEF f(x)=TAN(x) 110 LET left=-5 120 LET right=5 130 LET bottom=-5 140 LET top=5
This is because the tangent function gets the value for which calculation does not occurs an overflow error but that is huge in absolute on the neighborhood of π/2 or the values on which the tangent is not defined. |
In these cases, giving up plotting with connected lines, we make a program that plot many points. 100 DEF f(x)=TAN(x) 110 LET left=-5 120 LET right=5 130 LET bottom=-5 140 LET top=5 150 SET WINDOW left, right, bottom, top 160 SET POINT STYLE 1 170 DRAW axes 180 FOR x=left TO right STEP (right-left)/5000 190 WHEN EXCEPTION IN 200 PLOT POINTS: x, f(x) 210 USE 230 END WHEN 240 NEXT x 250 END Then we gets the graph as in the right. |
If the function to be plotted is trigonometric, we can sometimes avoid a worry by changing the unit of angle measure to degrees. 100 OPTION ANGLE DEGREES 110 DEF f(x)=TAN(x) 120 LET left=-320 130 LET right=320 140 LET bottom=-5 150 LET top=5 160 SET WINDOW left, right, bottom, top 170 DRAW axes(90,1) 180 FOR x=left TO right 190 WHEN EXCEPTION IN 200 PLOT LINES: x, f(x); 210 USE 220 PLOT LINES 230 END WHEN 240 NEXT x 250 END
|
It is not so easy to plot the curve of an equation of f (x, y)=0 in BASIC. 100 DEF f(x,y)=x^2+y^2 - 3*x*y +1 110 LET left=-4 120 LET right=4 130 LET bottom=-4 140 LET top=4 150 SET POINT STYLE 1 160 SET WINDOW left,right,bottom,top 170 DRAW axes 180 FOR y=bottom TO top STEP (top-bottom)/2000 190 LET x=left 200 LET z0=f(x,y) 210 FOR x=left TO right STEP (right-left)/2000 220 LET z=f(x,y) 230 IF z0*z<0 THEN PLOT POINTS: x,y 240 LET z0=z 250 NEXT x 260 NEXT y 270 END
We fix the value of y . Move x from the left to the right a little bit at a time, when the sign of f (x, y) changes, the point so that f (x, y)=0 is to be located nearby, then we plot the point. |
100 DEF f(x,y)=(1/(x-y))-x+3*y 110 LET left=-5 120 LET right=5 130 LET bottom=-5 140 LET top=5 150 SET POINT STYLE 1 160 SET WINDOW left,right,bottom,top 170 DRAW axes 180 FOR y=bottom TO top STEP (top-bottom)/2000 190 LET x=left 200 WHEN EXCEPTION IN 210 LET z0=f(x,y) 220 USE 230 LET z0=1 240 END WHEN 250 FOR x=left TO right STEP (right-left)/2000 260 WHEN EXCEPTION IN 270 LET z=f(x,y) 280 IF ABS(z0)<1 AND ABS(z)<1 AND z0*z<0 THEN PLOT POINTS: x,y 290 LET z0=z 300 USE 310 LET z0=1 320 END WHEN 330 NEXT x 340 NEXT y 350 END |