Showing posts with label chaotic attractor. Show all posts
Showing posts with label chaotic attractor. Show all posts

Thursday, September 17, 2020

STRANGE ATTRACTORS + BASIC CODING

 

More fun with BASIC and Strange Attractors...

 

This combines two things I’ve been playing with: Strange Attractors (previously via oscilloscopes and oscillating circuits); and BASIC computer language (via a download of Rob Hagemans’ awaesome PC-BASIC emulator, which loaded on my 2020 HP Prodesk just fine: https://robhagemans.github.io/pcbasic/ ).

 

The first program, which comes from Julien Sprott’s “Strange Attractors” book (1993) wouldn’t work at all for me, because it had no callouts in the BASIC programming for video display. I solved that by FINALLY figuring out/remembering I needed to set the display to something my modern computer could use while using an emulator.

 




If you run this program and get an error code about line 1350 make sure to set your video to something the emulator can deal with. So, run the program. Did you get the error code? Type in “SCREEN 9” which will change the screen resolution in the BASIC emulator and all of a sudden it works! You can try lower numbers than 9.

If you find a number that works you could actually add it to the program AFTER line 1010. So it would be like “1011 SCREEN 9”. I didn’t add it to the program below so you could play around and see what video mode works for your setup. I tried adding it before the DEFDBL A-Z statement and it didn’t work (make it a number after 1010 but before 1020).

 


Remember, after cutting and pasting the code into your emulator (using ctrl+c to copy off of here, but using F11+V to paste in the emulator) you must type SCREEN 9 if you get a “1350 error”. Then just type "run" again.

 

1010 DEFDBL A-Z

1020 DIM XS(499)

1030 SM% = 12

1040 PREV% = 2

1050 NMAX = 11000

1160 RANDOMIZE TIMER

1190 GOSUB 1300

1200 GOSUB 1500

1210 GOSUB 1700

1220 GOSUB 2100

1230 GOSUB 2400

1240 ON T% GOTO 1190, 1200, 1210

1250 CLS

1260 END

1300 REM INITIALIZE

1350 WINDOW (-.1, -.1)-(1.1, 1.1)

1360 CLS : LOCATE 13, 44: PRINT "MIKE LOGUSZ"

1420 RETURN

1500 REM SET PARAMETERS

1510 X = .05

1560 R = 4

1570 T% = 3

1580 P% = 0

1590 LINE (-.1, -.1)-(1.1, 1.1),,B

1630 RETURN

1700 REM ITERATE EQUATIONS

1720 XNEW = R * X * (1 - X)

2030 RETURN

2100 REM DISPLAY RESULTS

2210 XS(P%) = X

2220 P% = (P% + 1) MOD 500

2230 I% = (P% + 500 - PREV%) MOD 500

2300 PSET (XS(I%), XNEW)

2320 RETURN

2400 REM TEST RESULTS

2490 IF LEN (INKEY$) THEN T% = O

2510 X = XNEW

2550 RETURN


The 1040 PREV% = 2 line is the iterations (humps) in the wave. You can use 1-10. Starting here is 2, you should try 5 next. The higher you go the slower everything is. It gets boring. The photo shows line 1040 set to "5".

 

The 1360 can be changed to put your name on the screen. The "44" number on that line is what makes the name over to the right. Try making it a "34" and center it better if you like.

 

In the program above I've already made changes that incorporate the following caveats from author Julein Sprott:

"Line 1010: Be sure your version of BASIC supports double-precision (four-byte) floating-point variables. If it doesn't you may omit this line but you will then probably have to change the 4 in line 1560 to 3.99999 to avoid overflow resulting from round-off errors. With modern versions of BASIC and a computer with a math coprocessor there is no penalty and considerable advantage in using double precision. Because of the finite precision of computer arithmetic all cases will eventually repeat but with double precision the average number of iterations required before this happens is acceptably large.

Line 1320: Either your version of BASIC doesn't require this command or your computer or compiler doesn't support VGA graphics. Try reducing the 12 in line 1030 to a lower number until you find one that works. If none work try eliminating line 1320 altogether.

Line 1350: The WINDOW command defines the coordinates of the lower-left and upper-right corners of the graphics window for subsequent PSET and LINE commands. If your version of BASIC doesn't support this command you will have to delete this line and convert all the parameters in the PSET and LINE commands to address screen pixels. In such a case try replacing line 2300 with PSET (200 * X 200 - 200 * XNEW). One advantage of using the WINDOW command is that when a version of BASIC comes along that supports higher screen resolutions the program can be easily recompiled to take advantage of it."

You can do/undo these if you wish. Search "Strange Attractors" by Sprott for a cool author's website with html, word Doc and PDF versions of the book for free! Ebay also has copies of this book with the original (floppy) discs included--that also has other versions besides BASIC! Or just check out the word Doc from the author's site. The html has scanning errors/omissions and the PDF might too. I found fixes at the Word Doc versions. Check all three!


In the above I've done program 1 + program 2 + the fixes + my own little fix to the book's coding. Program 1 would give you just a single parabola instead of awesome chaos without the iterations of the 1040 line (introduced as program 2).



More Chaos!!! Even Stranger Attractors!!!!

Here is the program for up to fourth-dimension attractors with colors and different map surfaces (plane, sphere, torus, etc.). It seems to work just fine graphically without having to mess with anything like SCREEN 9 because the 1020 line is better and works with other lines of code to make the screen OK, at least in my emulator on my machine.

 




The additional lines of code (a lot) bring into the search for chaos quadratic equations, fractals, Henon maps, cubic and quartic and quintic maps, project onto various planes and surfaces, and can even use sound to express the results (see line of code 3500)! 


1010 DEFDBL A-Z                 'Use double precision

1020 DIM XS(499), YS(499), ZS(499), WS(499), A(504), V(99), XY(4), XN(4), COLR%(15)

1030 SM% = 12                   'Assume VGA graphics

1040 PREV% = 5                  'Plot versus fifth previous iterate

1050 NMAX = 11000               'Maximum number of iterations

1060 OMAX% = 5                  'Maximum order of polynomial

1070 D% = 2                     'Dimension of system

1080 EPS = .1                   'Step size for ODE

1090 ODE% = 0                   'System is map

1100 SND% = 0                   'Turn sound off

1110 PJT% = 0                   'Projection is planar

1120 TRD% = 1                   'Display third dimension as shadow

1130 FTH% = 2                   'Display fourth dimension as colors

1140 SAV% = 0                   'Don't save any data

1150 TWOPI = 6.28318530717959#  'A useful constant (2 pi)

1160 RANDOMIZE TIMER            'Reseed random number generator

1170 GOSUB 4200                 'Display menu screen

1180 IF Q$ = "X" THEN GOTO 1250 'Exit immediately on command

1190 GOSUB 1300                 'Initialize

1200 GOSUB 1500                 'Set parameters

1210 GOSUB 1700                 'Iterate equations

1220 GOSUB 2100                 'Display results

1230 GOSUB 2400                 'Test results

1240 ON T% GOTO 1190, 1200, 1210

1250 CLS

1260 END

 

1300 REM Initialize

1310 ON ERROR GOTO 6600         'Find legal graphics mode

1320 SCREEN SM%                 'Set graphics mode

1330 ON ERROR GOTO 0            'Resume default error trapping

1340 DEF SEG = 64: WID% = PEEK(74)  'Number of text columns

1350 WINDOW (-.1, -.1)-(1.1, 1.1)

1360 CLS : LOCATE 13, WID% / 2 - 6: PRINT "Searching Logusz..."

1370 GOSUB 5600                 'Set colors

1380 IF QM% <> 2 THEN GOTO 1420

1390    NE = 0: CLOSE

1400    OPEN "SA.DIC" FOR APPEND AS #1: CLOSE

1410    OPEN "SA.DIC" FOR INPUT AS #1

1420 RETURN

 

1500 REM Set parameters

1510 X = .05                    'Initial condition

1520 Y = .05

1530 Z = .05

1540 W = .05

1550 XE = X + .000001: YE = Y: ZE = Z: WE = W

1560 GOSUB 2600                 'Get coefficients

1570 T% = 3

1580 P% = 0: LSUM = 0: N = 0: NL = 0: N1 = 0: N2 = 0

1590 XMIN = 1000000!: XMAX = -XMIN: YMIN = XMIN: YMAX = XMAX

1600 ZMIN = XMIN: ZMAX = XMAX

1610 WMIN = XMIN: WMAX = XMAX

1620 TWOD% = 2 ^ D%

1630 RETURN

 

1700 REM Iterate equations

1710 IF ODE% > 1 THEN GOSUB 6200: GOTO 2020     'Special function

1720 M% = 1: XY(1) = X: XY(2) = Y: XY(3) = Z: XY(4) = W

1730 FOR I% = 1 TO D%

1740 XN(I%) = A(M%)

1750 M% = M% + 1

1760 FOR I1% = 1 TO D%

1770 XN(I%) = XN(I%) + A(M%) * XY(I1%)

1780 M% = M% + 1

1790 FOR I2% = I1% TO D%

1800 XN(I%) = XN(I%) + A(M%) * XY(I1%) * XY(I2%)

1810 M% = M% + 1

1820 IF O% = 2 THEN GOTO 1970

1830 FOR I3% = I2% TO D%

1840 XN(I%) = XN(I%) + A(M%) * XY(I1%) * XY(I2%) * XY(I3%)

1850 M% = M% + 1

1860 IF O% = 3 THEN GOTO 1960

1870 FOR I4% = I3% TO D%

1880 XN(I%) = XN(I%) + A(M%) * XY(I1%) * XY(I2%) * XY(I3%) * XY(I4%)

1890 M% = M% + 1

1900 IF O% = 4 THEN GOTO 1950

1910 FOR I5% = I4% TO D%

1920 XN(I%) = XN(I%) + A(M%) * XY(I1%) * XY(I2%) * XY(I3%) * XY(I4%) * XY(I5%)

1930 M% = M% + 1

1940 NEXT I5%

1950 NEXT I4%

1960 NEXT I3%

1970 NEXT I2%

1980 NEXT I1%

1990 IF ODE% = 1 THEN XN(I%) = XY(I%) + EPS * XN(I%)

2000 NEXT I%

2010 M% = M% - 1: XNEW = XN(1): YNEW = XN(2): ZNEW = XN(3): WNEW = XN(4)

2020 N = N + 1

2030 RETURN

 

2100 REM Display results

2110 IF N < 100 OR N > 1000 THEN GOTO 2200

2120    IF X < XMIN THEN XMIN = X

2130    IF X > XMAX THEN XMAX = X

2140    IF Y < YMIN THEN YMIN = Y

2150    IF Y > YMAX THEN YMAX = Y

2160    IF Z < ZMIN THEN ZMIN = Z

2170    IF Z > ZMAX THEN ZMAX = Z

2180    IF W < WMIN THEN WMIN = W

2190    IF W > WMAX THEN WMAX = W

2200 IF N = 1000 THEN GOSUB 3100        'Resize the screen

2210 XS(P%) = X: YS(P%) = Y: ZS(P%) = Z: WS(P%) = W

2220 P% = (P% + 1) MOD 500

2230 I% = (P% + 500 - PREV%) MOD 500

2240 IF D% = 1 THEN XP = XS(I%): YP = XNEW ELSE XP = X: YP = Y

2250 IF N < 1000 OR XP <= XL OR XP >= XH OR YP <= YL OR YP >= YH THEN GOTO 2320

2260    IF PJT% = 1 THEN GOSUB 4100     'Project onto a sphere

2270    IF PJT% = 2 THEN GOSUB 6700     'Project onto a horizontal cylinder

2280    IF PJT% = 3 THEN GOSUB 6800     'Project onto a vertical cylinder

2290    IF PJT% = 4 THEN GOSUB 6900     'Project onto a torus

2300    GOSUB 5000                      'Plot point on screen

2310    IF SND% = 1 THEN GOSUB 3500     'Produce sound

2320 RETURN

 

2400 REM Test results

2410 IF ABS(XNEW) + ABS(YNEW) + ABS(ZNEW) + ABS(WNEW) > 1000000! THEN T% = 2

2420 IF QM% = 2 THEN GOTO 2490  'Speed up evaluation mode

2430 GOSUB 2900                 'Calculate Lyapunov exponent

2440 GOSUB 3900                 'Calculate fractal dimension

2450 IF QM% > 0 THEN GOTO 2490  'Skip tests when not in search mode

2460 IF N >= NMAX THEN T% = 2: GOSUB 4900   'Strange attractor found

2470 IF ABS(XNEW - X) + ABS(YNEW - Y) + ABS(ZNEW - Z) + ABS(WNEW - W) < .000001 THEN T% = 2

2480 IF N > 100 AND L < .005 THEN T% = 2    'Limit cycle

2490 Q$ = INKEY$: IF LEN(Q$) THEN GOSUB 3600        'Respond to user command

2500 IF SAV% > 0 THEN IF N > 1000 AND N < 17001 THEN GOSUB 7000 'Save data

2510 X = XNEW                   'Update value of X

2520 Y = YNEW

2530 Z = ZNEW

2540 W = WNEW

2550 RETURN

 

2600 REM Get coefficients

2610 IF QM% <> 2 THEN GOTO 2640 'Not in evaluate mode

2620    IF EOF(1) THEN QM% = 0: GOSUB 6000: GOTO 2640

2630    IF EOF(1) = 0 THEN LINE INPUT #1, CODE$: GOSUB 4700: GOSUB 5600

2640 IF QM% > 0 THEN GOTO 2730  'Not in search mode

2650    O% = 2 + INT((OMAX% - 1) * RND)

2660    CODE$ = CHR$(59 + 4 * D% + O% + 8 * ODE%)

2670    IF ODE% > 1 THEN CODE$ = CHR$(87 + ODE%)

2680    GOSUB 4700              'Get value of M%

2690    FOR I% = 1 TO M%        'Construct CODE$

2700        GOSUB 2800          'Shuffle random numbers

2710        CODE$ = CODE$ + CHR$(65 + INT(25 * RAN))

2720    NEXT I%

2730 FOR I% = 1 TO M%           'Convert CODE$ to coefficient values

2740    A(I%) = (ASC(MID$(CODE$, I% + 1, 1)) - 77) / 10

2750 NEXT I%

2760 RETURN

 

2800 REM Shuffle random numbers

2810 IF V(0) = 0 THEN FOR J% = 0 TO 99: V(J%) = RND: NEXT J%

2820 J% = INT(100 * RAN)

2830 RAN = V(J%)

2840 V(J%) = RND

2850 RETURN

 

2900 REM Calculate Lyapunov exponent

2910 XSAVE = XNEW: YSAVE = YNEW: ZSAVE = ZNEW: WSAVE = WNEW

2920 X = XE: Y = YE: Z = ZE: W = WE: N = N - 1

2930 GOSUB 1700                 'Reiterate equations

2940 DLX = XNEW - XSAVE: DLY = YNEW - YSAVE

2950 DLZ = ZNEW - ZSAVE: DLW = WNEW - WSAVE

2960 DL2 = DLX * DLX + DLY * DLY + DLZ * DLZ + DLW * DLW

2970 IF CSNG(DL2) <= 0 THEN GOTO 3070       'Don't divide by zero

2980    DF = 1000000000000# * DL2

2990    RS = 1 / SQR(DF)

3000    XE = XSAVE + RS * (XNEW - XSAVE): YE = YSAVE + RS * (YNEW - YSAVE)

3010    ZE = ZSAVE + RS * (ZNEW - ZSAVE): WE = WSAVE + RS * (WNEW - WSAVE)

3020    XNEW = XSAVE: YNEW = YSAVE: ZNEW = ZSAVE: WNEW = WSAVE

3030    LSUM = LSUM + LOG(DF): NL = NL + 1

3040    L = .721347 * LSUM / NL

3050    IF ODE% = 1 OR ODE% = 7 THEN L = L / EPS

3060    IF N > 1000 AND N MOD 10 = 0 THEN LOCATE 1, WID% - 4: PRINT USING "##.##"; L;

3070 RETURN

 

3100 REM Resize the screen

3110 IF D% = 1 THEN YMIN = XMIN: YMAX = XMAX

3120 IF XMAX - XMIN < .000001 THEN XMIN = XMIN - .0000005: XMAX = XMAX + .0000005

3130 IF YMAX - YMIN < .000001 THEN YMIN = YMIN - .0000005: YMAX = YMAX + .0000005

3140 IF ZMAX - ZMIN < .000001 THEN ZMIN = ZMIN - .0000005: ZMAX = ZMAX + .0000005

3150 IF WMAX - WMIN < .000001 THEN WMIN = WMIN - .0000005: WMAX = WMAX + .0000005

3160 MX = .1 * (XMAX - XMIN): MY = .1 * (YMAX - YMIN)

3170 XL = XMIN - MX: XH = XMAX + MX: YL = YMIN - MY: YH = YMAX + 1.5 * MY

3180 WINDOW (XL, YL)-(XH, YH): CLS

3190 YH = YH - .5 * MY

3200 XA = (XL + XH) / 2: YA = (YL + YH) / 2

3210 IF D% < 3 THEN GOTO 3310

3220    ZA = (ZMAX + ZMIN) / 2

3230    IF TRD% = 1 THEN LINE (XL, YL)-(XH, YH), COLR%(1), BF: GOSUB 5400

3240    IF TRD% = 4 THEN LINE (XL, YL)-(XH, YH), WH%, BF

3250    IF TRD% = 5 THEN LINE (XA, YL)-(XA, YH)

3260    IF TRD% <> 6 THEN GOTO 3310

3270        FOR I% = 1 TO 3

3280            XP = XL + I% * (XH - XL) / 4: LINE (XP, YL)-(XP, YH)

3290            YP = YL + I% * (YH - YL) / 4: LINE (XL, YP)-(XH, YP)

3300        NEXT I%

3310 IF PJT% <> 1 THEN LINE (XL, YL)-(XH, YH), , B

3320 IF PJT% = 1 AND TRD% < 5 THEN CIRCLE (XA, YA), .36 * (XH - XL)

3330 TT = 3.1416 / (XMAX - XMIN): PT = 3.1416 / (YMAX - YMIN)

3340 IF QM% <> 2 THEN GOTO 3400         'Not in evaluate mode

3350    LOCATE 1, 1: PRINT "<Space Bar>: Discard       <Enter>: Save";

3360    IF WID% < 80 THEN GOTO 3390

3370    LOCATE 1, 49: PRINT "<Esc>: Exit";

3380    LOCATE 1, 69: PRINT CINT((LOF(1) - 128 * LOC(1)) / 1024); "K left";

3390    GOTO 3430

3400 LOCATE 1, 1: IF LEN(CODE$) < WID% - 18 THEN PRINT CODE$

3410 IF LEN(CODE$) >= WID% - 18 THEN PRINT LEFT$(CODE$, WID% - 23) + "..."

3420 LOCATE 1, WID% - 17: PRINT "F =": LOCATE 1, WID% - 7: PRINT "L ="

3430 TIA = .05               'Tangent of illumination angle

3440 XZ = -TIA * (XMAX - XMIN) / (ZMAX - ZMIN)

3450 YZ = TIA * (YMAX - YMIN) / (ZMAX - ZMIN)

3460 RETURN

 

3500 REM Produce sound

3510 FREQ% = 220 * 2 ^ (CINT(36 * (XNEW - XL) / (XH - XL)) / 12)

3520 DUR = 1

3530 IF D% > 1 THEN DUR = 2 ^ INT(.5 * (YH - YL) / (YNEW - 9 * YL / 8 + YH / 8))

3540 SOUND FREQ%, DUR: IF PLAY(0) THEN PLAY "MF"

3550 RETURN

 

3600 REM Respond to user command

3610 IF ASC(Q$) > 96 THEN Q$ = CHR$(ASC(Q$) - 32)   'Convert to upper case

3620 IF QM% = 2 THEN GOSUB 5800                 'Process evaluation command

3630 IF INSTR("ACDEHINPRSVX", Q$) = 0 THEN GOSUB 4200   'Display menu screen

3640 IF Q$ = "A" THEN T% = 1: QM% = 0

3650 IF ODE% > 1 THEN D% = ODE% + 5

3660 IF ODE% = 1 THEN D% = D% + 2

3670 IF Q$ = "C" THEN IF N > 999 THEN N = 999

3680 IF Q$ = "D" THEN D% = 1 + (D% MOD 12): T% = 1

3690 IF D% > 6 THEN ODE% = D% - 5: D% = 4: GOTO 3710

3700 IF D% > 4 THEN ODE% = 1: D% = D% - 2 ELSE ODE% = 0

3710 IF Q$ = "E" THEN T% = 1: QM% = 2

3720 IF Q$ = "H" THEN FTH% = (FTH% + 1) MOD 3: T% = 3: IF N > 999 THEN N = 999: GOSUB 5600

3730 IF Q$ = "I" THEN IF T% <> 1 THEN SCREEN 0: WIDTH 80: COLOR 15, 1: CLS : LINE INPUT "Code? "; CODE$: IF CODE$ = "" THEN Q$ = " ": CLS :  ELSE T% = 1: QM% = 1: GOSUB 4700

3740 IF Q$ = "N" THEN NMAX = 10 * (NMAX - 1000) + 1000: IF NMAX > 10 ^ 10 THEN NMAX = 2000

3750 IF Q$ = "P" THEN PJT% = (PJT% + 1) MOD 5: T% = 3: IF N > 999 THEN N = 999

3760 IF Q$ = "R" THEN TRD% = (TRD% + 1) MOD 7: T% = 3: IF N > 999 THEN N = 999: GOSUB 5600

3770 IF Q$ = "S" THEN SND% = (SND% + 1) MOD 2: T% = 3

3780 IF Q$ = "V" THEN SAV% = (SAV% + 1) MOD 5: FAV$ = CHR$(87 + SAV% MOD 4): T% = 3: IF N > 999 THEN N = 999

3790 IF Q$ = "X" THEN T% = 0

3800 RETURN

 

3900 REM Calculate fractal dimension

3910 IF N < 1000 THEN GOTO 4010         'Wait for transient to settle

3920 IF N = 1000 THEN D2MAX = (XMAX - XMIN) ^ 2 + (YMAX - YMIN) ^ 2 + (ZMAX - ZMIN) ^ 2 + (WMAX - WMIN) ^ 2

3930 J% = (P% + 1 + INT(480 * RND)) MOD 500

3940 DX = XNEW - XS(J%): DY = YNEW - YS(J%): DZ = ZNEW - ZS(J%): DW = WNEW - WS(J%)

3950 D2 = DX * DX + DY * DY + DZ * DZ + DW * DW

3960 IF D2 < .001 * TWOD% * D2MAX THEN N2 = N2 + 1

3970 IF D2 > .00001 * TWOD% * D2MAX THEN GOTO 4010

3980    N1 = N1 + 1

3990    F = .434294 * LOG(N2 / (N1 - .5))

4000    LOCATE 1, WID% - 14: PRINT USING "##.##"; F;

4010 RETURN

 

4100 REM Project onto a sphere

4110 TH = TT * (XMAX - XP)

4120 PH = PT * (YMAX - YP)

4130 XP = XA + .36 * (XH - XL) * COS(TH) * SIN(PH)

4140 YP = YA + .5 * (YH - YL) * COS(PH)

4150 RETURN

 

4200 REM Display menu screen

4210 SCREEN 0: WIDTH 80: COLOR 15, 1: CLS

4220 WHILE Q$ = "" OR INSTR("AEIX", Q$) = 0

4230    LOCATE 1, 27: PRINT "STRANGE ATTRACTOR PROGRAM"

4240    PRINT TAB(27); "IBM PC BASIC Version 2.0"

4250    PRINT TAB(27); "(c) 1993 by J. C. Sprott"

4260    PRINT : PRINT

4270    PRINT TAB(27); "A: Search for attractors"

4280    PRINT TAB(27); "C: Clear screen and restart"

4290    IF ODE% > 1 THEN PRINT TAB(27); "D: System is 4-D special map "; CHR$(87 + ODE%); " ": GOTO 4320

4300    PRINT TAB(27); "D: System is"; STR$(D%); "-D polynomial ";

4310    IF ODE% = 1 THEN PRINT "ODE" ELSE PRINT "map"

4320    PRINT TAB(27); "E: Evaluate attractors"

4330    PRINT TAB(27); "H: Fourth dimension is ";

4340        IF FTH% = 0 THEN PRINT "projection"

4350        IF FTH% = 1 THEN PRINT "bands     "

4360        IF FTH% = 2 THEN PRINT "colors    "

4370    PRINT TAB(27); "I: Input code from keyboard"

4380    PRINT TAB(27); "N: Number of iterations is 10^";

4390    PRINT USING "#"; CINT(LOG(NMAX - 1000) / LOG(10))

4400    PRINT TAB(27); "P: Projection is ";

4410        IF PJT% = 0 THEN PRINT "planar   "

4420        IF PJT% = 1 THEN PRINT "spherical"

4430        IF PJT% = 2 THEN PRINT "horiz cyl"

4440        IF PJT% = 3 THEN PRINT "vert cyl "

4450        IF PJT% = 4 THEN PRINT "toroidal "

4460    PRINT TAB(27); "R: Third dimension is ";

4470        IF TRD% = 0 THEN PRINT "projection"

4480        IF TRD% = 1 THEN PRINT "shadow    "

4490        IF TRD% = 2 THEN PRINT "bands     "

4500        IF TRD% = 3 THEN PRINT "colors    "

4510        IF TRD% = 4 THEN PRINT "anaglyph  "

4520        IF TRD% = 5 THEN PRINT "stereogram"

4530        IF TRD% = 6 THEN PRINT "slices    "

4540    PRINT TAB(27); "S: Sound is ";

4550        IF SND% = 0 THEN PRINT "off"

4560        IF SND% = 1 THEN PRINT "on "

4570    PRINT TAB(27); "V: ";

4580        IF SAV% = 0 THEN PRINT "No data will be saved       "

4590        IF SAV% > 0 THEN PRINT FAV$; " will be saved in "; FAV$; "DATA.DAT"

4600    PRINT TAB(27); "X: Exit program"

4610    Q$ = INKEY$

4620    IF Q$ <> "" THEN GOSUB 3600     'Respond to user command

4630 WEND

4640 RETURN

 

4700 REM Get dimension and order

4710 D% = 1 + INT((ASC(LEFT$(CODE$, 1)) - 65) / 4)

4720 IF D% > 6 THEN ODE% = ASC(LEFT$(CODE$, 1)) - 87: D% = 4: GOSUB 6200: GOTO 4770

4730 IF D% > 4 THEN D% = D% - 2: ODE% = 1 ELSE ODE% = 0

4740 O% = 2 + (ASC(LEFT$(CODE$, 1)) - 65) MOD 4

4750 M% = 1: FOR I% = 1 TO D%: M% = M% * (O% + I%): NEXT I%

4760 IF D% > 2 THEN FOR I% = 3 TO D%: M% = M% / (I% - 1): NEXT I%

4770 IF LEN(CODE$) = M% + 1 OR QM% <> 1 THEN GOTO 4810

4780    BEEP        'Illegal code warning

4790    WHILE LEN(CODE$) < M% + 1: CODE$ = CODE$ + "M": WEND

4800    IF LEN(CODE$) > M% + 1 THEN CODE$ = LEFT$(CODE$, M% + 1)

4810 RETURN

 

4900 REM Save attractor to disk file SA.DIC

4910 OPEN "SA.DIC" FOR APPEND AS #1

4920 PRINT #1, CODE$; : PRINT #1, USING "##.##"; F; L

4930 CLOSE #1

4940 RETURN

 

5000 REM Plot point on screen

5010 C4% = WH%

5020 IF D% < 4 THEN GOTO 5050

5030    IF FTH% = 1 THEN IF INT(30 * (W - WMIN) / (WMAX - WMIN)) MOD 2 THEN GOTO 5330

5040    IF FTH% = 2 THEN C4% = 1 + INT(NC% * (W - WMIN) / (WMAX - WMIN) + NC%) MOD NC%

5050 IF D% < 3 THEN PSET (XP, YP): GOTO 5330    'Skip 3-D stuff

5060 IF TRD% = 0 THEN PSET (XP, YP), C4%

5070 IF TRD% <> 1 THEN GOTO 5130

5080    IF D% > 3 AND FTH% = 2 THEN PSET (XP, YP), C4%: GOTO 5110

5090    C% = POINT(XP, YP)

5100    IF C% = COLR%(2) THEN PSET (XP, YP), COLR%(3) ELSE IF C% <> COLR%(3) THEN PSET (XP, YP), COLR%(2)

5110    XP = XP - XZ * (Z - ZMIN): YP = YP - YZ * (Z - ZMIN)

5120    IF POINT(XP, YP) = COLR%(1) THEN PSET (XP, YP), 0

5130 IF TRD% <> 2 THEN GOTO 5160

5140    IF D% > 3 AND FTH% = 2 AND (INT(15 * (Z - ZMIN) / (ZMAX - ZMIN) + 2) MOD 2) = 1 THEN PSET (XP, YP), C4%

5150    IF D% < 4 OR FTH% <> 2 THEN C% = COLR%(INT(60 * (Z - ZMIN) / (ZMAX - ZMIN) + 4) MOD 4): PSET (XP, YP), C%

5160 IF TRD% = 3 THEN PSET (XP, YP), COLR%(INT(NC% * (Z - ZMIN) / (ZMAX - ZMIN) + NC%) MOD NC%)

5170 IF TRD% <> 4 THEN GOTO 5240

5180    XRT = XP + XZ * (Z - ZA): C% = POINT(XRT, YP)

5190    IF C% = WH% THEN PSET (XRT, YP), RD%

5200    IF C% = CY% THEN PSET (XRT, YP), BK%

5210    XLT = XP - XZ * (Z - ZA): C% = POINT(XLT, YP)

5220    IF C% = WH% THEN PSET (XLT, YP), CY%

5230    IF C% = RD% THEN PSET (XLT, YP), BK%

5240 IF TRD% <> 5 THEN GOTO 5280

5250    HSF = 2                 'Horizontal scale factor

5260    XRT = XA + (XP + XZ * (Z - ZA) - XL) / HSF: PSET (XRT, YP), C4%

5270    XLT = XA + (XP - XZ * (Z - ZA) - XH) / HSF: PSET (XLT, YP), C4%

5280 IF TRD% <> 6 THEN GOTO 5330

5290    DZ = (15 * (Z - ZMIN) / (ZMAX - ZMIN) + .5) / 16

5300    XP = (XP - XL + (INT(16 * DZ) MOD 4) * (XH - XL)) / 4 + XL

5310    YP = (YP - YL + (3 - INT(4 * DZ) MOD 4) * (YH - YL)) / 4 + YL

5320    PSET (XP, YP), C4%

5330 RETURN

 

5400 REM Plot background grid

5410 FOR I% = 0 TO 15           'Draw 15 vertical grid lines

5420    XP = XMIN + I% * (XMAX - XMIN) / 15

5430    LINE (XP, YMIN)-(XP, YMAX), 0

5440 NEXT I%

5450 FOR I% = 0 TO 10           'Draw 10 horizontal grid lines

5460    YP = YMIN + I% * (YMAX - YMIN) / 10

5470    LINE (XMIN, YP)-(XMAX, YP), 0

5480 NEXT I%

5490 RETURN

 

5600 REM Set colors

5610 NC% = 15                   'Number of colors

5620 COLR%(0) = 0: COLR%(1) = 8: COLR%(2) = 7: COLR%(3) = 15

5630 IF TRD% = 3 OR (D% > 3 AND FTH% = 2 AND TRD% <> 1) THEN FOR I% = 0 TO NC%: COLR%(I%) = I% + 1: NEXT I%

5640 WH% = 15: BK% = 8: RD% = 12: CY% = 11

5650 IF SM% > 2 THEN GOTO 5720  'Not in CGA mode

5660    WID% = 80: IF D% < 3 THEN SCREEN 2: GOTO 5720

5670    IF (TRD% = 0 OR TRD% > 4) AND (D% = 3 OR FTH% <> 2) THEN SCREEN 2: GOTO 5720

5680    WID% = 40: SCREEN 1

5690    COLR%(0) = 0: COLR%(1) = 2: COLR%(2) = 1: COLR%(3) = 3

5700    WH% = 3: BK% = 0: RD% = 2: CY% = 1

5710    FOR I% = 4 TO NC%: COLR%(I%) = COLR%(I% MOD 4 + 1): NEXT I%

5720 RETURN

 

5800 REM Process evaluation command

5810 IF Q$ = " " THEN T% = 2: NE = NE + 1: CLS

5820 IF Q$ = CHR$(13) THEN T% = 2: NE = NE + 1: CLS : GOSUB 5900

5830 IF Q$ = CHR$(27) THEN CLS : GOSUB 6000: Q$ = " ": QM% = 0: GOTO 5850

5840 IF Q$ <> CHR$(27) AND INSTR("CHNPRVS", Q$) = 0 THEN Q$ = ""

5850 RETURN

 

5900 REM Save favorite attractors to disk file FAVORITE.DIC

5910 OPEN "FAVORITE.DIC" FOR APPEND AS #2

5920 PRINT #2, CODE$

5930 CLOSE #2

5940 RETURN

 

6000 REM Update SA.DIC file

6010 LOCATE 11, 9: PRINT "Evaluation complete"

6020 LOCATE 12, 8: PRINT NE; "cases evaluated"

6030 OPEN "SATEMP.DIC" FOR OUTPUT AS #2

6040 IF QM% = 2 THEN PRINT #2, CODE$

6050 WHILE NOT EOF(1): LINE INPUT #1, CODE$: PRINT #2, CODE$: WEND

6060 CLOSE

6070 KILL "SA.DIC"

6080 NAME "SATEMP.DIC" AS "SA.DIC"

6090 RETURN

 

6200 REM Special function definitions

6210 ZNEW = X * X + Y * Y       'Default 3rd and 4th dimension

6220 WNEW = (N - 100) / 900: IF N > 1000 THEN WNEW = (N - 1000) / (NMAX - 1000)

6230 IF ODE% <> 2 THEN GOTO 6270

6240    M% = 10

6250    XNEW = A(1) + A(2) * X + A(3) * Y + A(4) * ABS(X) + A(5) * ABS(Y)

6260    YNEW = A(6) + A(7) * X + A(8) * Y + A(9) * ABS(X) + A(10) * ABS(Y)

6270 IF ODE% <> 3 THEN GOTO 6310

6280    M% = 14

6290    XNEW = A(1) + A(2) * X + A(3) * Y + (CINT(A(4) * X) AND CINT(A(5) * Y)) + (CINT(A(6) * X) OR CINT(A(7) * Y))

6300    YNEW = A(8) + A(9) * X + A(10) * Y + (CINT(A(11) * X) AND CINT(A(12) * Y)) + (CINT(A(13) * X) OR CINT(A(14) * Y))

6310 IF ODE% <> 4 THEN GOTO 6350

6320    M% = 14

6330    XNEW = A(1) + A(2) * X + A(3) * Y + A(4) * ABS(X) ^ A(5) + A(6) * ABS(Y) ^ A(7)

6340    YNEW = A(8) + A(9) * X + A(10) * Y + A(11) * ABS(X) ^ A(12) + A(13) * ABS(Y) ^ A(14)

6350 IF ODE% <> 5 THEN GOTO 6390

6360    M% = 18

6370    XNEW = A(1) + A(2) * X + A(3) * Y + A(4) * SIN(A(5) * X + A(6)) + A(7) * SIN(A(8) * Y + A(9))

6380    YNEW = A(10) + A(11) * X + A(12) * Y + A(13) * SIN(A(14) * X + A(15)) + A(16) * SIN(A(17) * Y + A(18))

6390 IF ODE% <> 6 THEN GOTO 6450

6400    M% = 6

6410    IF N < 2 THEN AL = TWOPI / (13 + 10 * A(6)): SINAL = SIN(AL): COSAL = COS(AL)

6420    DUM = X + A(2) * SIN(A(3) * Y + A(4))

6430    XNEW = 10 * A(1) + DUM * COSAL + Y * SINAL

6440    YNEW = 10 * A(5) - DUM * SINAL + Y * COSAL

6450 IF ODE% <> 7 THEN GOTO 6500

6460    M% = 9

6470    XNEW = X + EPS * A(1) * Y

6480    YNEW = Y + EPS * (A(2) * X + A(3) * X * X * X + A(4) * X * X * Y + A(5) * X * Y * Y + A(6) * Y + A(7) * Y * Y * Y + A(8) * SIN(Z))

6490    ZNEW = Z + EPS * (A(9) + 1.3): IF ZNEW > TWOPI THEN ZNEW = ZNEW - TWOPI

6500 RETURN

 

6600 REM Find legal graphics mode

6610 SM% = SM% - 1

6620 IF SM% = 0 THEN PRINT "This program requires a graphics monitor": STOP

6630 RESUME

 

6700 REM Project onto a horizontal cylinder

6710 PH = PT * (YMAX - YP)

6720 YP = YA + .5 * (YH - YL) * COS(PH)

6730 RETURN

 

6800 REM Project onto a vertical cylinder

6810 TH = TT * (XMAX - XP)

6820 XP = XA + .5 * (XH - XL) * COS(TH)

6830 RETURN

 

6900 REM Project onto a torus (unity aspect ratio)

6910 TH = TT * (XMAX - XP)

6920 PH = 2 * PT * (YMAX - YP)

6930 XP = XA + .18 * (XH - XL) * (1 + COS(TH)) * SIN(PH)

6940 YP = YA + .25 * (YH - YL) * (1 + COS(TH)) * COS(PH)

6950 RETURN

 

7000 REM Save data

7010 IF N = 1001 THEN CLOSE #3: OPEN FAV$ + "DATA.DAT" FOR OUTPUT AS #3

7020 IF SAV% = 1 THEN DUM = XNEW

7030 IF SAV% = 2 THEN DUM = YNEW

7040 IF SAV% = 3 THEN DUM = ZNEW

7050 IF SAV% = 4 THEN DUM = WNEW

7060 PRINT #3, CSNG(DUM)

7070 RETURN

 


Phew! That was fun!

Saturday, December 15, 2018

BASIC Programming for CHAOS


BASIC Programming for CHAOS







The last time I played with BASIC programming was on my Commodore 64 computer. I'm attempting run a program that shows chaotic bifurcations (just like my Chaotic Chua Circuits).


We'll start with a simple 7 line code that will become the center of our chaos inducing 23 line code (don't worry, you can just copy and paste from here once you download a BASIC emulator).


So, here is an iterated logistic difference equation showing animal population growth. If you run it on an online BASIC simulator it works. For this, an online simulator works fine. Try this one:  https://repl.it/repls/InsignificantTepidEquipment



TURN ON YOUR CAPS LOCK FOR ALL CAP LETTERS!

If you type "2" as your input the population hovers at around half an animal. If you type "5" as your starting population it goes to infinity (which is an error). So you need numbers less than 4 to keep it running true. The population levels out if you put in 1.5 as a start:


10 N=.1
20 INPUT"VALUE OF R";R
30 FOR I=1 TO 30
40 N1=R*N*(1-N)
50 PRINT N1
60 N=N1
70 NEXT I




Here is the screen capture of it in action:


Infinity: that's a lot of kitties! But an error in the program of going too high (R=5).



So here is R = 3.999




There is a new version of BASIC from Microsoft available as a download or an online simulator here: https://smallbasic-publicwebsite.azurewebsites.net/

It's called Small Basic...and the language is different.

Here's a cut and paste version of the above script in the new language (it works, but doesn't give as many lines of results):

N=0.1
TextWindow.Write("What is the value of R? ")
R = TextWindow.ReadNumber()
FOR I=1 TO 30
N1=R*N*(1-N)
TextWindow.WriteLine("The result it " + N1 + ".")
N=N1
ENDfor

