Monday, 26 September 2011

Mandelbrot Set Part Two

OK, so last time we worked out that the limits of the mandelbrot set on the real number line was negative two on the negative side and plus a quarter on the positive side. Between those limits you can keep squaring and adding your original number and your series will never head off to infinity. So let's complicate matters and move to the complex plane.

First of all, let's look at \(i\). We now know what you have to do to multiply and add complex numbers. Remember that you can also write the number \(i\) as \(0+i\) and as \(1\angle\tfrac{\pi}{2}\). The first version means our point on the complex plane has a real value of zero, and an imaginary value of one. The second version means the point is one unit away from the origin (the crossing point of the real and imaginary axes), a quarter turn of circle from the real axis. So we know what it is, let's make it the first in our sequence:

\(0+i,\ldots\)

So what's the next number? The first step of working out the next number is to square the last one. How do we do that with complex numbers? Well, squaring a number is just multiplying the number by itself, so that is a multiplication task. Best move is to use the polar form. The rule we worked out was to multiply the lengths and add the angles. So if we look at the polar form of \(i\) we need to multiply one by one (getting one), and then add \(\tfrac{\pi}{2}\) to \(\tfrac{\pi}{2}\) (getting \(\pi\)). So our squared number is \(1\angle\pi\). Well, of course it fucking is. \(i\) IS the square root of negative one, so if we square it we get negative one. And negative one is, of course, one unit away from the origin ON the real axis, a half circle round from the starting position.

The second step is to add our first number. We need to switch back to rectangular form for this. Negative one in rectangular form is \(-1+0i\). To this we add our starting number. We can do this by just adding the real and imaginary parts. The real part of our starting number is zero, and the real part of our intermediate number is negative one, so the final real part is negative one. The imaginary part of the original is one, and the intermediate is zero, so the result is one. So our final number is \(-1+i\). In polar form this is \(\sqrt{2}\angle\tfrac{3\pi}{4}\). The polar form means turn three quarters of the way to a half circle, and go a distance of the square root of two. We get the distance from pythagoras as usual.

It is a bit tricky thinking about all this in abstract notation, even though we have gone through all of these steps before. Lets look at the process in picture form so we get a feel for what is going on. Our first number looks like this:



Now, let's look at the result of doing the squaring operation:


You can see we have doubled the angle (I have shown the extra angle with a dashed line), because we have added itself to itself. And the distance remains the same because one multiplied by one is one. Now we add on the original number:


And you can see that we end up, as predicted, at \(-1+i\). But there is something important to spot when you look at this that has been very difficult to spot when we have just been using pure algebra. Look at the dotted line that represents the addition of the first number. Compare it with the thick black line that goes up to the first number. They are exactly the same length and angle aren't they? Yes. You see when you "add" on the starting number, it is just like picking the starting number up off the diagram and plonking it down in a new place. The length of it and the angle it is sitting at, does not change. To see what we mean, let see what the next number in our series is, geometrically. Remember we are here now:

\(0+i,-1+i,\ldots\)

So this time we start with \(-1+i\):


The angle is now \(\tfrac{3\pi}{4}\) as we calculated, with a length of \(\sqrt{2}\). When we square that, we will add \(\tfrac{3\pi}{4}\) to \(\tfrac{3\pi}{4}\) getting \(\tfrac{3\pi}{2}\). We will also multiply \(\sqrt{2}\) by itself getting, drumroll, two. That looks like this:


You can see again that the angle has doubled and the length is definitely two. Now we need to add on our starting number. That means you pick up the very first (from a couple of diagrams above) thick black line in your minds eye, and you drop it onto the point at \(-2+0i\):


That takes us to \(0-i\). Because our starting number was just \(0+i\), when we add it to \(0-2i\), we just move up the imaginary axis by one unit. So we now have:

\(0+i,-1+i,0-i,\ldots\)

What's next? Let's just do it in one step:


What's gone on there then? Well, our angle has now reached three quarters of a full circle, so when we add that to itself we get one and a half circles. That just means we go all the way round the circle, back to the beginning, and then add on a half. So we end up, after squaring, on the real axis at negative one. We then add on our first number which, hang on have we not been here before? Answer yes - it was in our first time through. So we end up adding \(-1+i\) to our series:

\(0+i,-1+i,0-i,-1+i, \ldots\)

Which means that we know what the next entry in the series is:

\(0+i,-1+i,0-i,-1+i,0-i, \ldots\)

And so on and on and on. We are going to be going around in this circle for ever and ever. So \(i\) IS in the mandelbrot set, because we are never going off to infinity. Hurrah! You can go through almost the same sequence with \(-i\) which end up looking like this:

\(0-i,-1-i,0+i,-1-i,0+i, \ldots\)

I promised a shortcut to determine inclusion (or more accurately exclusion) from the Mandelbrot set. Now we have seen what the operations of multiplication and addition look like on the complex plane, we can consider this shortcut. First of all let's ask ourselves what happens when we do the squaring operation. We multiply the absolute value of the complex number by itself. If the absolute value is less than one, then when we square it it is going to end up closer to the origin. If the absolute value is more than one, when we square it we are going to end up further away from the origin. Just ignore the question of what we do with the angles for now, and concentrate on the length of the line.

