Code Snippet #12: Mandelbro fractals
Yo! Justin Case here. a while ago we did a Fractal tree thing in HTML, well we are going to be doing something similar in python, a Mandelbrot fractal or more specifically the Mandelbrot set. its basically math that makes something similar to the Cell stage of the game known as Spore, its it looks cool and you can use it to flex your coding skills. So grab your headache pills and some coffee, and let’s get started!
firsts things first lets look at what the end result should look like, so that if something breaks, its your fault and not mine.
next is the code, ah wait, before we get into the code make sure your pip is update and then in your terminal install numpy and matplotlib.
you can install both by typing:
1 2 3 | pip install numpy pip install matplotlib |
anywho back to the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | 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 mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter): r1 = np.linspace(xmin, xmax, width) r2 = np.linspace(ymin, ymax, height) n3 = np.empty((width, height)) for i in range(width): for j in range(height): n3[i, j] = mandelbrot(r1[i] + 1j*r2[j], max_iter) return r1, r2, n3 xmin, xmax, ymin, ymax = -2.0, 1.0, -1.5, 1.5 width, height = 1000, 1000 max_iter = 256 x, y, mandelbrot_image = mandelbrot_set(xmin, xmax, ymin, ymax, width, height, max_iter) |
this is the two functions and junk needed to do mandelbro by checking to see if a number is in the set and then another to make a grid to draw on
and then the get the code to run:
1 2 3 4 5 6 | plt.imshow(mandelbrot_image.T, extent=[xmin, xmax, ymin, ymax], cmap='hot') plt.colorbar() plt.title("Mandelbrot Set") plt.xlabel("Re") plt.ylabel("Im") plt.show() |
extent
parameter setting the limits for the x and y axes.and thats pretty much it, you should get a weird bacteria looking thing when you run the code.
make sure you get all of the libraries tho.
I said I would include a FAQ(i like saying this for some reason) at the end of my posts, lets see what we got:
disclaimer:ChatGPT aided in my FAQ cause I am uncreative.
Frequently Asked Questions
What is the Mandelbrot set?
The Mandelbrot set is a complex mathematical set of points whose boundary forms a fractal. It is defined by iterating a simple mathematical formula and is known for its intricate and beautiful patterns.
How does the Mandelbrot set relate to complex numbers?
The Mandelbrot set is generated using complex numbers. Each point in the set corresponds to a complex number, and the set is created by iterating a function that involves complex number arithmetic.
Why are fractals important?
Fractals are important in various fields due to their unique properties. They are used in computer graphics, natural phenomena modeling, and even in financial markets for analyzing stock prices.
Can I customize the visualization of the Mandelbrot set?
Yes, you can customize the colors, resolution, and other aspects of the visualization using matplotlib and other graphical libraries in Python.
What are some applications of fractals?
Fractals have applications in nature (e.g., patterns of leaves and snowflakes), computer graphics (e.g., generating realistic landscapes), art, and even in modeling chaotic systems like weather and stock markets.
Where can I learn more about fractals and the Mandelbrot set?
You can find numerous resources online, including tutorials, mathematical papers, and videos that explain the concept of fractals and the Mandelbrot set in detail.
anyways thats it for this snippet, I am working on a case but I dont know how to post it on here, but we will see, there is a new 3rd case I am starting(a simple VR tourism application) but, I am still planning it out.
anyways thats all for now tata
~~Justin Case
Comments
Post a Comment