Come here often and see all the crazy things that I have been upto!

Archive for February, 2012

Fractals Blow my Mind! (Part #2)

Today I will delve into more detail (as promised!) about how to use the iterative equation  to determine which points belong or don’t belong to a mathematical set. I will use the equation recursively on each point to test if it ever escapes the set, and if so, to record how many iterations it takes to do so. It is possible to zoom into a fractal to infinity, but the processing power required to do so greatly surpasses the abilities of my puny brain, and my post-homework time; instead, just watch this video and be baffled.

I am learning here as well, so bear with me. I will try to simplify my explanation as much as possible, which will eventually be used in my independent assignment. The end purpose of this guide is to draw the Mandelbrot set by hand, understand how it is done, and be able to appreciate the sheer complexity of fractals.

Let’s get right to it, shall we? As I said in my last post, start off by drawing a Cartesian plane with a scale of 0.5, extending to 2 in all directions. In the center of the  9×9 grid should be the origin (0, 0i), like so:

Now, for the tricky part involving calculations; don’t give up all hope just yet, and you will have a beautiful fractal to post on your wall, or whatever you see fit.  This will take a while, and a lot of paper, so be prepared to give up some precious time if you actually want to draw it. If not, just read on and enjoy!

Start off with the equation , in which the C is the constant complex number, the point on the grid which we are testing. The Z is the result of the iterations, and starts off as zero, or (0, 0i). There are actually 2 Z’s in the equation, second is the initial Z, which would start at 0, and  the first is the “new” Z, the result of the iteration. After doing , the second Z would simply be that point. After that, the Z would be equal to the old Z squared, plus the C. Then you have that result, which would be squared again and then add C. On and on it goes!

For a point to belong to the set, the magnitude of Z must never exceed 2. Either it will continue fluctuating between 0 and 2 no matter how many times you iterate, in which case it belongs to the set and can be labelled so. Else it will eventually expand to infinity after a set number of iterations, in which case it does not belong to the set. The number of iterations is only important if you want to add shading, which shows extra detail that would not be visible otherwise.

Choose a point that you want to know if it belongs to the set. For this example I will start with the top left square (-2, 2i). First determine the magnitude of this number by squaring both the x and y values, adding them, and finding the square root of their sum.

In this case the magnitude would be approximately 2.82. Therefore, the point does not belong to the set, and the iteration count is 0, since it is greater than 2 even before one pass.

Point (-2, 2i) — Part of the set: No, Iteration count: 1, Color: Red

Now let’s try the center square, (0, 0).

Obviously 0 is less than 2, and will never increase beyond 2. Therefore this point is part of the set.

Point (0, 0i) — part of the set: Yes, Iteration count: 0, Color: Blue or Black

And how about a slightly harder one? Let’s do (-1.5, 1)

Would you look at that, the magnitude (Z) is not 0, but it is still less than 2. Now I promised we would get to use the equation , and now is our chance to do the second pass, or iteration, using that equation. This is where the trickiest calculations come into play. Here you need to first off square the result of the previous iteration, which is simply (-1.5, 1), by using the multiplication shortcut .

Now we can simply add the x’s and y’s together, and get a complex number in (x, y) format as the result. Then we simply calculate the magnitude of that number to see if it is greater than 2.

At last the point (-1.5, 1) has escaped the Mandelbrot set after  a total of 2 iterations. Now that point can be labelled on the Cartesian plane, and the iteration count can also be recorded.

Point (-1.5, 1) — Part of the set: No, Iteration Count: 2, Color: Green

What I have just shown you here, can be repeated for any point on the Cartesian plane, no matter how precise it is. To complete this fractal, continue using the equation on each of the 81 individual point on the grid. As you calculate each point, record the iteration count and whether or not it is part of the set in a neat table, to simplify the task of coloring in the fractal.

A quick note here, in this case its quite obvious which points will or won’t escape the set, but it won’t always be that easy. When calculating many precise points, an iteration limit must be set, which if exceeded, means that it will never reach infinity. This limit is the maximum number of times that the point will be iterated through the equation, after which the point is assumed to never increase. Also, some points will form a pattern or alternate between two values; if this happens, it can be assumed that the point will never increase past 2.

Once all the points in the grid have been calculated and you have determined:

A. whether or not it belongs to the set, and

B. how many iterations it has taken to escape,

An example of the fluctuation of some points being iterated through the equation. The red one remains in the set ( under 2) past the threshold, but the blue one surpasses 2 and exponentially increases towards infinity soon after.

you can proceed to the fun part, coloring it in! Start off by choosing a color for points that belong to the set, preferably black, and then a seperate gradient for each quantity of iterations,  from at least 0 to 3 iterations. Color in each square with its specific color, and you will have a beautiful fractal of the Mandelbrot Set!

The end result! Quite beautiful, yet it does look somewhat plain and lacking in detail. However, there is a solution to this problem.

I have already spent more than enough time on this blog post, but if you want to further continue this project I’ll let you do it on your own. Simply draw out a new grid with more points and a smaller scale, I recommend 17×17 with a scale of 0.25 or even 21×21 with a scale of 0.2. In other words, you are increasing the resolution, and decreasing the point size. I’m warning you though, The amount of work increases exponentially with an increase in the amount of points, so be prepared to give up a lot of time! Send me your results and I might even feature it here on my blog!

If I was a computer (which sadly, I am not), I would be able to increase the resolution to millions of points, and draw this out in a matter of seconds. I could also add these beautiful colors and be able to zoom in anywhere to nearly infinite detail. Unfortunately, it would take you years to do it on your own; that is why I’m providing you with a program to do it for you, which you can download here:

Enjoy and try out that colorcylcing mode! — Patrick

Fractals Blow My Mind! (Part #1)

While I was doing research and notes for my independent assignment, I realized just how deep and complex the field of fractals is. I took the decision of selecting fractals as the main topic quite lightly, mainly because they are just so fascinating and mysterious! However now I realize that truly understanding them is not for the slightest faint of heart. I found some amazing website explanations of fractals, and lets say, some not so great. I don’t blame them though, it is in fact really hard to explain “imaginary complex numbers”, and I don’t pretend to understand them myself.

You see, the Mandelbrot Set, which is the most popular and in my opinion also the most beautiful, is based on an iterative equation . Being that the equation is iterative just like all fractals are, this means that a given number must be put into the equation many times, in order to get the final result. Sounds simple, right? Well in reality, many calculations occur before and throughout this process, which I will try to explain in more detail here.

Basically, a fractal integrates complex numbers into its equation, a number that cannot possibly exist in reality. So how can it exist if it can’t exist? Well, this is where it gets tricky, and the number i will haunt you just as it did me. The number i was created because no real number can be squared and give a negative number as a result. This number (umm, letter) is equal to the square root of -1, which is not possible, but then again, what number squared would give -1? We don’t know, so that’s why we represent it with the letter i.

The imaginary unit i is used in complex numbers in the complex form a+bi, in which the a is the real part graphed on the x-axis, and b multiplied by i is the imaginary part, which shows up on the y-axis.  In fractals, we are not concerned about the actual complex number, but its magnitude, or distance from zero. with real numbers, this is simply the absolute value of that number. However, the magnitude of a complex number is its distance from the origin of the Cartesian plane, which is found by taking the square of the distance from both the x and y axes, then taking the square root of their sum.

Example: If you are given the number 5i, and you want to know its square, simply square the real part, and then change it to negative, giving -25.  Similarly in the expression 3i, the square would be 9 negative, because 3 times i would be the square times -1, which is -9.

Now on to the fun stuff, actually graphing the fractal BY HAND! To start off, draw a Cartesian plane that extends to 2 in all directions, since that is the size of the fractal. The horizontal axis can be labelled RE, for real, and the vertical IM, for imaginary, and both should be scaled to at least 0.5 (giving a 9×9 grid, which would be sufficient for a rough sketch, a smaller scale would be even better, but more time-consuming). Each square would be given a coordinate, such as (-2, 2), (-1.5, 1), (1, 0.5) etc, which will be substituted into the equation  to determine if it surpasses 2.

The process of selecting a coordinate, determining its magnitude, substituting into the equation using the complex number Z and the constant C, and iterating this equation to determine whether the point belongs to the Mandelbrot set, will be explained in more detail in my next blog post.