|
| 1 | +import numpy as np |
| 2 | +import matplotlib.pyplot as plt |
| 3 | + |
| 4 | +def mandelbrot(c, max_iter): |
| 5 | + z = 0 |
| 6 | + for n in range(max_iter): |
| 7 | + if abs(z) > 2: |
| 8 | + return n |
| 9 | + z = z*z + c |
| 10 | + return max_iter |
| 11 | + |
| 12 | +def mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter): |
| 13 | + x = np.linspace(xmin, xmax, width) |
| 14 | + y = np.linspace(ymin, ymax, height) # More general, though width == height here |
| 15 | + |
| 16 | + mset = np.zeros((height, width)) |
| 17 | + |
| 18 | + for i in range(height): |
| 19 | + for j in range(width): |
| 20 | + c = complex(x[j], y[i]) # Fix: imaginary part varies with i |
| 21 | + mset[i,j] = mandelbrot(c, max_iter) |
| 22 | + |
| 23 | + return mset |
| 24 | + |
| 25 | +# xmin, xmax, ymin, ymax = -2.0, 1.0, -1.5, 1.5 |
| 26 | +width, height = 1000, 1000 |
| 27 | +max_iter = 100 |
| 28 | + |
| 29 | +mandelbrot_image = mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter) |
| 30 | + |
| 31 | +plt.imshow(mandelbrot_image, extent = [xmin, xmax, ymin, ymax], cmap='hot') |
| 32 | +plt.colorbar() |
| 33 | +plt.title('Mandelbrot Visualization') |
| 34 | +plt.xlabel("Re(c)") |
| 35 | +plt.ylabel("Im(c)") |
| 36 | +plt.show() |
0 commit comments