Chaos Theory: Iterative Sine Wave Generator

This program is written in Basic. Cut and paste it into a Basic 'workbench' such as Microsoft QuickBasic. Then compile it and run it. It generates the sine wave by iterating a simple difference equation. It displays the sine wave, as it is being generated, both as an amplitude-time graph and as a phase diagram. [Português] [PDF]

'SINE-WAVE DIFFERENCE FUNCTION
screen 9,3
color 7,1
th = 3                     'text horizontal bias
s = 1.5                    'horizontal scaling factor
n = 150                    'integral extent of the axes
h = 230                    'horizontal zero bias
v = 330                    'vertical zero bias
nn = 550                   'extent of time-series horizontal axis
hh = 25                    'time-series horizontal bias
vv = 160                   'time-series vertical bias
ss = 200                   'inter-iteration delay

'DISPLAY THE RUN INFORMATION
locate 14, th : print"Difference-generated sine function"
locate 23, th : print"Press any key to stop program."
locate 19, th : print"yMAX ="
locate 20, th : print"yMIN ="

'DRAW AXES FOR THE TIME-SERIES DIAGRAM
ts = 1                        'set time-series flag
c = 3                         'set colour to cyan for axes
t = nn : x = 0 : gosub p      'set pen to end of time axis
t = 0 : gosub l               'draw line to time-series origin
x = 1 : gosub l               'draw time-series y-axis
locate  2,  3 : print "y"
locate 12, 74 : print "time"

'DRAW THE GRAPH AXES FOR THE PHASE DIAGRAM
ts = 0                        'clear time-series flag
x = 1 : y = 0 : gosub p       'set pen to end of x-axis
x = 0 : gosub l               'draw line to origin
y = 1 : gosub l               'continue line to end of y-axis
locate 14, 43 : print "y"
locate 24, 73 : print "x";

'ITERATE NON-LINEAR DIFFERENCE FUNCTION UNTIL A KEY IS PRESSED
oldx = 0
t = 1                    'initialise the time
u = 61                   'start of trace-clearing sweep
ymax = 0                 'holds maximum excursion of y
ymin = 1                 'holds minimum excursion of y
sx = -1                  'set initial value of input
sy = 0                   'for position of first plot
gosub p                  'set plotting pen to starting value
e = 0                    'reset the out-of-range flag

while inkey\$ = ""        'while user has not pressed a key...
gosub p                'set starting point for current line
oldx = x
gosub f                'ITERATE THE DIFFERENCE FUNCTION
newx = x
gosub l                'draw line to point x, y
gosub TimeSeries       'put plot on time-series graph
for tt = 0 to 5000 : next
wend : end

'TIMES-SERIES GRAPH PLOTTING ROUTINE
TimeSeries:
ts = 1                   'set time-series flag
c = 10                   'set trace colour to bright green
if t > nn then t = 1     'if end of sweep has been reached
x = oldx : gosub p       'set to previous plot position
t = t + 5                'increment the time axis
x = newx : gosub l       'draw line to current plot
if u > nn then u = 1
uu = u + hh : uuu = uu + 10
for i = uu to uuu        'draw the trace-wiper in blue
line (i, vv - n) - (i, vv - 1), 1
next
u = u + 5
ts = 0                    'clear the time-series flag
return

p:
gosub q : pset(a, b), c   'set starting point for current line
return
l:
gosub q : line -(a, b), c 'draw line to point x, y
return

'CONVERT REAL X/Y CO-ORD TO SCALED INTEGRAL NUMBERS OF PIXELS
q:
if ts = 0 then
a = s * (h + n * x)
b = v - n * y
else
a = hh + t
b = vv - n * x
end if
return

'ITERATE THE DIFFERENCE FUNCTION
f:
if sx < 0 then sa = .05 else sa = -.05
sy = sy + sa
sx = sx + sy
if sy > ymax then ymax = sy : locate 19, th + 8 : gosub num
if sy < ymin then ymin = sy : locate 20, th + 8 : gosub num
x = sx / 4 + .51
y = sy / 4 + .51
return
num:
y\$ = str\$(sy) : PRINT y\$ + space\$(13 - len(y\$))
return

© October 1997 Robert John Morton | PREV | NEXT