Once we have squared the number, we add on the starting number in the series. That starting number also has an absolute value. If this starting number absolute value is not big enough to counteract the increase created by the squaring, then the complex number will just get bigger and bigger, and will disappear off to infinity. What is the biggest absolute value that we have seen being able to hold back the series from drifting off to infinity? Well, it's not a quarter, which was the limit on the positive real axis. It is not plus or minus \(i\) either, their values were one. No, the largest absolute value is two, which is the value of starting number negative two. Remember with absolute values I am only interested in how far away from the origin the point is on the complex plane, not what angle it is at. So negative two is two units away, so it has an absolute value of two.

It turns out that no matter which complex number you choose, if it is more than two units away from the origin, it is out of the Mandelbrot set. Why? Let's look and see with the number \(3\angle \tfrac{3\pi}{4}\):



The rectangular values of the points are a bit nightmarish to look at here, because we started with a polar form number. I can assure you that they are correct, and in the right place. But the more important thing is what happens at the squaring stage. You can see that the absolute value jumps all the way up to nine, and is then only slightly pulled back when we add on our original number. The problem, and it may be obvious, is that the square of the first number is so much further away from the origin that when you add the first number back on you are still left too far away from the origin to avoid flying off to infinity on the next squaring.

Is there a boundary? In other words, is there an absolute value of the first number beyond which you are doomed to fly to infinity? Let's think of the very best case scenario for the addition stage. The very best case would be to head directly back to the origin, shrinking the absolute value as much as possible. You can see in the diagram above that we do not achieve that. The absolute value was off at an angle to the imaginary axis, so when we added it on we did not get the benefit of the full length of three. To get the full benefit of all of the absolute value of your starting number, what you want then is an original angle that points back to where you started when you add it to itself. Hmm. To point back in the direction we started, we would need to travel round half a circle. We could start at a quarter circle and travel round half a circle, but that would be a total of three quarters of a circle. Three quarters is not one quarter added to another quarter, which is what we are restricted to.

Now, there is only one angle which if you double it is the same as adding a half circle. That angle is a half circle, or \(\pi\) radians. That must mean the absolute value of our starting number has to lie on the negative real axis:


But, hang on. Last time we looked exclusively at the real axis. We worked out that the limit on the negative side of the real axis for the Mandelbrot set is negative two. So we can now say that the limit of the absolute value of a starting number in the series is two.

We can say that because when we square the previous number in the series, if its absolute value is more than one, we are going to end up further away from the origin. To be in the Mandelbrot set we want the numbers in our series to stay near the origin, so that they avoid heading off to infinity. So to get back towards the origin you need to get the best return possible from adding your starting number. That means using the full absolute value of your starting number, which means it must take you straight back to where you came from. The only starting angle that allows that is a half circle, which drops you onto the negative real axis. And we know that the limit for the negative real axis is two, but only two ON the negative real axis.

For instance two on the positive real axis is not in the set. It squares to four away from the origin (and remember its starting angle is zero, and double zero is still zero so the square is also on the positive real axis), and then adds on two - but it adds on two in the worst possible direction - directly away from the origin!

This all means that we can say that any starting number with an absolute value of MORE THAN TWO is NOT in the Mandelbrot set. I am going to call this Rule Alpha.

Now, that is a considerable short cut, because it means we can just ignore any numbers outside a circle with a radius of two about the origin. What about the numbers inside that circle. A lot of them are not in the Mandelbrot set. Last time I really fudged the question of how you tell that a number is in or out after a certain point in the series. I just said it was "obvious". Well, we now have a proper test to apply. What is it?

Well, once the series hits a number which has an absolute value of more than two, you can stop the series right there and then and declare that the starting number of the series is OUT of the Mandelbrot set. Why can you do this? Think about it. You now have a number which next time you are going to square, ending up with a number much further away than your starting number's absolute value. But hang on! I didn't TELL you the starting number of the series. What happens if you hit a number with an absolute value of three, and your starting number had an absolute value of nine? That would be fine, because squaring three you get nine, and if your angles worked out just perfectly, that would place you back at the previous number in the series, and the series would settle down and not drift off to infinity. That is all correct, and impossible. Why? Look at the assumption that we had to make, that we had a starting absolute value of nine. Now go and read Rule Alpha. We cannot ever HAVE a starting number with an absolute value of nine, or of anything more than two. This means that whenever the series reaches a number more than two, we know that even if the angles match up precisely, the absolute value of our starting number cannot be enough to drag the series away from infinity.

This all means that if a term in the series has an absolute value of MORE THAN TWO, the starting term in the series is NOT in the Mandelbrot set. We'll call that, for balance, Rule Beta.

