Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code the following problem in python: The Mandelbrot set is an example of a frac

ID: 3733399 • Letter: C

Question

Code the following problem in python:

The Mandelbrot set is an example of a fractal: an object that contains a structure within a structure, iteratively as far as we care to look. Consider the following iterative equation (all variable:s represent complex numbers) The Mandelbrot set considers a complex number c and sets zo-0.The above equation is applied iteratively until lzl is above 2 or until z has been updated many times (to z0, say). If Izl is not found to ever exceed 2, then the point c is in the Mandelbrot set; if it does exceed 2, it is not in the Mandelbrot set 1. Consider a complex number in the formc-x+iy. Consider a100x100 grid of points on the complex plane constrained by -2SxS 2 and-2sys2 For each value of c on this grid, determine the number of iterations required for lzl to exceed 2. If lzl does not exceed 2, record the maximum number of iterations considered instead Record these values on a100x100NumPy array 2. 3. Save the array formed in step 2 to file. Then comment out the analysis code and instead load your results from file. (This speeds things up as you play around in the following step!) Plot the array constructed in step 2 using pit, imshow(«array»,

Explanation / Answer

import matplotlib.pyplot as plt
import numpy as np

def mandelbrot(max_reps_allowed):
# max_reps_allowed is the maximum repetitions till which you want to check |z|
xs = np.linspace(-2, 2, 100)
ys = np.linspace(-2, 2, 100)

required_reps = np.zeros([100, 100])
for i, x in enumerate(xs):
for j, y in enumerate(ys):
c = complex(x, y)
reps = 0
previous_z = 0
current_z = 0
while(abs(current_z) <= 2 and reps < max_reps_allowed):
current_z = pow(previous_z, 2) + c
previous_z = current_z
reps += 1
required_reps[i, j] = reps
return required_reps

data = mandelbrot(max_reps_allowed=100)
# to save the data in a file
np.savetxt(r'ENTER_YOUR_DIRECTORYdata_file.txt', data)
# to read the data and plot it
data_file = np.loadtxt(r'ENTER_YOUR_DIRECTORYdata_file.txt')
plt.imshow(data_file, cmap='spectral')
plt.show()