Question: This is a Python script which creates a Mandelbrot set using a Python library called Matplotlib.I have compiled the code using Thonny IDE. import numpy

This is a Python script which creates a Mandelbrot set using a Python library called Matplotlib.I have compiled the code using Thonny IDE.


import numpy as np
import matplotlib.pyplot as plt

def mandelbrot(c, max_iter):
    z = c
    for i in range(max_iter):
        if abs(z) > 2.0:
            return i
        z = z**2 + c
    return max_iter

def create_mandelbrot(width, height, x_min, x_max, y_min, y_max, max_iter):
    image = np.zeros((height, width))
    for x in range(width):
        for y in range(height):
            zx = np.interp(x, [0, width], [x_min, x_max])
            zy = np.interp(y, [0, height], [y_min, y_max])
            c = complex(zx, zy)
            image[y, x] = mandelbrot(c, max_iter)
    return image

# Set the parameters
width = 800
height = 800
x_min, x_max = -2.5, 1.5
y_min, y_max = -2.0, 2.0
max_iter = 100

# Create the Mandelbrot set image
image = create_mandelbrot(width, height, x_min, x_max, y_min, y_max, max_iter)

# Plot the image
plt.imshow(image.T, cmap='hot', extent=(x_min, x_max, y_min, y_max))
plt.title('Mandelbrot Set')
plt.xlabel('Re(c)')
plt.ylabel('Im(c)')
plt.colorbar(label='Iterations')
plt.show()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

The detailed ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Electrical Engineering Questions!