This little article is more for my own notes. Hoping to add more details later.
What is the Mandelbrot Set?
The Mandelbrot set is a set of complex numbers defined in the complex plane1. It exhibits great complexity and is popular for its aesthetic appeal and fractal structures1. The set is defined as the complex numbers for which the function f(z) = z^2 + c does not diverge to infinity when iterated starting at z = 0, i.e., for which the sequence z, f(z), f(f(z)), ... remains bounded in absolute value1.
What is the Julia Set?
The Julia set, named after the French mathematician Gaston Julia, is a fractal that is defined from a function2. The Julia set consists of values such that an arbitrarily small perturbation can cause drastic changes in the sequence of iterated function values2. If c is held constant and the initial value of z is varied instead, the corresponding Julia set for the point c is obtained1.
Relationship Between the Mandelbrot Set and the Julia Set
There is a close correspondence between the geometry of the Mandelbrot set at a given point and the structure of the corresponding Julia set34. The Mandelbrot set forms a kind of index into the Julia set35. The Mandelbrot set is the set of all c for which the Julia set is connected6.
Python Implementation to Display a Mandelbrot Set
Here’s a simple Python implementation to display a Mandelbrot set:
import numpy as np
import matplotlib.pyplot as plt
def mandelbrot(c, max_iter):
z = c
for n in range(max_iter):
if abs(z) > 2:
return n
z = z*z + c
return max_iter
def draw_mandelbrot(xmin,xmax,ymin,ymax,width,height,max_iter):
r1 = np.linspace(xmin, xmax, width)
r2 = np.linspace(ymin, ymax, height)
return (r1,r2,np.array([[mandelbrot(complex(r, i),max_iter) for r in r1] for i in r2]))
def main():
dpi = 80
img_width = dpi * 3
img_height = dpi * 3
xmin = -2.0
xmax = 1.0
ymin = -1.5
ymax = 1.5
max_iter = 256
x,y,z = draw_mandelbrot(xmin,xmax,ymin,ymax,img_width,img_height,max_iter)
fig, ax = plt.subplots(figsize=(3,3),dpi=dpi)
ticks = np.arange(0,img_width,3*dpi)
x_ticks = xmin + (xmax-xmin)*ticks/img_width
plt.xticks(ticks, x_ticks)
y_ticks = ymin + (ymax-ymin)*ticks/img_width
plt.yticks(ticks, y_ticks)
ax.set_title("Mandelbrot Set")
ax.imshow(z, origin='lower', cmap='hot')
plt.show()
if __name__ == "__main__":
main()
This script generates an image of the Mandelbrot set. The mandelbrot function determines the iteration count at which a point escapes to infinity (or reaches the maximum iteration count). The draw_mandelbrot function generates the Mandelbrot set over a given range and resolution. The main function sets up the plot, calls draw_mandelbrot to generate the data, and then displays the result.
Please note that this is a basic implementation and there are many ways to optimize and enhance this code to generate more detailed or colorful images of the Mandelbrot set. I’ve got more pictures to upload soon. Documenting this for fun at the moment.
I hope this article helps you understand the Mandelbrot set, the Julia set, and their relationship. Happy coding!
