# Mandelbrot ### 1 collaborator Uri Wilensky (Author)

### Tags

mathematics

Tagged by Reuven M. Lerner over 9 years ago

Model group CCL | Visible to everyone | Changeable by group members (CCL)
Model was written in NetLogo 5.0.4 • Viewed 346 times • Downloaded 32 times • Run 1 time Download this modelEmbed this model

## 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 generation turtles do a random walk across a complex plane, computing one additional iteration of F(Z) = Z2 + 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 before the complex number at that patch reaches a maximum. 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

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 could 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 generation turtles, place them in the middle of the world (at complex coordinate (0,0)), and scale the 101,101 world to approx -1 to 1 on both the real and complex planes.

To start the calculation, 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 7 will "throw" each turtle a distance of 7 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.

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.

## 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 black.

Notice how different values for THROW change the speed and precision of the project. Also, try running the model with different values for NUM-TURTLES.

## THINGS TO TRY

It's fairly easy to hack at the NetLogo code to change the scale factor --- it's set as the global variable `factor`. You might also think about adjusting the viewport in the plane, to allow for a larger picture (although the smaller sized picture might look better and emerge quicker).

You might also play with the colors to experiment with different visual effects.

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

## EXTENDING THE MODEL

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.

## 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 routines 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.

## CREDITS AND REFERENCES

This site offers an introduction to fractals.
http://www.cs.wpi.edu/~matt/courses/cs563/talks/cbyrd/pres1.html

An introduction to complex mathematics and the Mandelbrot set.
http://www.olympus.net/personal/dewey/mandelbrot.html

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.)
http://www.math.gatech.edu/~cain/winter99/complex.html

The Fractal Geometry of Nature by Benoit Mandelbrot

## HOW TO CITE

If you mention this model in a publication, we ask that you include these citations for the model itself and for the NetLogo software:

• Wilensky, U. (1997). NetLogo Mandelbrot model. http://ccl.northwestern.edu/netlogo/models/Mandelbrot. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL.
• Wilensky, U. (1999). NetLogo. http://ccl.northwestern.edu/netlogo/. Center for Connected Learning and Computer-Based Modeling, Northwestern Institute on Complex Systems, Northwestern University, Evanston, IL. This model was created as part of the project: CONNECTED MATHEMATICS: MAKING SENSE OF COMPLEX PHENOMENA THROUGH BUILDING OBJECT-BASED PARALLEL MODELS (OBPML). The project gratefully acknowledges the support of the National Science Foundation (Applications of Advanced Technologies Program) -- grant numbers RED #9552950 and REC #9632612.

This model was converted to NetLogo as part of the projects: PARTICIPATORY SIMULATIONS: NETWORK-BASED DESIGN FOR SYSTEMS LEARNING IN CLASSROOMS and/or INTEGRATED SIMULATION AND MODELING ENVIRONMENT. The project gratefully acknowledges the support of the National Science Foundation (REPP & ROLE programs) -- grant numbers REC #9814682 and REC-0126227. Converted from StarLogoT to NetLogo, 2001.

Click to Run Model

```turtles-own
[
previous-count  ; the patch color of the previous patch- used in the turtles' movement
]

patches-own
[
c-real       ; real portion of the constant complex number c
c-imaginary  ; imaginary portion of the constant complex number c
z-real       ; real portion of the complex number z
z-imaginary  ; imaginary portion of the complex number z
counter      ; keeps track of the color that the patch is supposed to be
]

;;; Initialization Procedures

to setup
clear-all
setup-turtles
setup-patches
reset-ticks
end

to setup-turtles
crt num-turtles
[
set color green
setxy random-xcor random-ycor
set size 3  ;; easier to see
]
end

to setup-patches
[
; set the real portion of c to be the x coordinate of the patch
set c-real (pxcor / scale-factor)
; set the imaginary portion of c to be the y coordinate of the patch
set c-imaginary (pycor / scale-factor)
; have the initial value of z be 0 + 0i
set z-real 0
set z-imaginary 0
set counter 0
]
end

;;; Run-Time Procedures

to go
mandelbrot-calc-and-color
step
wiggle
climb
tick
end

; calculate the equation of the mandelbrot fractal for each patch with a turtle on it and change
; its color to be an appropriate color.

to mandelbrot-calc-and-color
; if the distance of a patch's z from the origin (0,0) is less than 2 and its counter is less
; than 256 perform another iteration the equation f(z) = z^2 + c.
ask turtles with [(modulus z-real z-imaginary <= 2.0) and (counter < 256)]
[
let temp-z-real z-real
set z-real c-real + (rmult z-real z-imaginary z-real z-imaginary)
set z-imaginary c-imaginary + (imult temp-z-real z-imaginary temp-z-real z-imaginary)
set counter counter + 1
set pcolor counter
]
end

;ask each turtle to move forward by 1

to step
[ ifelse can-move? 1
[ fd 1 ]
[ setxy random-xcor random-ycor ] ]
end

;ask each turtle to change its direction slightly

to wiggle
[
rt random 10
lt random 10
]
end

to climb
[
ifelse counter >= previous-count
[
set previous-count counter
set color yellow
jump throw
]
[
set previous-count counter
set color blue
rt 180
]
]
end

;;; Real and Imaginary Arithmetic Operators

to-report rmult [real1 imaginary1 real2 imaginary2]
report real1 * real2 - imaginary1 * imaginary2
end

to-report imult [real1 imaginary1 real2 imaginary2]
report real1 * imaginary2 + real2 * imaginary1
end

to-report modulus [real imaginary]
report sqrt (real ^ 2 + imaginary ^ 2)
end

```

There are 10 versions of this model.

Uri Wilensky over 9 years ago Updated to NetLogo 5.0.4 Download this version
Uri Wilensky over 10 years ago Updated to version from NetLogo 5.0.3 distribution Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Updated from NetLogo 4.1 Download this version
Uri Wilensky over 12 years ago Model from NetLogo distribution Download this version