|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +# the mandelbrot set is defined such that z_{n+1} = z_n^2 + c |
| 5 | +# remains bounded, which is sufficient to say that |z_{n+1}| <= 2 |
| 6 | +# where c is a complex # and we start with z_0 = 0 |
| 7 | + |
| 8 | + |
| 9 | +# we want to consider a range of c, as complex numbers c = x + iy, |
| 10 | +# were -2 < x < 2 and -2 < y < 2 |
| 11 | + |
| 12 | +N = 1024 |
| 13 | + |
| 14 | +c = np.zeros((N, N), dtype=np.complex) |
| 15 | + |
| 16 | +xmin = -2 |
| 17 | +xmax = 1 |
| 18 | +ymin = -1.5 |
| 19 | +ymax = 1.5 |
| 20 | + |
| 21 | +#xmin = 0 |
| 22 | +#xmax = 0.5 |
| 23 | +#ymin = -0.75 |
| 24 | +#ymax = -0.25 |
| 25 | + |
| 26 | +x = np.linspace(xmin, xmax, N, dtype=np.float) |
| 27 | +y = np.linspace(ymin, ymax, N, dtype=np.float) |
| 28 | + |
| 29 | +c[:,:].real = x[:,np.newaxis] |
| 30 | +c[:,:].imag = y[np.newaxis,:] |
| 31 | + |
| 32 | +z = np.zeros((N,N), dtype=np.complex) |
| 33 | + |
| 34 | +niter = np.zeros((N,N), dtype=np.int) |
| 35 | +niter[:,:] = -1 |
| 36 | + |
| 37 | +MAX_ITER = 256 |
| 38 | + |
| 39 | +for n in range(MAX_ITER): |
| 40 | + z = z**2 + c |
| 41 | + idx = np.logical_and(abs(z) > 2, niter == -1) |
| 42 | + niter[idx] = n |
| 43 | + |
| 44 | +# some things may still have not passed |
| 45 | +niter[niter == -1] = MAX_ITER-1 |
| 46 | + |
| 47 | +print(niter.min(), niter.max()) |
| 48 | + |
| 49 | +plt.imshow(niter.T, extent=[xmin, xmax, ymin, ymax], origin="lower") |
| 50 | +f = plt.gcf() |
| 51 | +f.set_size_inches(8.0, 8.0) |
| 52 | +plt.show() |
| 53 | + |
| 54 | + |
0 commit comments