Lesson 11 of 15

Mandelbrot Set

Mandelbrot Set

The Mandelbrot set is one of the most famous fractals in mathematics. A complex number c belongs to the set if the sequence defined by:

z0=0,zn+1=zn2+cz_0 = 0, \quad z_{n+1} = z_n^2 + c

remains bounded (never escapes to infinity). In practice, we check whether |z| ever exceeds 2 — if it does, the sequence will diverge to infinity.

The escape time algorithm counts how many iterations it takes for |z| to exceed 2. Points inside the set never escape; points outside escape after some number of steps. The number of iterations before escape gives information about how "close to the boundary" a point is, and is used to color the famous fractal images.

Implement the following functions:

  • mandelbrot_iter(cr, ci, max_iter) — return the number of iterations before |z|² > 4, or max_iter if the point is in the set. The complex number c = cr + ci·i, z starts at 0.
  • in_mandelbrot(cr, ci, max_iter) — return True if the point is in the Mandelbrot set (did not escape within max_iter iterations)
  • mandelbrot_grid(cr_min, cr_max, ci_min, ci_max, nx, ny, max_iter) — count how many grid points in the given rectangle are in the Mandelbrot set
Python runtime loading...
Loading...
Click "Run" to execute your code.