The Mandelbrot fractal is a mathematical set generated by the iterative equation
zn+1 = (zn)² + c
where c is a chosen complex number.
Steps to determine if a point c belongs to the set:
Choose a complex number c
We start with z0=0
We iterate the equation until ∣zn∣>2 (meaning the point escapes the set) or until reaching a maximum number of iterations Maxiter.
If after Maxiter iterations we have ∣zn∣≤2, then c belongs to the Mandelbrot set.
To visualize the fractal:
Points that do not belong to the set can be colored based on how many iterations they took to escape, creating gradient patterns that show the "distance" to the set.
Points that belong to the set are usually represented in black.
A very simple Python code for this could be the following:
import numpy as np
import matplotlib.pyplot as plt
# Function to determine if a point belongs to the Mandelbrot set
def mandelbrot(c, max_iter):
z = 0
for i in range(max_iter):
if abs(z) > 2:
return i # Number of iterations before escaping
z = z * z + c
return max_iter # Max iterations if the point does not escape
# Function to generate the Mandelbrot set within a given range
def generate(width, height, x_min, x_max, y_min, y_max, max_iter):
x_vals = np.linspace(x_min, x_max, width) # Generate x-axis values
y_vals = np.linspace(y_min, y_max, height) # Generate y-axis values
mandelbrot_set = np.zeros((height, width))
# Compute Mandelbrot set value
for i, y in enumerate(y_vals):
for j, x in enumerate(x_vals):
mandelbrot_set[i, j] = mandelbrot(complex(x, y), max_iter)
return mandelbrot_set
# Parameters for the Mandelbrot set visualization
width, height = 500, 500
x_min, x_max = -2, 1
y_min, y_max = -1.5, 1.5
max_iter = 100
# Generate and plot the Mandelbrot set
set = generate(width, height, x_min, x_max, y_min, y_max, max_iter)
plt.imshow(set, extent=(x_min, x_max, y_min, y_max), cmap='inferno')
plt.colorbar()
plt.show()
Here, some outputs with different values of max_iter:
max_iter=4
max_iter=6
max_iter=8
max_iter=10
max_iter=20
max_iter=50
max_iter=150
max_iter=200
max_iter=256
Some details in a small region of the plane:
Here a versión of the code which allows to change the pallete de colors in a simple way:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import LinearSegmentedColormap
# Function to determine if a point belongs to the Mandelbrot set
def mandelbrot(c, max_iter):
z = 0
for i in range(max_iter):
if abs(z) > 2:
return i # Number of iterations before escaping
z = z * z + c
return max_iter # Nx iterations if the point does not escape
# Function to generate the Mandelbrot set within a given range
def generate(width, height, x_min, x_max, y_min, y_max, max_iter):
x_vals = np.linspace(x_min, x_max, width) # Generate x-axis values
y_vals = np.linspace(y_min, y_max, height) # Generate y-axis values
mandelbrot_set = np.zeros((height, width))
for i, y in enumerate(y_vals):
for j, x in enumerate(x_vals):
mandelbrot_set[i, j] = mandelbrot(complex(x, y), max_iter)
return mandelbrot_set
# Custom color map: black for points that don't escape
colors = [(0, 0, 0), (1, 0.1, 0), (0.1, 0.3, 0.8), (0, 0, 0)]
n_bins = 100 # Number of bins
cmap_name = 'black_to_color'
custom_cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bins)
# Parameters for the Mandelbrot set visualization
width, height = 1000, 1000
x_min, x_max = -0.3, 0.0
y_min, y_max = -0.8, -1.1
max_iter = 120
# Generate and plot the Mandelbrot set
set = generate(width, height, x_min, x_max, y_min, y_max, max_iter)
# Create the image with the custom color map
plt.imshow(set, extent=(x_min, x_max, y_min, y_max), cmap=custom_cmap)
plt.colorbar()
plt.show()
Some different palletes for the iterations: