write a program than plots the mandelbrot set
- User
write a program than plots the mandelbrot set
Assistant
Sure, here's a simple Python program that plots the Mandelbrot set using matplotlib and NumPy. This program calculates the points in the complex plane that do not diverge when iterating the Mandelbrot function.
import numpy as np import matplotlib.pyplot as plt import matplotlib.image as mpimg import matplotlib.ticker as mtick def mandelbrot(c_real, c_imag, max_iters): for j in range(max_iters): z_real = 0 z_imag = 0 for i in range(j): z_real = z_real*z_real - z_imag*z_imag + c_real z_imag = 2*z_real*z_imag + c_imag if (z_real*z_real + z_imag*z_imag) >= 4: return j return max_iters def generate_mandelbrot(width, height): x_min = -2.0 x_max = 1.0 y_min = -1.0 y_max = 1.0 c_real = np.linspace(x_min, x_max, width) c_imag = np.linspace(y_min, y_max, height) max_iters = 300 mandelbrot_set = np.zeros((height, width)) for i in range(height): for j in range(width): c_real_val = c_real[j] c_imag_val = c_imag[i] mandelbrot_set[i][j] = mandelbrot(c_real_val, c_imag_val, max_iters) return mandelbrot_set def plot_mandelbrot(mandelbrot_set): fig, ax = plt.subplots() ax.imshow(mandelbrot_set, interpolation='nearest', origin='lower')
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.