Each one of these lines is a seperate line in Small Basic (just number them 1, 2, 3, etc.) like this screenshot:

Oddly, it cannot handle decimals for the R value, so here is R=5:



Fun in (Small) BASIC for the year 2020!!!!!!!!!!!

Bifurcation/Chaos

Now here is the bifurcation program, which I snagged from a booksale of weeded library books (I'm a librarian). There was a whole pile of stuff on chaos, but it was written in the early 1990s, so it was computer stuff and not oscilloscope stuff--pretty cool! Check out "Science of Chaos" by Christopher Lampton, the appendix (Page 111) has a great little primer on BASIC; although most of the commands seem to be in QBASIC and not BASIC (but it's been about 3 decades ago for me, so who knows). It doesn't matter because this is super easy (just copy and paste what I did).

You download and install a BASIC emulator onto your computer, like I did for the following video and screen shots you can just paste the code in. I used "PC-BASIC" made by Rob Hagemans because it is one of the few emulators that also emulates graphics that are needed to display the chaos! I got it off a site called SourceForge. A lot of BASIC/QBASIC/GW-BASIC emulators don't do graphics. PC-BASIC does. Worked for me on a modern Lenovo ThinkCentre desktop, LOL! https://sourceforge.net/projects/pcbasic/



There is an emulator is called "DOSBOX" also works but is very slow, and you can't paste into it--you just have to retype everything. It runs online, so no downloads. The problem is that you can't pause/exit a program without crashing/locking up:  https://archive.org/details/msdos_qbasic_megapack



Anyway, it is also one of the few emulators that seems to allow pasting lines of code in. That sure beats retyping everything.  I typed this program into an email at some point. Then I just copied it in the email program like normal (Control + C) and pasted into into PC-BASIC by hitting F11+V and it all appeared. Then I typed RUN and it started drwing the lines/single attractor/two splits (4) attractors and finally the chaos.



Here is the BASIC code for Bifurcation Leading To Chaos:


10 KEY OFF:CLS
20 SCREEN 7
30 DEFSNG A-Z
40 COLUMNS = 320
50 ROWS = 200
60 START = 1
70 FINISH = 3.999
80 TOP = 0
90 BOTTOM = 1
100 MAXREPS = 10
110 HEIGHT = BOTTOM - TOP
120 VPCT = 1/HEIGHT
130 FOR R = START TO FINISH STEP(FINISH - START)/COLUMNS
140 X = .1
150 FOR I = 1 TO 100
160 X = R * (X - X * X)
170 NEXT I
180 FOR I = 1 TO 30
190 X = R * (X - X * X)
200 PSET ((R- START) * COLUMNS/(FINISH - START), ROWS-(X - TOP) * ROWS * VPCT)
210 NEXT I
220 NEXT R
230 A$ = INPU$(1)


This was way easier than my Chua Circuit + Oscilloscope adventures in chaos (which were lots of fun too though).

After you cut and paste and run the program the line drawing will eventually stop. If you hit any key and then type LIST it will show you the lines of the program again.

Then comes a neat part: if you want to make a change in a line you could use arrow keys and move up and retype little bits and pieces of the code; or you could just retype the line.

Lets say you want to change line 20 from having a screen value of 7 to a value of 9. Right at the cursor that you're left at after hitting list just type:    20 SCREEN 9      then hit enter and then type RUN and it will change the line and run the program and you can see the changes it makes (or get an error).






COOL CHANGES YOU CAN MAKE TO THE ABOVE CODE:

Line 20 
This sets the screen resolution to 7.
You can change that to 9 and get higher resolution. It worked for me.
I tried setting it to 12, but got an error message. Probably because the emulator couldn't handle that high of a resolution. Here, the higher resolutions are the emulation of old EGA and VGA video cards, LOL! This program is actually best performed on an old CGA graphics adapter (which is why the crappy value of 7 is chosen--it's low res, but works).

Many emulators I tried gave an error at line 10 or 20, meaning they didn't emulate graphics adapters.



The QBASIC Screen values represent:

SCREEN 0: Textmode, cannot be used for graphics. This the screen mode that text based programs run on.

SCREEN 1: 320 x 200 Resolution. Four Colors

SCREEN 2: 640 x 200 Resolution. Two Colors (Black and White)

SCREEN 7: 320 x 200 Resolution. Sixteen Colors (USE THIS ONE)

SCREEN 8: 640 x 200 Resolution. Sixteen Colors

SCREEN 9: 640 x 350 Resolution. Sixteen Colors

SCREEN 10: 640 x 350 Resolution. Two Colors (Black and White)

SCREEN 11: 640 x 480 Resolution. Two Colors

SCREEN 12: 640 x 480 Resolution. Sixteen Colors

SCREEN 13: 320 x 200 Resolution. 256 Colors.





Line 40 and Line 50
I changed the values from the original 320 & 200 to higher values. This changed the aspect ratios and distorted things. Once the values got too high (around 800) it gave stack overflow errors. It also tended to start the line drawing near the center of the screen, and/or have stuff run off the edge of the screen which was annoying.




Lines 160 through Line 200
You'll notice these mirror the equation in the first (animal breeding) program.
What's crazy, is that if you plug in other equations, you'll get nearly identical chaotic bifurcations on screen...because whenever something breeds chaos, the chaos is the same. Chaos = Chaos!




Lines 60 through Line 90
Here's where it gets really interesting and beautiful. These lines are Start, finish, top and bottom. Which you would assume to just shift the same image on the screen left or right or up or down, just like the line 40 and line 50 stuff tended to do. You'd be wrong. It actually enlarges portions of the original output--and because it's a fractal like chaos it stays interesting and amazing.

Here are the original lines:

60 START = 1
70 FINISH = 3.999
80 TOP = 0
90 BOTTOM = 1

And here are a new set of values:

60 START = 3.5601
70 FINISH = 3.59
80 TOP = .34
90 BOTTOM = .35



I'll pop them into the entire code, so you can cut and paste one block:


10 KEY OFF:CLS
20 SCREEN 7
30 DEFSNG A-Z
40 COLUMNS = 320
50 ROWS = 200
60 START = 3.5601
70 FINISH = 3.59
80 TOP = .34
90 BOTTOM = .35
100 MAXREPS = 10
110 HEIGHT = BOTTOM - TOP
120 VPCT = 1/HEIGHT
130 FOR R = START TO FINISH STEP(FINISH - START)/COLUMNS
140 X = .1
150 FOR I = 1 TO 100
160 X = R * (X - X * X)
170 NEXT I
180 FOR I = 1 TO 30
190 X = R * (X - X * X)
200 PSET ((R- START) * COLUMNS/(FINISH - START), ROWS-(X - TOP) * ROWS * VPCT)
210 NEXT I
220 NEXT R
230 A$ = INPU$(1)


...and here are the results, which are a an enlarged portion of the lower "eye" of the original:



Keep playing with lines 60 through 90 and get your own crazy, unique views of chaos.




Line 150 / Line 180
Change the numbers in these so instead of 1 to 30. Let's say: make it 1 to 10 and 1 to 20 it will draw faster, but also differently!!! If you make the numbers larger (especially on Line 180 it will draw much slower, and also give an error at the end that might blank the screen out--so take a photo before it reaches the end just in case--or video).



Lower numbers in Lines 150 and 180 so it drew very quickly, and gave a sort of negative Lorenz Butterfly type look:




This one had a larger number on Line 180 and took a couple minutes to finish. It's filled in quite a bit in the chaotic area (which might not be chaotic anymore I guess).






Another useful feature you can use in QBASIC (which I think most of these commands are actually from) is BEEP.

At the end of your program add another line and write BEEP.

So for the above experiments we would add:

240 BEEP

Then, when your chaos is done drawing you're treated to a very loud BEEP! Don't have headphones on with that one.



Sunday, July 15, 2018

Chua Circuit Chaos Easy Build





Chua Circuit Chaos Easy Build - No Inductor




Here's a phase portrait of a beautiful single scroll chaotic attractor from my oscilloscope! I was able to also confure up a multiple scroll attractor and also a teeny-tiny double scroll illustrating the classic chaotic circuit output. A Strange Attractor is when a bounded chaotic system has some kind of long term pattern that isn’t a simple periodic oscillation or orbit.



Here is how two superimposed attractors form the famous double-scroll attractor:





Here is a video:;







Nice Mobius strip attractor, the only possible attractors in this pattern are limit cycles:







Here is a multile limit cycle attractor as it adds more ovals to become a single scroll chaotic attractor.



My oscilloscope settings for my "newer" scope.

There is a progression from steady state to limit cycle to chaos and bifurcation. Steady state is a dot; an oval signifies a 1 period limit cycle; multiple overlapped ovals show 2 or 3 or 4 limit cycles; once the ovals are plentiful and have a sort of inverted rounded pyramid shape (like mine above) it's a single scroll chaotic attractor. 

The famous double scroll chaotic attracot can have two appearances depending on your setup and equipment: the single chaotic attractor with some extra, lighter ovals underneath it; or sort of a figure 8 shape.



The circuit build is towards the bottom of this post.

Chua's Circuit is basically an oscillator that outputs waveforms that never repeat: chaos! Chua's Circuit is the simplest circuit that can output real chaos. Chaos in the form of a double swoosh set of circles on an oscilloscope screen. This waveform is called an attractor, which demonstrates chaos in a continuous time dynamical system...I like the idea of a waveform generator / oscillator that never repeats. A continuouly evolving, periodic output that never repeats: chaos!

What defines chaos in a system like this: extreme sensitivity to initial conditions; cause and effect are not proportional; and it is nonlinear.

This circuit will also display bifurcation: small, smooth changes lead to a sudden huge change in the system. Tuning a silent radio with a knob that you're barely moving, you keep trying to nudge the timing knob and you all of a sudden loud music starts playing. Or slowly as possible applying more pressure to a mousetrap until it springs shut. The straw that broken the camel's back.

We will make it less simple by replacing the supposedly hard to find, but definatly hard to choose thet correct value for inductor-with a gyrator circuit which acts as an ideal inductor. Just a couple extra cheap parts and it still works.



First off when looking at schematics to build your very own Chua Circuit you need to ignore 99% of the useless schematics out there. The schematics you'll see are for explaining this circuit, but not building it. Here is an example of the classic circuit...that doesn't tell you what you need to know:


You will see on the left side an "L". That denotes a simple 18mH inductor. These inductors are sort of hard to find. You could try and swap in and out various 18mH inductors with less than 30 ohms resistance and play around with that. They are pricey and might not be exactly the spec your circuit needs to operate. We will replace the "L" with a TL082 op-amp acting as a gyrator inductor simulator. It works like an inductor (in this circuit at least) and operates ideally. Simple! The gyrator is known as "The Fifth Linear Element" and basically couples voltage from one source to the current from a different source...and then does the same with the remaining devices' current and voltage. Once working I plan on replacing the gyrator with an actual inductor, since I just found a pile of different sized ones in a drawer. An actual inductor will add parasitic resistance that will have to be counteracted in the circuit.


You will see on the right side of the circuit "NR". That is Chua's Diode...except that there is no such thing as a Chua Diode. You can't buy one in a store, you have to make it. Luckily we can make our own Chua Diode using a second TL082 op-amp. 

There are versions of this circuit which leave the inductor and replace this Chua diode with a memristor, but it involves more potentiometers (knobs) than this way. You will soon learn of my hatred for wiring pots. Anyway Chua’s diode has nothing in common with  a diode; it has a non-linear voltage-current characteristic that makes the oscillator output “unrepeatable”.

Then we'll add in some resistors, capacitors (use metal film, not carbon, for best results), a couple of knobs (potentiometers), and a DC power supply instead of two 9v batteries oddly wired together in many of the circuit designs.


The only other fancy "thing" I've made has been a scanning tunneling electron microscope. This project is very similar in that it used op-amps and the x, y and z inputs on an old oscilloscope. Because this is low voltage you can use a PC computer-based oscilloscope without blowing stuff up-as long as it can handle 9vDC. However if you use a modern fancy standalone digital oscilloscope the swooshing waves of the the output (a Chaotic Attractor) turns into swarms of ugly dots that are really hard to interpret.


After the annoyance with the schematic above I became annoyed with the build schematics I found for one simple reason: they show 4 triangles that are op-amps. So, I assumed I needed four op-amps, but the TL082 is a double-op-amp: each black box with 8 legs (pins) sticking out actually has two triangles inside of it. Four triangles on the schematic = two TL082 op-amps. Nice.

Also, the schematic didn't label the triangles as being A/B pairs. Which I'm not used to. So, when I scribble out my diagrams I'm labeling them A and B. One A/B triangle pair is a single TL082. This will make total sense once you read the next few lines and check the pinouts.


So, the first thing to get straight is the op-amp circuits. While it might look like we'll be using four TL082 circuits we're really only using two of them!


Here's a simple pinout that is for a different op-amp, but which has the same pinout as a TLN082, but it's drawn better. It's much easier to see how each little TL082 microchip is actually a double op-amp. One amp is "A" triangle and the other is "B" triangle. Look at this and the build schematic will make sense:





You can see that there is an "A" op-amp, and a "B" op-amp.

A = pins 1, 2 and 3.
B = pins 5, 6 and 7.

Pins 48 power the entire TL082 (both A and B together).

Inverting inputs are negative (-) inputs.
Non-inverting inputs are positive (+) inputs.

So, in the schematics for Chua circuits (done without an inductor, using a gyrator instead) you will see four triangles. They represent the A & B portions of only two TL082 op-amp circuits.

You will see two triangles near each other, and two other triangles paired up on the other side of the schematic. Each pair of triangles near each other represents a single TL082!

You must treat each triangle as an A or B regarding pins. Decide which triangle in a pair is the "A triangle" and which is the "B triangle".

Anything that goes to the A triangle will only use pins 1, 2 and 3.
Anything that goes to the B triangle will only use pins 5, 6, and 7.
Pins 4 and 8 get the input from your power supply/batteries. It doesn't really matter which triangle those go to, but for simplicity we'll put the 4/8 power to the "A" triangles.

So, on the left side of the circuit where we are not using an inductor, but building a gyrator instead we have two triangles.



Left Side of Schematic-Gyrator Inductor Simulator


Triangle A is on the right:
Pin 1 = OUT A
Pin 2 = Inverting Input A (negative -)
Pin 3 = Noninverting Input A (positive +)
Pin 4 = Negative Power Supply input
Pin 8 = Positive Power Supply input

Triangle B is on the left:
Pin 7 = OUT B
Pin 6 = Inverting Input B (negative -)
Pin 5 = Noninverting Input B (positive +)

There, now you have all 8 pins on one of the TL082 op-amps wired up!




Right Side of Schematic-Chua Diode


Triangle A is on the right:
Pin 1 = OUT A
Pin 2 = Inverting Input A (negative -)
Pin 3 = Noninverting Input A (positive +)
Pin 4 = Negative Power Supply input
Pin 8 = Positive Power Supply input

Triangle B is on the left:
Pin 7 = OUT B
Pin 6 = Inverting Input B (negative -)
Pin 5 = Noninverting Input B (positive +)

There, now you have all 8 pins on your second TL082 op-amps wired up!

BREADBOARD WIRING

Wire Jumpers:

c18-c24
e18-f19
g19-g25
e21-f21
he21-h24
d23-d29
e29-f29
i26-i32
j28-j29
c25-c31
h32-h39
h42-h49
g48-g49
i48-i54
e49-f49
h52-h56
c48-c52
d49-d53
b51-b55
a55-a56
f56-f57
d3-g3
j50 to right power right outside
left power rail inner to b3
left power rail outer to a54
left power rail outer to a26
j50 to right power rail outer
j10 to right power rail inner
j51 to right power rail inner
left power rail outer to a10
j23 to right power rail inner
left power rail inner to b3
i3 to right power rail outer
left power rail outer to a54
left power rail outer to a26
j50 to right power rail outer
j10 to right power rail inner
j51 to right power rail inner
left power rail outer to a10
j23 to right power rail inner

TL082 Chips
Top of chip (with half circle dimple) pins 1 and 8: e23 and f23
Top of chip (with half circle dimple) pins 1 and 8: e51 and f51

Resistors
220 b10-b13
220 i10-i13
1k b21-b24
2.2k b25-b28
1k h25-h28
100 g26-g29
3.3k i39-i42
22k a48-a51
22k c53-c56
3.3k left power rail inner to a52
3.3k i39-i42
2.2k g50-g53
2.2k g54-g57
220 j53-j56

Capacitors (film metal not ceramic)
10nf j42 to right power rail outer
100nf a21-a25
100nf j26 to right power rail outer

LEDs
left power rail inner to to a13
j13 to right power rail outer

Batteries
9v Battery 1: red positive to c3 / black negative to c10
9v Battery 2: red positive to h10 / black negative to h3

