gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
write code for mand
- User
write code for mand
- Assistant
Sure! Here is an example code in Python for generating the Mandelbrot fractal:
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 create_fractal(width, height, zoom, x_offset, y_offset, max_iter): img = np.zeros((width, height)) for x in range(width): for y in range(height): zx, zy = (x - width / 2) / zoom + x_offset, (y - height / 2) / zoom + y_offset c = complex(zx, zy) img[x, y] = mandelbrot(c, max_iter) return img # Fractal parameters width = 800 height = 800 zoom = 250 x_offset = 0 y_offset = 0 max_iter = 256 # Generate the fractal fractal_img = create_fractal(width, height, zoom, x_offset, y_offset, max_iter) # Display the fractal plt.imshow(fractal_img.T, cmap='hot', origin='lower') plt.axis('off') plt.show()This code uses the
numpylibrary to create a 2D array to store the iteration count for each pixel. The function iterates the equation until either the iteration count exceeds or the absolute value of exceeds 2. The function generates the fractal by calling for each pixel and assigns the iteration count to the corresponding element in . Finally, the fractal image is displayed using .
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.