Improved Mandelbrot Set generator
Michael Castleman, mlc@sri.kbt.com

What is MANDEL2?
----------------
MANDEL2 is an `improved' program to draw the Mandelbrot set on the TI82. It's
really slow, but it works.

How do I run MANDEL2?
---------------------
First, set the window vars. If you don't know what you're doing, I recommend
Xmin=-2, Xmax=1.25, Ymin=-1.5, and Ymax=1.5 Then, run MANDEL2, and find
something else to do for a while. In about 90 minutes, you can look at the
Mandelbrot set.

What *is* the Mandelbrot set?
-----------------------------
(the following answer was lifted from "An Introduction to the Fractint 
Formula Parser" by Bradley Becham (74223.2745@compuserve.com))

   =====================================================
   6.0  SOME BASICS -- A WALK THROUGH THE MANDELBROT SET
   =====================================================

   6.1  COMPLEX NUMBERS AND THE COMPLEX PLANE
   ------------------------------------------
   First, a disclaimer: This document is not intended to be a complete
   course on complex math. If you want to learn more about complex numbers,
   find a good algebra text.  FRACTAL CREATIONS also has a good summary of
   the math involved here.  But even if you are unfamiliar with complex
   numbers, read on.  Don't be intimidated by the word "complex"!

   Let's go over some of the fundamental concepts that you'll need to get
   started.  Since you already have a copy of Fractint, you have
   undoubtedly spent some time exploring the Mandelbrot set (M-set), easily
   the most famous fractal.  By reviewing some of the details about how
   this fractal is generated, you'll be better equipped to imagine new
   varieties of fractal formulas.

   This fractal is called a "set" because it is a set of points on a
   two-dimensional plane, somewhat like the graphs you probably had to draw
   in your algebra classes.  The Mandelbrot set exists in the  "complex
   plane", so-called because it is composed of complex numbers.

   You should know that a complex number has two parts: the real and the
   imaginary.  Just as the real number system is the union of the rational
   and the irrational number sets, so the complex number system is a union
   of the real and the imaginary numbers.  An imaginary number is any real
   number multiplied by the square root of -1.  This square root of -1 has
   a special name: i. So a complex number which had a real component 8.5
   and an imaginary component 3.2 could be written as 8.5 + 3.2i, or in
   parser notation as (8.5,3.2).  And because the reals are a subset of
   the complex numbers, any real number is also a complex number; that is,
   2 = 2 + 0i or (2,0).

   You can perform arithmetic with complex numbers; addition, subtraction,
   multiplication and division are all possible, and follow the rules of
   basic algebra with 'i' being treated as a variable.  Fractint also
   supports exponents (X^Y means X to the power of Y) and a variety of
   functions that operate on complex numbers, such as sin(), tan(), etc.
   I won't belabor this subject further for now, except to point out that
   when a complex number is operated on mathematically, both the real and
   the imaginary parts of the number may change; this concept is important
   in the discussion that follows.  (A further discussion of complex
   arithmetic can be found in section 11.5, "Dissecting A Formula With
   Algebra", and functions are described in section 7.2.5, "Functions".)

   In the complex plane, the horizontal axis corresponds to the real number
   line while the vertical axis corresponds to the imaginary number line.
   Any particular complex number, therefore, can be plotted as a point on
   the plane, and any point on the plane has a complex number that
   corresponds to it.  The real part of the number determines the
   horizontal placement of the point, and the imaginary number determines
   the vertical.  The origin of the graph (the place where the axes cross)
   is 0 + 0i.


   6.2  THE MANDELBROT SET
   -----------------------
   As a prelude to examining formulas, let's look at the processes involved
   in deciding whether a particular point on the complex plane belongs to
   the M-set.  Since the concepts involved are somewhat abstract, we'll try
   to create an analogy that is easier to visualize, and talk in very
   general terms at first.

   Imagine a circle drawn on the ground, with a little ball sitting in the
   center.  We'll pick a spot on the ground, somewhere within the circle,
   and call that the "test point".  Now we will start moving the ball
   in discrete steps according to a set of specific rules (which we won't
   describe yet) and watch the path that the ball takes.

   The first step always moves the ball over to our test point.  The second
   step moves it to a different location, and the third step to yet another
   location.  We'll keep applying the rules of movement, over and over,
   calculating a new position for the ball each time, and counting the
   number of moves we make.

   If we try this process for several different test points, we will see
   something very interesting.  For some test points, the ball seems to
   settle into a fairly predictable path, something like the orbit of an
   object in space -- it moves from spot to spot, but it never strays
   outside of the circle drawn on the ground.  For other test points, the
   ball may move around within the circle for a while and then exit.  And
   for some test points, the ball leaves the circle after very few moves.

   Now let's try to categorize the different test points, according to the
   behavior of the moving ball.  If the ball never leaves the circle, we'll
   color the test point blue, but if the ball *does* leave the circle then
   we'll give the test point a different color, based on the number of
   steps required make the ball cross the boundary.  If you did this for
   enough test points, an image of the Mandelbrot set would appear!

   Let's move beyond our analogy now and get more specific.  Instead of the
   ground, visualize the complex plane, and instead of a ball, visualize a
   moving point called 'Z'.  Now picture a circle on the plane, centered on
   the origin, with a radius of 2.

   First, we'll choose a point to test; let's say 0.2 + 0.5i.  Next we must
   define two complex variables, Z and C, such that Z = 0 + 0i and C = the
   value of the test point, ie C = 0.2 + 0.5i.

   Then the following algorithm is iterated (repeated over and over):
   Calculate the value of Z^2 + C, and then place the result in Z.  Since Z
   has a new value, find the point on the complex plane that corresponds to
   Z, and then check to see if the distance between Z and the origin
   exceeds 2.  If the distance is greater than 2 (Z is outside of the
   circle) then the test point is *not* in the Mandelbrot set, and you may
   stop calculating values for Z. But if Z remains in the circle, we move
   back to the top of the loop and calculate a new value for Z and check
   it again.

   In our example, if we start with Z = 0 + 0i and C = 0.2 + 0.5i, after the
   first time through the loop we now see that Z = 0.2 + 0.5i.  Since this
   falls within the "bailout" circle we will calculate again, with the
   result that now Z = -0.01 + 0.7i.  The next iteration ends with Z
   holding the value -0.2899 + 0.486i.

   We could repeat this process over and over, noting that Z shifts its
   position with each iteration and yet never exits the bailout circle.

   But because our time and patience have limits, we couldn't (and wouldn't
   want to) repeat the experiment an infinite number of times!  This is
   where the value for maximum iterations, set on Fractint's <X> menu,
   comes in.  The default value for maximum iterations is 150.  This means
   that if the program goes through the iterated loop 150 times, and Z has
   never strayed outside the bailout circle, Fractint *assumes* that the
   test point (C) is indeed part of the set, colors it accordingly, and
   moves on to another test point.

   Now what if the test point had the value 1.5 - 1.2i?  After the first
   iteration, Z = 1.5 - 1.2i, which is still barely within the bailout
   circle.  After the second iteration, Z = 2.31 - 4.8i.  This time Z has
   strayed out of the circle, so the test point is *not* part of the M-set.
   Because the bailout condition has been met, Fractint stops iterating the
   formula and colors the test point.  By default, Fractint chooses a color
   based on the number of iterations required to make Z exit the bailout
   circle, but this can be changed by various options on the Fractint
   menus.

   I have mentioned two conditions that cause Fractint to stop looping
   through the formula: 1) the bailout condition is met, and 2) the maximum
   number of iterations has been reached.  There is one other condition
   that can cause Fractint to stop iterating: periodicity.  If Fractint
   detects that Z has fallen into a periodic loop, repeating the same
   values over and over without leaving the bailout circle, it reasonably
   assumes that Z will *never* exit the circle and stops iterating even
   though the maximum number of iterations may not have been performed yet.
   This is one of the reasons that Fractint is so much faster than other
   fractal programs you may have tried.

   We're almost there, but before Fractint can create a picture of the
   Mandelbrot set it must settle a couple of problems, both of which have
   to do with infinity.

   First, you should see that in the complex plane there is an infinite
   number of points.  Obviously Fractint can't test them all.  So, it
   chooses a subset of the points, defined by the corners of your zoom box,
   and only considers points within that box.

   But even within that box, there is an infinity of possible points to
   test.  Here, the resolution of your computer display is used to resolve
   the problem.  Remember that a picture on your screen is composed of
   little dots called pixels.  Fractint chooses points on the plane that
   correspond to the locations of the pixels, and only tests those points.
   (It can create images at a higher resolution than your display via the
   "disk-video" modes, but we won't go into that.)  One point per pixel is
   enough.

   So now we have a finite number of points to test.  Fractint moves from
   pixel to pixel, finding the value on the complex plane that corresponds
   to each pixel and performing the test loop.  Pixels are colored dark
   blue (by default) if they are deemed part of the M-set, and a different
   color (normally based on number of iterations needed to exit the bailout
   circle) if they are not.
(end quote)

What makes MANDEL2 better than other Mandelbrot set drawers for the TI82?
-------------------------------------------------------------------------
First off, MANDEL2 knows that the Mandelbrot set is horizontally mirrored;
that is, the top and bottom halves of the image look exactly the same.
MANDEL2 takes advantage of this to cut processing time to slightly over half
of what it would be on other Mandelbrot set drawers, assuming your image
contains the horizontal axis. Also, MANDEL2 takes four passes to draw the
image. While this doesn't speed up the calculation time, it allows you to
get a rough estimate of what the image is going to look like sooner.

Memory Used
-----------

        Type Name                       Amt
        ---- -------------------------- ---
        prgm MANDEL2                    204
        real A,B,C,E,H,I,X,Y   8 * 15 = 120
                                        ---
                                        324

References
----------
An early version of this program was based on the BASIC program on page 202
of _Dictionary_of_Computer_Terms:_3rd_Edition_, by Douglas Downing and
Michael Covington.
If you wish to be able to draw Mandelbrot Sets and tons of other cool stuff
on your PC, I suggest Fractint, by the Stone Soup Group. You can find it all
over the place. Also, it's much, much quicker than MANDEL2; in 320x200x256
mode, it can draw the first zoom in about 10 seconds on my 386.