Ground wire for oscilloscope probe ground clips
right power rail outer (last bottom, right hole)

Oscilloscope probe to leg of capacitor sticking out of hole j26
Oscilloscope probe to left of capacitor sticking out of hole j42

Potentiometers/Trim Pots
Pot 1: middle pin b31 / either other pin left power rail innter / 3rd pin unused
Pot 2: middle pin f39 / either other pin f42 / 3rd in unused

The left side of the breadboard usually has positive and negative...but in this circuit both columns are negative.

The right side of the breadboard is positve on inner column, negative on the outer column closest to the edge of the board.

10k linear potentiometers are recommended because they're easier to turn and are more precise. I used 10k trim pots with are less precise and have to be turned with a screwdriver--bad choice!




Connect the two halves of the circuit with some wires, knobs, outputs to oscilloscope or a USB PC computer-based oscilloscope (with a huge resistor on the outputs so you don't fry your computer or digital scope) and you're good to go. I have real oscilloscopes that are analog/tube and can handle high voltage inputs. Your digital and computer PC scopes might blow up if you try and put more than 3v into them. Read your specs. This thing basically has two 9vDC input spots that may total 18vDC if you mess things up.

As always my nemesis is the humble potentiometer (volume knob). On some projects you need to use all 3 pins, but some only 2...and the circuit schematics almost never tell you which. Is it a voltage divider (might be 3 pins) or is it a current adjuster (2 pins?) or is it a volume knob for audio (3 pins?) or a variable resistor (2 pins!)?

************On the most popular "fritzing" diagram for Chua circuits they show 3 wires going to the potentiometers but in the explanation 8 pages later they state that the third wire isn't wired to anything, it's just there to physically keep the pot from moving on the table as you spin the little knob!!!!!!!!!!!!!!!!! I'm so annoyed I spent so much time trying to figure out why/where that third wire was going to!!!! Use only two wires for your Chua circuit pots/knobs: the middle one and one on either side.


I had similar issues with my scanning electron microscope build: five pots labeled V1-V5 for variable resistor, and one of them showing connection to: ground, -negative voltage (which in a DC circuit is the same as the ground) and then the incoming wiper wire from ??? Ugh! Anyway, here we go:



PARTS


100 ohm x1

1k ohm x2

220 ohm x4

2.2k ohm x2

22k ohm x2

3.3k ohm x2

TL082 IC Op Amp Chips x2

LEDs x2

On/Off switch x1 (DPDT double pole/double throw 6 contact)

Potentiometers x2

9vDC power supply or two 9v batteries and wires.

Breadboard (or not).

Analog oscilloscope with two probes (or three if you use the "z" input on the back too). Or coax cables with fittings at one end (probably RG-58 BNC-connector 50ohm) to plug into analog scope inputs, and cut off the other ends to hook to the Chua Circuit. Or you could get some BNC female sockets and put them in your Chua's Circuit...but short lengths of coax wire with BNC connectors are super cheap on Amazon and eBay, so I buy them and cut them in half quite a bit: each coax cable cut in have gives me: two BNC to bare-wire cables. BNC plugs into oscilloscope or function generator or Geiger counter, etc. and the other end I solder to whatever circuit I'm building. Some of them were 75ohm, and some were even old 1970s cable TV wires that weren't marked. Whatever. 

Bits of wire, wire strippers, solder and iron if you're going to not use breadboard.

Two 1M resistors only if hooking up to digital oscilloscope or computer. Old analog oscilloscope don’t need them.

Just hook old analog oscilloscope probes to Capacitor 1 and Capacitor 2 (the two near each other) and then ground the probe ground clips to -9vDC (black) on the side rail of the breadboard.


This will be my first ever breadboard project, so I’ll be following Valentin Siderskiy’s instructions pretty much step-by step, but adding my own clarifying notes. Such as: you can leave a wire off of each potentiometer, they only need two in this circuit! Or the ever popular "I think LEDs are polarized so you have to stick them in the right way or they won't light up."



Also, you could easily (I think) leave off: 

On/off switch; 
Two LEDs; 
Resistors for the LEDs; 
Two more resistors that just smooth down the 5k pots to 2.5k--but then you'd need to probably buy actual 2.5k pots. Dumbing down 5k or 10k pots to only 2.5k makes it easier to make slight adjustments to the circuit while turning the knobs. 


Since this is my first breadboard I'm going to leave all that stuff in--when I point-to-point wire it up I'll be able to actually tell how to leave them off without breaking connections. I still think in "point-to-point" and not breadboard or circuit board layouts.

Running the circuit
I used a couple different oscilloscopes. One had 3 inputs: channel a and b (verticals) and a horizontal channel. I just used a and b and set the dial for A vs B to plot the voltages against eachother to get the single scroll chaotic attractor. This gave the same results as my older oscilloscope with a single vertical and single horizontal input (x vs y).

The slightest turn of the potentiometers resulted in HUGE changes to the image on the oscilloscope. This is called bifurcation. 

One of my potentiometers measured only 1.3k instead of 10k when tested on a multimeter: so I replaced it and immediately got better results. I'll probably invest in two full sized pots with convenient knobs to twist.

I had two old mismatched batteries, I'll add new ones.

When I unhook the oscillscope ground clips from the ground wire the whole circuit becomes very sensitive to hand movements. 
Bifurcation was observed: tiny, smooth changes to a paremeter (knob twist) results in huge changes to the output (dot turned into a chaotic attractor). Hysterisis (like my previous post about neon lamp bulbs) was observed: the spot where the chaotic attractor turns on is different from where it turns off--plus there's two knobs that influence eachother. 

Unplugging the inside leg of the 10nf capacitor gave an extremely small, single trace of the famous double scroll attractor. It was very low resolution: it looked like a number "8" written in Old English font! 

Here are some attractors that resulted when I changed one, then both of the pots from 10k to 5k:





Here are some sweet toroidal Class 1 Eigenvalue = 10 (sort of) attractors.







Here are some results after putting in two 5k pots, then replacing one of the batteries with a 9vDC power supply...but varying the DC voltages from around 1.3vDC to 10vDC:






Here is the shape you see right before the classic double scroll: I was playing with the voltage (I replaced one of the 9v batteries with a DC bench power supply and going from 1.3vDC to 10vDC. One of the IC chips was getting pretty warm though):


It's a homoclinic bifurcation, the periodic orbit grew units it collided with the saddle point.







Multiscroll attractor:









You can see how this double-double scroll attractor developed from multiple loops:


The Logusz Attractor (double-double or chaotic quad-attractor as I'm thinking of naming it)...although it's actually pretty close to what others have found as a projection of the Vc2 / IL plane or the Vc / IL1 plane. The literature on this and others:

Anshan, H. [1988] "A Study of the Chaotic Phenomena in Chua's Circuit," Proceedings of 1988 IEEE International Symposium on Circuits and Systems (Cat. No.88CH2458-8). IEEE. vol.1, pp.273-276.

Bartissol, P., Chua, L.O. [1988] "The Double Hook (Nonlinear Chaotic Circuits)," IEEE Transactions on Circuits & Systems, vol.35, no.12, pp.1512-1522.



Anshan in particular backs up what I discovered myself: op-amp voltage adjustments can lead to lots of new patterns and attractors. Of course he found this out in the 1980s--and I'm just playing around in my basement as something to do besides mow my lawn...it's nice to see my "weird" non-perfect, non-double scroll attractors have actual mathematical explanations in eigenvalues (weird math) but also voltages.














Great sources of information that I ripped off:


Professor Leon Chua, the inventor of this chaotic circuit.

"Jim" who made http://www.chaotic-circuits.com/

Valentin Siderskiy, Vikram Kapila and Aatif Mohammed of http://www.chuacircuits.com and published papers and a great Instructable post. Check out "Chua's Circuit for experimenters using readily available parts from a hobby electronics store".

"Chua’s Circuit for High School Students" by Gandhi, Gauruv., Muthuswamy, Bharathwaj2 and Roska, Tamas.

This list of awesomeness; hover your mouse over the titles and you can click and be rewarded with actual PDFs of the articles (and not just crummy citations): http://people.eecs.berkeley.edu/~chua/circuitrefs.html