Mandelbrot set: Difference between revisions

Content added Content deleted
(→‎{{header|Python}}: a more mathematical spelling, as the students are used to it.)
m (→‎{{header|Python}}: Added a few comments.)
Line 6,394: Line 6,394:
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt


p = 100 # points per unit
n = 50 # number of iterations
n = 50 # number of iterations
r = 2.5 # escape radius (must be greater than 2)


x = np.linspace(-2.5, 1.5, 401)
x = np.linspace(-2.5, 1.5, 4*p+1)
y = np.linspace(-1.5, 1.5, 301)
y = np.linspace(-1.5, 1.5, 3*p+1)


A, B = np.meshgrid(x, y)
A, B = np.meshgrid(x, y)
C = A + B * 1j
C = A + B*1j


Z = np.zeros_like(C)
Z = np.zeros_like(C) # initial values are always zero
T = np.zeros(C.shape)
T = np.zeros(C.shape) # table of escape times

r = 2.5 # escape radius must be greater than 2
M = abs(Z) < r # mark all points that have not escaped


M = abs(Z) < r
for k in range(n):
for k in range(n):
Z[M] = Z[M] ** 2 + C[M]
Z[M] = Z[M]**2 + C[M]
M = abs(Z) < r
M = abs(Z) < r
T[M] = k + 1
T[M] = k + 1