by Paulo Marques, 2014/03/01
This notebook shows how to implement and visualize the Mandelbrot Set in Julia. The Mandelbrot set is a very well known and beautiful fractal discovered in the 70s. It was named after Benoit Mandelbrot, a well-known mathematician. The first images of this object were computed in 1978.
Let's do it in Julia.
The basic idea behind the Mandelbrot set is that you have a 2D plane that represents an image. Each point of that plane is a complex number $z = x + y i$. Now, suppose what happens if, for each point $c$ in the plane, you iterate over and over calculating a new number as follows:
$$ z_{n+1} \gets z_{n}^2 + c$$In this formula $z_0 = 0$ and $c$ is the point in the plane you are looking at.
In this situation, two different things can happen. As the number of iterations advances either $\|z\|$ tends to infinity or it tends to 0. Mathematicaly, points where it tends to zero, are part of the set and are colored black. Points that tend to infinity are not part of the set and are colored white.
Since in a computer you cannot iterate an infinite number of times, typically you put a bound on how many times you do this calculation. Also, you can use the "how fast" $\|z\|$ is going to infinity to color the set much more nicely. You can also check if $z$ is going to infinity just by comparing $\|z\|$ with 2. If it has grown that big, it certainly is not part of the set.
So, for a point $c$, lets define a function mandel_pt()
that tells us the number of iterations that point is taking to diverge to infinity. (Note: points that are truly inside the set will take MAX_ITER
iterations. We will map those to zero as it makes for nicer colors.)
# Returns the number of iterations a points takes to go to infite on a mandelbrot set
function mandel_pt(c, MAX_ITER=100)
iter = 0
z = 0
while (iter < MAX_ITER)
z = z^2 + c
iter+= 1
if abs(z) > 2.0
return iter
end
end
0
end;
Now we want to define a function that for a rectangle of pixels, representing the complex plane that goes from $z_{min}$ to $z_{max}$, calculates the corresponding image. I.e., we are going to map all the points in that rectangle to a color, using mandel_it()
, returning the corresponding image.
# Return the Mandelbrot set by mapping each point to the corresponding number of iterations
mandel(zmin, zmax, resolution=1000) =
[ mandel_pt(complex(j, i)) for i=linspace(zmin.im, zmax.im, resolution), j=linspace(zmin.re, zmax.re, resolution) ];
We are all set! Let's calculate it:
# The complex plane to map
(zmin, zmax) = (-2.2-1.5im, +1.2+1.5im)
# The Mandelbrot set
mandel_set = mandel(zmin, zmax);
Finally, we can plot it.
# import basic library
using PyPlot;
# Auxiliary function to map the complex rectagle to an array
to_extent(zmin, zmax) = [zmin.re, zmax.re, zmin.im, zmax.im]
# Plot it
axis("off")
imshow(mandel_set, extent=to_extent(zmin, zmax));
There's another type of fractals called Julia. In fact, the formula is very similar to Mandelbrot but whereas $z_0 = c$ and $c$ was the point of the plane you were looking at, you can make $c$ be a generic complex constant and $z_0$ just be the point. Let's modify the program to reflect this.
# Returns the number of iterations a points takes to go to infite on a julia set
function julia_pt(z, c, MAX_ITER=100)
iter = 0
while (iter < MAX_ITER)
if abs(z) > 2.0
return iter
end
z = z^2 + c
iter+= 1
end
0
end;
# Returns a Julia Set
julia(zmin, zmax, c, resolution=1000) =
[ julia_pt(complex(j, i), c) for i=linspace(zmin.im, zmax.im, resolution), j=linspace(zmin.re, zmax.re, resolution)];
We are all set (again). Let's calculate a bunch of Julia Sets.
# The complex plane to map
(zmin, zmax) = (-1.5-1.5im, +1.5+1.5im)
# Constants to explore
c_explore = [-0.04+0.66im; 0.285+0.01im; 0.8+0.14im; -0.8+0.156im]
julias = [ julia(zmin, zmax, c) for c=c_explore ];
And plot them.
n_lines = round(Int, ceil(length(c_explore)/2))
fig, ax = subplots(n_lines, 2, figsize=figsize=(10, 10))
ext = to_extent(zmin, zmax)
for i=1:length(ax)
ax[i][:axis]("off")
if i<=length(c_explore)
ax[i][:imshow](julias[i], extent=ext)
end
end
That's all folks.
Copyright (C) 2014 Paulo Marques (pjp.marques@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.