import numpy as np import matplotlib.pyplot as plt def mandelbrot(xmin, xmax, ymin, ymax, density): # Question 1.1 xpoints = int((xmax-xmin)*density) ypoints = int((ymax-ymin)*density) # Question 1.2 X = np.linspace(xmin, xmax, xpoints) Y = np.linspace(ymin, ymax, ypoints) maxiter = 50 # Question 1.3 Z = np.ones((ypoints, xpoints), dtype='int')*maxiter # Question 1.4 for i in xrange(ypoints): for j in xrange(xpoints): c = complex(X[j], Y[i]) z = c for k in xrange(maxiter): z = z*z+c if abs(z) > 2: Z[i,j] = k break # Question 1.5 return Z # Question 2 Z = mandelbrot(-2, 1, -1, 1, 1000) plt.imshow(Z) plt.colorbar() plt.savefig('mandelbrot_full.png', dpi=300) plt.close() # Question 3 Z = mandelbrot(-0.5, 0.5, 0.5, 1, 2000) plt.imshow(Z) plt.colorbar() plt.savefig('mandelbrot_zoom.png', dpi=300) plt.close()