Plot a Function Graph

Plot a Function y = f (x)

In most cases, we can get a program which plots a function by modifying the lines from 100 to 140 in the following.
The function is given at the DEF statement in line 100.
The range of the x-coordinates are specify by "left" and "right" in lines 110 and 120. and the y-coordinates "bottom" and "top" in lines 130 and 140. As the scales of x-direction and y-direction can be different in a function graph, they can be specified independently.

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.
The number of apertures between left and right is p-1. Thus the increment (right-left)/(p-1) in line 180 is optimum.
Usually the feature of the function that we want to plot is doubted, because, if not, we don't want to do so. We must make a program in the presupposition that the calculation result shall not be known until the calculation is really executed. That is to say, programs that plot functions need exception handling.
For example 1, if a calculation error occurs in line 200, the control shall break the execution of that line, execute the USE section and then terminate the execution of the WHEN EXECPTION IN ~ END WHEN block. PLOT LINES in line 220 has the effect of unconnecting the lines.

When we plot a function whose change in values is violent, we need to increase the increment in the FOR loop.
Example 2. Plot y =sin(1/x)

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.
For example, if we replace lines form 100 to 140 of example 1 by follows to plot a tangent function, we get the figure right.

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.
PLOT POINTS draws a mark at the point. We need SET POINT STYLE 1 as in line 160 to change the point style to a dot.
Example 3. Plot a tangent function.

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.
Exception handling becomes simple as above because nothing have to be done when an exception has occurred.

If the function to be plotted is trigonometric, we can sometimes avoid a worry by changing the unit of angle measure to degrees.
Full BASIC surely raises an exception for TAN(-90) or TAN(90) in degree measure. Thus we design so that the variable x should have these values by adjusting the increment of the FOR loop.
Example 4. Plot a tangent function.

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


Plot the curve of an equation f (x, y)=0

It is not so easy to plot the curve of an equation of f (x, y)=0 in BASIC.
It is recommended that we change it into the form y=f (x) or a parametric equation and then plot it. But if we cannot change the equation into such a form, we use a method as follows.
Example 5. Plot a curve of an equation f (x, y)=0.

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.


Example 5 assumes the function to f (x, y) be continuous. When a function is not continuous, a means that removes discontinuous points is needed.
Example 6.

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


Back