File Exchange

image thumbnail


version (363 Bytes) by Altan Bassa


Updated 04 Sep 2001

No License

The Mandelbrot set is computed by operating on a fairly simple equation that contains complex numbers of the form

x + yi where i = sqrt(-1)

The Mandelbrot equation is

z <- z^2 + c


z = x + yi and c = a + bi

substituting these values into z^2 + c we have

(x + yi)^2 + a + bi

x^2 + 2xyi - y^2 + a + bi

separating the real and imaginary parts of z gives

x <- x^2 - y^2 + a

y <- 2xy + b

To determine whether a point (a,b) in the complex plane is a member of the Mandelbrot set, the real and imaginary parts of the equation are iterated. The x and y values are first initialized to zero. The constants a and b, the point in the plane, are then substituted into the equations giving

x <- a and y <- b

for the first iteration. The two new values for x and y, along with the constants a and b, are now substituted into the equations again. This procedure (iteration) continues until the absolute value of x + yi 2, i.e., sqrt(x^2 + ^y2) 2. For those cases where this value never exceeds 2, the maximum number of iterations is preset. A value of about 500 is usually adequate, although this value is raised to several thousand when smaller details at high magnification are examined. The number of times the equations are iterated before the value of sqrt(x^2 + y^2) 2 is called the dwell. Those initial points (a,b) where the dwell is infinite, or for more practical purposes attains the preset maximum, are members of the Mandelbrot set. Another way to describe this is to say that for points within the Mandelbrot set, the sequence of points produced by this iteration procedure is bounded inside a circle of radius 2, where points outside the set are unbounded and continue to grow and escape the circle.

The Mandelbrot set exists entirely within the area defined by

-2 <= a <= 2 and -2 <= b <= 2

in the complex plane. A Mandelbrot image is produced by taking this area of the complex plane and dividing it into an array of 1200 x 1200 points. Each one of these points becomes the constant (a,b). The iteration procedure previously described is used on each of the 1.44 million points, coloring each point in the Mandelbrot set black and all others white. The algorithm is:

maxcount <- 1000
for b <- 2 to -2 stepdown 1/300
for a <- -2 to 2 step 1/300
x <- 0
y <- 0
count <- 0
while sqrt(x^2 + y^2) < 2 and count < maxcount
x <- x^2 - y^2 + a
y <- 2*x*y + b
count <- count + 1
end while
if count = maxcount plot(a,b,BLACK)
else plot(a,b,WHITE)
end for a
end for b

While the algorithm is not that complex, the amount of computation is enormous. Depending on programming language and style, the inner loop has at least four multiplications and a square root. For a point in the Mandelbrot set, this loop is executed 1000 times and there are over a million points to check! It is not surprising that the Mandelbrot set was not discovered until the age of computers.

Cite As

Altan Bassa (2021). Mandelbrot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

wang andy


ken darrow

Cortland Setlow

Just what I needed. That line ought to be
while (-2<a)&(a<2)&(-2<b)&(b<2)&(n<r);
There's some kind of character code issue.
To repeat, change &#139; to a less-than sign.

jim chase

There appears to be an error in the program, which you can correct by replacing the line with while statement to :

while (sqrt(a^2+b^2)< 2)&&(n < r)

Then it sort-of does something like the picture on Student MatLab R13.

Daniel Soll

If running under Linux, remove &#139; and insert < instead.

bard joel

File does not work

CJ Kloxin

tono V.

Jason Bold

File does not work as is

MATLAB Release Compatibility
Created with R10
Compatible with any release
Platform Compatibility
Windows macOS Linux
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!