Showing posts with label programming. Show all posts
Showing posts with label programming. Show all posts

Friday, January 18, 2019

Mandelbrot BASIC Program



Mandelbrot BASIC Program






Here's another cool bit of code to cut and paste into BASIC emulator.

Same as previous post: download and install a BASIC emulator onto your computer, and paste the code in. I used "PC-BASIC" made by Rob Hagemans because it is one of the few emulators that also emulates graphics! 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   https://sourceforge.net/projects/pcbasic/

PC BASIC 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 type RUN and Mandelbrot stuff appears.


There are an infinite number of fractions between any two whole numbers: between 1 and 2 there are 1/2, 1/4, 1/8, 1/1000, 1/2304049904, 1/9994848483292929, etc. The dark set are the Mandelbrot numbers. The colors are numbers outside the set, blasting toward infinity.

Zoom in for a closer look, and there is always a ton of similar detail to see: fractals!

Here's the code to cut and paste (remeber to use F11 + V to paste once you're inside PC BASIC):


10           DEFSNG A-Z
20           SCREEN 1: KEY OFF
30           MAXDWELL = 150
40           NUMCOLORS = 4
50           NUMROWS = 100
60           NUMCOLS = 100
70           YOFFSET = 1
80           XOFFSET = 1
90           INPUT "LOWER LEFTHAND CORNER, REAL PART"; ACORNER
100         INPUT "LOWER LEFTHAND CORNER, IMAG. PART"; BCORNER
110         INPUT "LENGTH OF SIDE"; SIDE
120         CLS
130         COLOR 1
140         LINE (0, 0)-(NUMCOLS + XOFFSET, 0)
150         LINE (NUMCOLS + XOFFSET, 0)-(NUMCOLS + XOFFSET, NUMROWS + YOFFSET)
160         LINE (NUMCOLS + XOFFSET, NUMROWS + YOFFSET)-(0, NUMROWS + YOFFSET)
170         LINE (0, NUMROWS + YOFFSET)-(0, 0)
180        LOCATE 17, 1
190         PRINT "PERCENTAGE COMPLETE = 0"
200         PRINT "DWELL FOR LAST PIXEL = 0"
210         PRINT "LARGEST DWELL = 0"
215         PRINT "MAXDWELL ="; MAXDWELL
220         PRINT "REAL PART = "; ACORNER
230         PRINT "IMAGINARY PART = "; BCORNER
240         PRINT "SIDE =  "; SIDE
250         HIGHDWELL = 0
260         GAP = SIDE / NUMROWS
270         AC = ACORNER
280         FOR X = XOFFSET TO NUMROWS - 1 + XOFFSET
290         AC = AC + GAP
300         BC = BCORNER
310         FOR Y = YOFFSET TO NUMCOLS - 1 + XOFFSET
320         BC = BC + GAP
330         AZ = 0
340         BZ = 0
350         COUNT% = 0
360         SIZE = 0
370         WHILE (SIZE < 4) AND (COUNT% < MAXDWELL)  
380         TEMP = AZ * AZ - BZ * BZ + AC
390         BZ = 2 * AZ * BZ + BC
400         AZ = TEMP
410         SIZE = AZ * AZ + BZ * BZ
420         COUNT% = COUNT% + 1
430         WEND
440         COLOR 1
450         LOCATE 18, 23  
460         PRINT COUNT%; " ";
470         IF (COUNT% < MAXDWELL) AND (COUNT% > HIGHDWELL) THEN HIGHDWELL = COUNT%: LOCATE 19, 16: PRINT HIGHDWELL
480         IF COUNT% = MAXDWELL THEN PSET (X, NUMROWS - Y + 1), 0 ELSE PSET (X, NUMROWS - Y + 1), COUNT% MOD (NUMCOLORS - 1) + 1
490        NEXT Y
500         LOCATE 17, 22
510         PRINT 100 * X /NUMCOLS; " ";   
520         NEXT X
530         AS = INPUT$(1)            

                

                       
Once you run it you'll be asked to input three numbers. Here's the starter version:

-2
-2
4

these three inputs will give you the same Mandbrot as above.



Other numbers suggested are:
-.114
.917
.017

You end up zooming into the Mandelbrot, but since it's like fractals it looks very similar (but never the same). Sort of like the chaos in the last post's bit of code.

You can zoom in too much and get crazy numbers like 1.2453245E-9 in the readouts. If you've got the time and computing power you can try this:

10 DEFDBL A-Z




This Mandelbrot code really grinds the hard drive as it works. I used the default 20 Line of Screen 1. You can up the ante for more colors and better resolution by changing the lines, same as last post. Each variation increases colors and/or resolution/and or size but always makes it take longer to actually render onscreen:

20 SCREEN 7 : KEY OFF
40 NUMCOLORS = 16


Lots more colors:

20 SCREEN 13 :KEY OFF
40 NUMCOLORS = 256


Here's one that gives you 16 colors but increases the image size from 100 rows and columns to 200:

20 SCREEN 9 :KEY OFF
40 NUMCOLORS = 16
50 NUMROWS = 200
60 NUMCOLS = 200


Like in the last post, if everything goes haywire, it's probably because your graphics card can't handle this program...even though it's like two decades later chaos, infinities, bifurcations and fractals use a lot of computing power. Plus, you're going through a BASIC emulator which is going to slow things down too.

How slow? Here's a video I took with Line 20 Screen set to only "1" and the colors limited to only 4. The little window is only 100 rows x 100 columns. Look how slow it goes!









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.