The basic way to draw a graph of a function is by
drawing it as a sequence of small
lines between (x, f(x)) and (x+dx, f(x+dx)).
There are several things that make this graph not quite ordinary.
The function 1/x is singular at 0, so you cannot just graph
an entire interval
through 0, and must plot left and right separately.
But then the graph oscillates at a higher and higher frequency
as it gets close to x=0,
with period decreasing below any fixed size. This requires that
the interval dx decrease as the graph gets closer to 0.
Draw the graph of y = sin (1/x)
for |x| < 1.
Color blue the regions inside the graph
where y is positive, and
red those where it is negative.
How should it be chosen? Roughly speaking, the number of
lines in each loop of the graph should remain constant,
say N = 100. The graph crosses
the x-axis at the points 1/ π n, so one loop
lasts between 1/ π n and 1/ π (n+1).
What the program belwo does is plot each loop separately.
solution would be to set dx = x2/N, since
1/ π n - 1/ π (n+1) = 1/ π n(n+1) which is roughly proportional
to x2 if x = 1/ π n. (This is not what I thought
originally.) Doing this makes it easy to color alternate loops
in different colors.