Create fractals on your calculator! --------------------------------------------------------------------------- Of course, this method will work for both calculators and computers. I'll explain at the end how to make color plots on a computer. --------------------------------------------------------------------------- First I'm going to lecture a bit about theory. You can skip this part but it's not hard to understand and, in my opinion, really interesting. The basic idea about fractals is that they are self-repeating (or recursive), that is, they are made up of smaller versions of themselves. Hence the term fractal -- if you divide one up into smaller pieces, you see the same thing. Or perhaps you see certain patterns repeating. A consequence of this is that the circumference of a (two-dimensional) fractal object is infinite but its area is finite. If that's kind of hard to swallow, consider this popular analogy: the coastline of England as seen from a satellite has a particular degree of jaggedness to it. If you zoom in closer, you see the same degree of jaggedness -- the coastline never gets smoother. If it did, that would imply that the jaggedness seen from space is simply a phenomenon that results from being so far away. The coastline is always jagged, because there's always cliffs or rocks to break up its smoothness, continuing all the way down to the molecular level. This jaggedness seems to be intrinsic to the island regardless of scale. Therefore, England's coast is fractal. If you wanted to walk along the entire coast of England, you would never complete your journey if you took every minute jagged twist and turn. Which means that the circumference of England is infinite, though its surface area is clearly not. Other examples of fractal objects are clouds and landscapes. You could even say that the universe is fractal (galaxies orbit galaxies, stars orbit within galaxies, planets orbit stars, electrons orbit nuclei). In mathematics, people have figured out ways to construct imaginary fractal objects. The beauty of these objects (sometimes called fractal sets) is that they usually require a bare minimum of mathematical definition but yield an infinite amount of detail (information). Benoit Mandelbrot was one of the pioneers of computer-generated mathematical fractals. In particular, his set is a sort of dictionary of all previous fractal sets (known as Julia sets), so that by evaluating his set you could get information on the other sets, too. One last note before we move on: don't get the terms fractals and chaos mixed up too easily. What you're looking at when you make a fractal is not what most people would call an example of chaos. The latter means, loosely: the amplification of small variations in systems and the study of attractors within those systems. A famous example is a butterfly flapping its wings in Beijing, causing a storm in Texas. Very small changes in the weather get amplified very quickly because the weather is made up of small local weather phenomena, which are made up of yet smaller weather "units"... weather is fractal. But weather also moves, and since its overall "movement" is dependent on the movements of smaller weather units, a change in such a small unit (like the flapping of a butterfly's wings) can affect the entire world's weather system as the change is propagated and increased through larger weather units (first around Beijing, then China, then Asia, etc.). Of course, butterflies all over the world flap their wings every day, which at least partly is why the weather is so damn unpredictable (and why it's so freezing cold on this spring day as I write this). So the point I'm trying to make is that the Mandelbrot set is not chaotic. You can imagine the set as a snapshot of a chaotic process, like taking a satellite picture of the weather. The weather is chaotic but the picture isn't. But the picture, just like the weather, is fractal! "Enough already! Tell us how to make those damn fractal thingies!" No, no, wait! Don't bail out on me just yet. A little more mathematical gibberish and then I'll give you the code. The Mandelbrot set is plotted on the complex plane. If you're not familiar with this, remember that "i" is the square root of -1. A complex number is in the form a + bi, where "a" is the real part and "b" is the imaginary part. The complex plane is a simple Cartesian plane where we put "a" on the x-axis and "b" on the y-axis. So to plot the complex number 7 + 2i, we'd put a point at (7,2). Easy enough. Now, to make the set, we consider each point on the plane within -2 < a < 2 and -2 < b < 2 and make it go through a certain test. If it "passes" the test, we put a pixel at that point. If it doesn't, we don't. Then we move on to the next point. This collection of pixels makes up the image. The test is this: given the recursion z(next) = z^2 + c where c is the complex number at (a,b) and the inital value of z is c, does z stay bounded or does it go off to infinity? In other words, we find the next value of z by computing z^2 + c. We take this new value of z and plug it in again. We do this as many times as we need to find out whether z stabilizes at some point or sequence of points (called an orbit or attractor) or whether it shoots off into the distance. Given this information, the rest is elementary. You can see now why this algorithm is suited for calculators and computers: it's very simple and short but requires a long series of calculations. So long, in fact, that on a TI-81 it can take about a half hour for a plot of the entire set to six or seven hours for a detailed close-up plot. Since I bet you're anxious (well, by now you might be bored to tears...), here's the code. I'll explain the details and how to adapt it to your calculator/computer at the end. 1 ClrDraw 2 screenwidth = 96 3 screenheight = 64 4 x1 = -screenwidth/2 5 y1 = -screenheight/2 6 x2 = screenwidth/2 7 y2 = screenheight/2 8 l = (x2 - x1)/screenwidth 9 m = (y2 - y1)/screenheight 10 u = 0 11 if y1 < 0 and y2 > 0 12 u = 1 13 xmin = x1 14 ymin = y1 15 xmax = x2 16 ymax = y2 17 xscl = 0 18 yscl = 0 19 All-Off 20 DispGraph 21 i = x1 22 label 1 23 j = y1 24 if y1 < 0 and y2 > 0 25 j = 0 26 label 2 27 f = ((-2 + 4i)/screenwidth) 28 g = ((2 - 4j)/screenwidth) 29 x = f 30 y = g 31 n = 1 32 label 3 33 s = (x^2 - y^2 + f) 34 t = (2xy + g) 35 z = s^2 + t^2 36 if z > 4 37 goto 4 /* note: this refers to label 4, not line 4 */ 38 x = s 39 y = t 40 n = n + 1 41 if n <= 10 42 goto 3 43 PT-On(i,j) 44 if u = 1 45 PT-On(i,-j) 46 label4: 47 j = j + m 48 if j <= y2 49 goto 2 50 i = i + l 51 if i <= x1 52 goto 2 53 end First, I've left in all the lines specific to the TI-81 like ClrDraw, All-Off, etc. These are lines 1 and 13-20. If you have a TI-81 or TI-82, you can copy this program directly (press 2nd-PRGM and go into edit mode) except for the following modificatios. I wrote the variable assignments in the more customary "new = old" format. So an assignment like x = 5 would have to become 5->x on the TI's. Similarly, since the TI-81 and -82 programming language doesn't support "and" in an "if" statement, if y1 < 0 and y2 > 0 (statement) in lines 11 and 24 would have to become if y1 > 0 goto x if y2 > 0 (statement) label x (actually, Chris Gibrich told me that the TI's do have an implementation for "and", so I'll update this info ASAP) Finally, the TI-81 and -82 use one-character variable names. Replace long variable names like "screenwidth" with the character of your choice. If you're implementing this on a different calculator, consult your manual to see what the equivalent instructions for the given program are. Now for the code. Lines 2 and 3 assign the width and height of your calculator (or computer) screen. On the TI-81 and -82, this is 96 pixels by 64 pixels. Read the technical specifications in your manual to customize this for a different calculator. Lines 4 through 7 set the bounds of the graph area. Remember that the bounds of the Mandelbrot plot in the complex c-plane are -2 < a < 2 and -2 < b < 2. The program is set up to treat -screenwidth/2 as the left bound (a = -2), screenwidth/2 as the right bound (a = 2), -screenheight/2 as the lower bound (b = -2) and screenheight/2 as the upper bound (b = 2). (x1,y1) and (x2,y2) define the lower left and upper right bounds of the plane you actually want to see. If you keep x1, y1, x2 and y2 as they are given in the above program, you get a plot of the entire set. If you want to zoom in, you need to change these values. Let's say for simplicity's sake that instead of plotting -2 < a < 2 and -2 < b < 2, you just want -2 < a < 0 and 0 < b < 2 (i.e., quadrant II). You need to change x1, y1, x2 and y2 accordingly: x1 = -screenwidth/2 y1 = 0 x2 = 0 y2 = screenheight/2 Now just quadrant II will fill the screen when you run the program. Be aware that you can assign any silly values you want to the x1, y1, x2 and y2 coordinates, but to get the right aspect ratio (that is, to avoid squished or stretched plots), the ratio between the width and height of the box defined by x1, y1, x2 and y2 must be the same as the ratio of screenwidth to screenheight (in this case, 96/64 = 1.5). It takes a little work to get everything right, but you can zoom in as closely as you want. Just remember that rendering times get very slow as you zoom in. Moving on... lines 8 and 9 determine the distance between each point on your calculator. If you copy the program as above, l and m will both be one. This should make sense to you because in this setup, the plot box is the same size as the total plot area (96 by 64). If we were to go with the example above where we just display quadrant II (which is -2 < a < 0 and 0 < b < 2 in the complex plane and -48 < x < 0 and 0 < y < 32 in the calculator's plot plane), the l and m increments between points would be 0.5 since we're zooming into one fourth of the original image, which gives us twice the detail, hence the distance between the points we evaluate is halved). If I just lost you, don't worry. It's not essential information. In lines 10-12, we assign a variable "u". If the plot box crosses the x-axis, that is, y1 is negative and y2 is positive, then we assign "u" to one, otherwise we assign it to zero. This is done so we can mirror the image above the x-axis down to below the x-axis. The Mandelbrot set has symmetry about the x-axis, so instead of plotting two points (x,y) and (x,-y), we just evaluate (x,y) and copy it to (x,-y). Later in the program, "u" will be used to carry out the mirror operation (if necessary). Lines 13-20 are, as mentioned, specific to the TI-81 and -82. In lines 21-26, we set up a loop structure so that (i,j) evaluate each point in the plot area. Note that the variable "i" here has nothing to do with "i", the square root of -1. Lines 27-46 contain the actual evaluation and plotting part. In lines 27-30, the plot coordinates i and j are transformed into coordinates on the complex plane. Lines 31-42 is the iterative loop that plugs in z into z^2 + c as many times as specified in line 41. Lines 33-35 are algebraic manipulations to carry out the operation z(next) = z^2 + c given the real ("f") and imaginary ("g") coordinates. Line 36 checks to see whether z is bounded or not. We're going to keep things simple and just ask whether it's bigger than 4 or not. This works pretty well. If it's bigger than 4, we decide it's not bounded and skip the rest of the iterative loop. If not, we keep going. Once the loop is finished (again, determined by the number in line 41... more iterations yield more detailed plots) and z is smaller than 4, we put a point at (i,j) (and its mirror point, (i,-j), if necessary). That's it! If you're doing this on a computer and want to put in color, just figure out how long z stays bounded (i.e., < 4) before it becomes unbounded and then color the pixel accordingly. You can simply use the variable "n" in the above program for this. Each color gradation then represents a difference in the number of iterations during which the z was less than 4. If you're ambitious, you can expand the program to making zooming in more efficient (a good improvement would be that, instead of defining the box, all you need to do was give a center point and zoom factor) or figure out a shading scheme on the calculator similar to the color scheme I just described. Have fun! If you have problems or comments, send me some e-mail: p-baer@ux5.cso.uiuc.edu. --------------------------------------------------------------------------- Back to the home page.