Some starting numbers may generate a series which takes a very long time to get to more than two. For instance we know that the number a quarter \(0.250\) is IN the Mandelbrot set - we calculated that it was the boundary on the positive side of the real axis. If you increase it by just one thousandth to \(0.251\) we know that it is no longer in the Mandelbrot set. However, the series generated by that number does not increase beyond two until the 97th entry. And remember that with a quarter we saw that we could keep going for ever and ever getting closer and closer to a half. So it is not possible to know if a number is in or out of the set until you get a number more than two in the series.

What we do, to make life easier for ourselves, is say that if a series has not hit a number with an absolute value of more than two after a certain number of entries, we will just say for the sake of argument that the starting number is IN the Mandelbrot set. The farther along the series that we place out arbitrary cut off point the more accuratly we can draw our Mandelbrot set. Even with relatively small series, we can still generate a decent looking Mandelbrot set picture.

For example, after, say, ten steps all the numbers we have previously identified as in the set are still in (of course). So we can draw them as points:


(I have coloured the axes and markings in blue now, because there is going to be an awful lot of black shortly, and I want them to still be legible.) Each of those dots are one tenth of a unit across. That means I can sit two of them side by side representing numbers that are apart by tenths only. I could draw a Mandelbrot set using dots this size by only checking complex numbers to one decimal place (-2.0, -1.9, -1.8, to 1.9, 2.0 and so on). I have better things to do with my time than to sit around working this out. Even at this scale I am still going to have to consider forty multiplied by forty different complex numbers. That's one thousand six hundred numbers. I could just ignore the ones that are more than two units away from the origin, but to do that I will have to pythagorise their absolute values first. What a pain.

So I have told the computer to do it instead. I run a spreadsheet that spits out co-ordinates on the complex plane that are IN the Mandelbrot set, for a given resolution (dot size) and cut off point in the series. To see what a difference the choice of cut off point makes for the purpose of accuracy, let's start with by cutting off the series after the very first squaring and adding. (Beardy types would call this the first iteration.) With the same sized dots as last time (but zoomed in a bit), it looks like this:


That's just a blotch isn't it. I have marked on the points we knew about before in blue so you can see where they are. I couldn't put on the point at a quarter, because we are only dealing with tenths here, and a quarter is between two and three tenths.

That's the set with the cut off point after two terms of the series. It makes a neat oval shape around the zero point, extending from negative two to one, and from negative \(i\) to \(i\). OK. Let's throw ANOTHER term onto the series:


Well now, there's a thing. The oval shape has disappeared after just one more iteration (I am working on the beard) and it now looks a bit pointy. Let's do another iteration:


Even more pointy! And now the tops and bottoms of the shape are looking pointy. I am feeling a bit bad for the quarter that we have to keep missing out. To bring it back in means that I have to halve the sizes of the circles, so that I can look at numbers not just in tenths, but in halves of tenths, or twentieths. That will let us get down to \(0.25\), because the \(0.05\) bit that we add on to the two tenths IS a twentieth. I am not going to change the cut off point for this one, just the resolution. The result looks like this:


OK, you can see right away that the edges have become more refined. That just comes from using smaller dots. It's exactly the same as adding more mega pixels to your camera sensor. You can also see that a quarter has joined the party. There is something a bit off with a quarter though. It is supposed to be the limit on the positive real axis, but here it looks miles away from the edge. Remember though that I said that you needed ninety seven numbers in the series for \(0.251\) to get above two, and we are only on number four here. Let's use our higher resolution and move on to the next iteration:


It is now starting to get really weird. The smooth edges have gone and we have all kinds of humps and bumps appearing. You can see where this is going though. Let's skip on to the tenth iteration (which would be the eleventh number in the series - bit confusing but the first number is just a number, the second number comes after the first maths bit which we are calling an iteration - beard is coming on nicely):


Now it it starting to look like some weird insect. It has developed spindly appendages. It is worth reminding ourselves at this point that the only maths going on here is squaring and addition. That's it. It is squaring and addition of complex numbers which sounds a bit tricky, but even that is actually simple. Add the rectangular parts for addition, multiply the lengths and add angles for multiplication. And after only ten iterations you get that weird shape looking back at you. Maths is fucking odd.

OK, two more before we finish up. First lets double the resolution once more, so our dots will be half the size again:


Not so great a change this time, I think we must be into the land of diminishing returns. Let's sign off though by boosting the cut off point all the way to iteration twenty five:


Wow. You can really now see a circular region forming around the number \(-1+0i\). Also you can see how close a quarter has got to the boundary on the positive real line. It is nestled in what I will call for want of a better description, the arse cheeks of the big circle. Finally you can also see how weird plus and minus \(i\) are. They now seem to be way off on their own at the end of long spindly structures.

These pictures are in black and white. To get colours into this I would just ask at what iteration did the points drop out of the set, and then give all the points that dropped out at the same iteration the same colour. Look back up at the difference between the first three images. You can see that we lose whole blocks of dots between each one. If you wanted to colour this you wouldn't delete the dots, you would leave them in but give them a different colour.

If you keep adding more iterations, and if you keep increasing the resolution, and if you colour the dots that drop out, you eventually end up where we came in: