'''This program visualises a Mandelbrot set (cf. Chapter 9.2).
The axis window is defined by means of the following parameters:
ux = smallest x-coordinate
ox = largest x-coordinate
nx = number of grid points on the x-axis
uy = smallest x-coordinate
oy = largest x-coordinate
ny = number of grid points on the y-axis
The parameter kmax determines the number of iterations.
'''
import numpy as np
import matplotlib.pyplot as plt
ux, ox, nx = -2., .6, 600
uy, oy, ny = -1., 1., 500
x = np.linspace(ux, ox, nx)
y = np.linspace(uy, oy, ny)
xx, yy = np.meshgrid(x, y)
c = xx + yy*1j # c is initialized as the complex plane
z = np.zeros(c.shape)
kmax = 50 # Increase this number to improve the quality of the plot
'''This will cause some runtime warnings, which can be ignored.
The computation might take a few seconds to compute.
'''
for k in range(kmax):
z = np.power(z,2) + c
z = np.fmin(100,abs(z))
fig = plt.figure()
plt.pcolormesh(xx, yy, z)
plt.gray()
plt.savefig('mandelbrot.png',dpi = 600)
plt.show()