Home
Help
Resources
Extensions
FAQ
NetLogo Publications
Donate

Models:
Library
Community
Modeling Commons

Beginners Interactive NetLogo Dictionary (BIND)
NetLogo Dictionary

User Manuals:
Web
Printable
Chinese
Czech
Farsi / Persian
Japanese
Spanish

# Mandelbrot

 If you download the NetLogo application, this model is included. You can also Try running it in NetLogo Web

## WHAT IS IT?

This model draws a mathematical object called the Mandelbrot set, named after its discoverer, Benoit Mandelbrot. It demonstrates an interesting technique for generating the design as well as providing a nice example of hill climbing.

A number of fractal generating turtles do a random walk across a complex plane, computing one additional iteration of F(Z) = Z<sup>2</sup> + C each time they cross a patch, where C is the coordinates of the patch, and Z is a complex number from the result of the last iteration. A count is maintained of the number of iterations computed at each patch until the patch reaches its maximum iterations. This count is then translated into a color, giving the Mandelbrot set its distinctive look.

An interesting way to view the emerging set is that you are looking straight down on one of the Hawaiian Islands. The center is extremely high (infinitely so, in fact), simply because no fixed number of iterations at these points will cause the associated complex number to reach a pre-determined maximum. The edges of the set are steeply sloped, and the "sea" around the set is very shallow.

## HOW IT WORKS

The Mandelbrot set is a set of points on the complex plane. It is the set of points for which the iterated map F(Z) = Z<sup>2</sup> + C with a seed of 0 remains finite. In other words the orbit of 0 under the map stays finite. As an example of computing the orbit, start with F(0), then next in the orbit is F(F0)), then F(F(F0)))....Most complex numbers under this map with seed 0 will produce infinite orbits. Some orbits tend towards a constant, some are cyclic, and some chaotic, all of which produce points in the Mandelbrot set as they are not infinite. To determine if a point C (that is with coordinates (x, y) in the complex plane) is in the Mandelbrot set, apply the map F(Z) = Z<sup>2</sup> + C to it iteratively, starting from Z = 0. If when applied iteratively, it diverges within a fixed limit of iterations (in our case 256) then we treat it as infinite and exclude it from the Mandelbrot set.

The way this model implements the algorithm, 1000 turtles are created and do a random walk in the complex plane. When they land on a patch, they check that it is within the modulus limit (of 2) and that it has not yet maxed out its iterations. If these conditions hold, they perform another iteration on the patch, increase the iteration counter of the patch, turn yellow and take a random step. If the new patch has an iteration counter higher than one they've seen before, they jump to a nearby patch for the next iteration. If the new patch isn't higher than they turn around and face the opposite direction. In this way the turtles are "climbing" the gradient of the iterations, getting closer and close to the overall top of the hill. The purpose of the jump is to make sure the turtles don't get stuck on the top of a local maximum, but instead search the space for a global maximum "height" of the hill. The patches are colored by the value of the counter, so patches that have successfully passed the Mandelbrot test more times have a higher NetLogo color and those that have passed it less times, have a lower NetLogo color.

Complex Numbers In case you are not familiar with complex numbers, here as an introduction to what they are and how to calculate with them.

In this model, the world becomes a complex plane. This plane is similar to the real or Cartesian plane that people who have taken an algebra course in middle school or high school should be familiar with. The real plane is the combination of two real lines placed perpendicularly to each other. Each point on the real plane can be described by a pair of numbers such as (0,0) or (12,-6). The complex plane is slightly different from the real plane in that there is no such thing as a complex number line. Each point on a complex plane can still be thought of as a pair of numbers, but the pair has a different meaning. Before we describe this meaning, let us describe what a complex number looks like and how it differs from a real one.

As you may know, a complex number is made up of two parts, a real number and an imaginary number. Traditionally, a complex number is written as 4 + 6i or -7 - 17i. Sometimes, a complex number can be written in the form of a pair, (4,6) or (-7,-17). In general, a complex number can be written as a + bi or (a,b) in the other way of writing complex numbers, where both a and b are real numbers. So, basically a complex number is two real numbers added together with one of them multiplied by i. You are probably asking yourself, what is this i? i is called the imaginary number and is a constant equivalent to the square root of -1.

Getting back to the complex plane, it is now easier to see, if we use the paired version of writing complex numbers described above, that we let the real part of the complex number be the horizontal coordinate (x coordinate) and the imaginary part be the vertical coordinate (y coordinate). Thus, the complex number 5 - 3i would be located at (5,-3) on the complex plane. Thus, since the patches make up a complex plane, in each patch, the pxcor corresponds to the real part and the pycor corresponds to the imaginary part of a complex number. A quick word on complex arithmetic and you will be set to understand this model completely.

Two complex numbers are added or subtracted by combining the real portions and then combining the imaginary portions. For example, if we were to add the two complex numbers 4 + 9i and -3 + 11i, we would get 1 + 20i, since 4 - 3 = 1 and 9 + 11 = 20. If we were to subtract the first number from the second number, we would get -7 + 2i, since -3 - 4 = -7 and 11 - 9 = 2. Multiplication is a bit harder to do. Just remember three things. First, remember that i * i = -1. Second, be sure to follow the addition and subtraction rules supplied above. Third, remember this scheme First Outside Inside Last or FOIL for short. In other words, you multiply the first parts of each number, add this to the product of the outside two parts of each number, add this to the product of the inside two parts of each number, and add this to the product of the last two parts of each number. In general, this means given two complex numbers a + bi and c + di, we would multiply the numbers in the following manner:

(a * c) + (a * di) + (bi * c) + (bi * di) = ((a * c) - (b * d)) + ((a * d) + (b * c))i

If we were to multiply the same two numbers from above, we would get -12 + 44i - 27i - 99 = -111 + 17i, since 4 * -3 = -12, 4 * 11i = 44i, 9i * -3 = -27i, and 9i * 11i = -99.

## HOW TO USE IT

Click on SETUP to create NUM-TURTLES fractal generating turtles, place them randomly, and scale the 101,101 world to approx -1 to 1 on both the real and complex axes.

The slider SCALE-FACTOR scales the fractal so that you can see more or less of it. The higher the value, the less of the entire fractal you will see. Be aware that you sacrifice resolution for the price of being able to see more of the fractal.

The slider THROW determines the range the turtles will jump if they're on top of a hill where the number of iterations is locally maximum.

To first try the model, start the slider THROW at 0, press the GO button. Note that the system seems to stall, with each turtle "stuck" on a local maximum hill.

Changing THROW to 9 will "throw" each turtle a distance of 9 each time they reach the top of a hill, essentially giving them a second chance to climb an even greater hill. The classic Mandelbrot shape will begin to appear fairly quickly.

## THINGS TO NOTICE

Notice that the "aura" around the Mandelbrot set begins to appear first, then the details along the edges become more and more crisply defined. Finally, the center fills out and slowly changes to purple.

Notice how different values for THROW change the speed and precision of the model.

## THINGS TO TRY

Try running the model with different values for NUM-TURTLES. What differences do you see?

Experiment with different values of SCALE-FACTOR. What differences do you see?

Experiment to see what real numbers are in the Mandelbrot set.

You might also think about changing the code so as to adjust the viewport in the plane, to allow for a larger picture (although the smaller sized picture might look better and emerge quicker).

Notice also what happens when you turn off climbing and/or wiggling.

Change the patch size and world size. What differences do you see?

## EXTENDING THE MODEL

Change the color code to experiment with different visual effects. Consider using the HSB or RGB color schemes.

Try to produce some of the other complex sets --- the Julia set for instance. There are many other fractals commonly known today. Just about any book on them will have several nice pictures you can try to duplicate.

One of the coolest things about the Mandelbrot set is as you zoom in, you start see all sorts of additional structure. Try using the LevelSpace NetLogo extension to allow users to click on a point of the set and open a new model that zooms in on just that point of the set.

## NETLOGO FEATURES

To accomplish the hill climbing, the code uses `current-count` and `previous-count` turtle variables, comparing them to one another to establish a gradient to guide turtle movement. The goal of each turtle is to move up the emerging gradient, "booting itself up" to the ever growing center of the set.

Note that complex arithmetic is not built in to NetLogo, so the basic operations needed to be provided as NetLogo procedures at the end of the code. These complex arithmetic routines are also used in other fractal calculations and can be tailored to your own explorations.

The count of iterations computed on a patch is set to the pcolor of the patch, making use of NetLogo's color scheme. Since the counter ranges from 0 to 256, the colors range from gray to red to orange to brown to yellow to green to lime to turquoise to cyan to sky to blue to violet to magenta to pink, back to gray then to red, to orange to brown to yellow to green to lime to turquoise to cyan to sky to blue and maxes out at violet.

## CREDITS AND REFERENCES

You may find more information on fractals in the following locations: This site offers an introduction to fractals: http://web.cs.wpi.edu/~matt/courses/cs563/talks/cbyrd/pres1.html.

An introduction to complex mathematics and the Mandelbrot set. https://web.archive.org/web/20160131061433/http://www.ddewey.net/mandelbrot/

An introductory online textbook for Complex Analysis. (Note: This is a college level text, but the first chapter or so should be accessible to people with only some algebra background.) https://people.math.gatech.edu/~cain/winter99/complex.html

The Fractal Geometry of Nature by Benoit Mandelbrot

## HOW TO CITE

If you mention this model or the NetLogo software in a publication, we ask that you include the citations below.

For the model itself:

Please cite the NetLogo software as: