The Mandelbrot Set

Question

The Mandlelbrot set is the set of all points in the complex plane for which, starting with $z_0 = 0$, the iteration $z_{n+1}=z_n^2 + c$ remains bounded. It is the case that if $|z_n|$ ever becomes larger than 2, then the sequence diverges (i.e. is not bounded).

The Mandelbrot set may be visualised as an image in which the $(x, y)$ coordinates of each pixel are the real and imaginary parts of $c = x + iy$, and the pixel is coloured black or white according to whether $c$ is in the set or not.

Write a program to display the Mandelbrot set in the region of the complex plane bounded by $-3 \le x \le 1, -2 \le y \le 2$, by displaying a space if a point is in the set or an asterisk if it isn't.

Hints:

  • Map the region of interest to an array 40 rows of 80-character strings, and inspect each point in this array:
cx, cy = -1., 0.
xsize, ysize = 80, 40
for j in range(ysize):
    for i in range(xsize):
        x = cx + 4. * (i - xsize / 2.) / xsize
        y = cy + 4. * (j - ysize / 2.) / ysize
        ...
  • Either use Python's builtin support for complex numbers, _e.g._:
z = 3. + 8.5j

or handle the real and imaginary parts as separate floats.

  • When iterating, in addition to checking if $|z_n| \ge 2$, keep track of the number of iterations performed and bail (assume $c$ is in the set) if this number exceeds some maximum (say, 1000).

(Extra credit). Extend your answer to the previous exercise by generating a 500 x 500 pixel greyscale image of the Mandelbrot set. The shade of grey a pixel is coloured should reflect the number of iterations required to determine that the corresponding point is not in the set. Use PIL, the Python Imaging Library as follows:

from PIL import Image

im = Image.new('RGB', (xsize, ysize))
...
im.putpixel((i, j), (ired, igreen, iblue))
...
im.save('mandelbrot.png', 'PNG')

where a pixel is placed at $(i, j)$ and coloured with red, green and blue components ired, igreen, iblue (each an integer from 0 to 255, such that (0,0,255) is pure blue, (255, 255, 255) is white, (0, 0, 0) is black, etc).


Solution

To access solutions, please obtain an access code from Cambridge University Press at the Lecturer Resources page for my book (registration required) and then sign up to scipython.com providing this code.