Fractal

Mandelbrot set

The Mandelbrot set is simple rule that can create a self-similar fractal structure. It is probably the most famous fractal, having gained its popularity from the fascinating glossy images by Heinz-Otto Peitgen and Peter Richter in their famous book The Beauty of Fractals. As a fractal, the Mandelbrot set is a self-similar structure, which lives in the complex plane, and contains infinitely many copies of itself. It is created when the complex valued map:

z_{n+1} = z^{2}_{n} + c \quad z, c \in \mathbb{C}\\ \quad \\ \text{With}\\ z = \xi + i \eta\\ c = c_r + i c_i\\ \quad \\ \xi_{n+1} = \xi^2_{n} - \eta^2_{n} + c_r\\ \eta_{n+1} = 2\xi_n \eta_n + c_i

The initial value is and the constant is the location in the complex plane with as - and as -value, respectively.

Question

  • Write a python (matlab) program to draw the Mandelbrot set.

Solution
1
"""
2
@author: yacine.mezemate
3
"""
4
5
import matplotlib.pylab as plt
6
import numpy as np
7
 
8
def Man(c):
9
	z = 0
10
	for n in range(1, 100):
11
		z = z**2 + c
12
		if abs(z) > 2:
13
			return n
14
	return np.NaN
15
 
16
X = np.arange(-2, .5, .004)
17
Y = np.arange(-1,  1, .004)
18
Z = np.zeros((len(Y), len(X)))
19
 
20
for j, y in enumerate(Y):
21
	print (j, "of", len(Y))
22
	for i, x in enumerate(X):
23
		Z[j,i] = Man(x + 1j * y)
24
 
25
plt.imshow(Z, cmap = plt.cm.prism, interpolation = 'none', extent = 
26
      (X.min(), X.max(), Y.min(), Y.max()))
27
     
28
plt.xlabel("Real(c)", fontsize = 18)
29
plt.ylabel("Imaginary(c)", fontsize = 18)
30
plt.title("Mandelbrot set", fontsize = 18) 
31
plt.show()
PreviousPreviousNextNext
HomepageHomepagePrintPrintCreated with Scenari (new window)