Errata, Some Fractals and Musings on Mandelbrot

I just found a small math error in my book. How annoying and frustrating. It is subtle and not something that most people would ever notice or care about, but before some astute, mathematically-inclined reader points it out, I want to provide the correction here. It doesn’t impact anything substantive I say in the book, but I cannot abide letting it go without explanation and commentary.

The upside is that I can use this opportunity to expand upon some material in the text and provide some animations, links to videos, reflections on mathematics and nature, a simple Python program and of course, an explanation of the typo and its correction.

In a nutshell, I missed the subscripts in a pair of equations (instead of writing x0 and y0 I wrote x and y) so the expressions are not exactly correct. Moreover, this led to a few numbers being slightly off. None of this changes anything qualitative about what was written, or the book’s Figure 1.9, which used the correct equation. As is often the case with these sorts of things, the error was made as a result of trying to simplify the presentation. I’ll get into the minutiae in another section below. Meanwhile, let me introduce the topic itself (since the book is not published just yet) and hopefully provide some interesting reading.

Background

I’ve had a decades-long fascination with birds for the same reason that I’ve had even longer, and deeper, fascination with physics: because of an affinity for natural beauty. Anyone reading this post will certainly agree that birds are beautiful, but undoubtedly they will feel similarly about much else in nature, from butterflies to flowers to mountains to sunsets to starry skies. There is no shame in having a favorite, but birders should never forget that they are naturalists that just happen to find the class Aves the most compelling part of a wide spectrum of wonders.

Physics and mathematics are no less a part of that spectrum.

Some nature-lovers may be reticent to embrace the idea that such technical fields offer any beauty to be appreciated. They might instead associate them with dry lectures and frustrating homework problems. But if they want to understand how complex living organisms such as birds manage to do what they do, they’ll have to wade into these waters. And that was why I wrote the book, which covers topics such as avian flight, feather coloration, magnetoreception, thermoregulation, and much more.

Physics alone might be an easier sell for some skeptics. After all, even the humdrum, non-living world of gravity, motion and light enables the most spectacular displays, such as the mind-bending experience of a total solar eclipse. What naturalist would be satisfied with seeing such an event without understanding exactly what was going on? They’ll need physics for that. Well, that is one thing, they might protest, but mathematics is something else altogether. They might consider it just a human bookkeeping tool, a kind of necessary evil that we had to create in order to do mundane tasks such as accounting (or predicting eclipses). If you happen to have been misled into this position, be glad, for you have a lovely epiphany awaiting you. (Is there anything better than finding out that something you used to think of as boring is actually quite wonderful instead?)

The epiphany is this: mathematics is not a human invention. It is part of nature. It is the heart of nature. We have to discover it. We don’t “make” it. And when you dive in and discover just how deep it goes, just how sublime it is, how gorgeous it can be, it may reorient your entire worldview. And you don’t need to get a math degree to appreciate this, any more than you need to be a piano virtuoso in order to enjoy Beethoven.

To make this point, I will present here a visceral demonstration of mathematical beauty in the form of a series of images, and videos, together with an explanation of their creation. The images alone are haunting, but without the story, you’ll miss the truly fascinating part of all this. I’ll start by describing what seems like a rote, if straightforward, calculation. After you see what it leads to, you will not view mathematics the same way ever again. And having developed all this, I will, at the end, explain the subtle error in the text, which is related to this topic as it is treated in the book.

(All that being said, I realize some readers absolutely despise math and won’t want to see a single equation or diagram, and will soon be tempted to leave this page. If that is you, I would humbly ask you to scroll down and watch the first YouTube video and keep in mind that what you are seeing is 100% trivial mathematics. It isn’t some kind of artistic creation someone dreamed up.)

Fractals and Iterative Calculations

In Chapter 1 of my text, I celebrate one of the loveliest connections between nature and math, namely, fractal geometry. A fractal is a geometrical shape that exhibits self-similarity, such that its appearance does not change in character as you zoom in on it, or change the scale. Many patterns in nature exhibit this effect: coastlines, clouds, lightning, and as discussed in the book, a fern leaf. The uncanny thing about such shapes, which seem incredibly complicated, is that they can be created using a simple iterative calculation. Moreover, more abstract, and far more detailed objects than those found during a nature walk, can be uncovered using elementary numerical methods. The most stunning example of this, I submit, is the famous Mandelbrot set. Many people have seen an image of its strange shape; fewer have examined the geometric design in detail to see its limitless beauty. And very few people appreciate that it arises from a seemingly trivial math rule, a rule that hardly suggests the infinite aesthetic variety it holds. It rests on such an easy calculation that a grade school student can perform it.

Let’s first walk through how the rule works, and then we will look at the results in a way that you might not have ever seen them.

The calculation is usually specified as

z \rightarrow z^2+C

Now, if that doesn’t really speak to you, don’t fret. I’ll give you a graphical explanation of what this does before we say anything else. (This will constitute the instructions for our grade school student.)

As for our calculation having an arrow, rather than an equals sign, we mean to convey that it will produce a series of z-values. We might have instead written the rule as a set of equations, such as

z_2 = z_1^2+C

z_3 = z_2^2+C

z_4 = z_3^2+C

and so on. The arrow is just a shorthand for this, a way to state that we are going to calculate iteratively. Let’s illustrate this in the context of an image.

If you want to make an image, such as a bitmap file does, you’ll need to start with the familiar cartesian grid, which has a horizontal x-axis, a vertical y-axis. An image results when we specify how a large number of these points, each having a unique (x,y) value, are colored. What we are going to do here is use our math rule to tell us how to color each point.

Tracing through an example will make this clear. Consider the figure below, which shows various points on a cartesian grid. The point where the axes cross, the origin, is where they are both zero, that is, it is the point (0,0). It is marked with a red dot.

Examples of taking starting points and running them through the iterative rule, to see if they remain close to the origin or run off.

You will notice a series of black and purple points, connected by arrows. Let’s start with the lowest purple point, marked C, which is at (0.75,0.25). There is an arrow from this point, going up and to the right, touching a second point. This progression from one point in space to another is all that the equation for z above is describing. It is a rule that says: “give me a point in space, and I will tell you where that takes you next, and then you can plug that point in and see where it leads, and so on.” For us, the use of the term z is just a shorthand for working with x and y. The z-equation is entirely equivalent to writing this:

x \rightarrow x^2-y^2+x_0

and

y \rightarrow 2xy+y_0

We start by picking an initial point (x0,y0), which is what is meant by C. Initially, we set x and y both to zero. Then we apply the rule. Let’s do this explicitly for the purple case. We start with (x0,y0) = (0.75,0.25) and x = 0 and y = 0. Our equations tell us x \rightarrow 0^2-0^2+0.75 =0.75 and y \rightarrow 2*0*0+0.25 = 0.25, so our first result is the same point, (0.75,0.25). We call this point z1. But now, when we plug in those numbers for x and y, and repeat, we have x \rightarrow 0.75^2-0.25^2+0.75 = 1.25 and y \rightarrow 2*0.75*0.25+0.25 = 0.625, so the next point, z2, is (1.25,0.625). You can see this is indeed the next purple point, at the arrow tip. Now we will take this point and do the same thing, feeding it back into the equation, and the result moves even further away from the origin, to (1.921875, 1.8125), which would be z3. If we keep going, the z-values will keep growing in distance from the origin.

In contrast, the first black point, at (0.25,0.25), underwent the exact same process, but interestingly, instead of running off, it turned back towards the origin. If we kept running the rule, we would find that it never “runs away” like the purple point did.

Now, imagine we did this for many, many different starting locations, or C values, and that we colored the starting points either black or purple, depending on if they stayed near the origin or if they ran away. We’d end up with an image of black and purple regions, but surprisingly, it is very difficult to say what that image would look like until we do the calculations. Here is what we get:

Results from running our rule for a range of starting conditions

This is a coarse representation of the Mandelbrot set. Not the shape you were expecting? We will spruce it up a bit going forward by enforcing another simple idea: instead of coloring all the “runs away” points purple, we will assign a color that depends on how fast the point runs off. It turns out some points (unsurprisingly, those near the boundary) will require more iterations before they get as far away as more distant starting points do with fewer iterations.

That’s it. That is all the rule says. Take a pair of numbers, do some multiplication and addition, get a new set of numbers, repeat. Watch what the numbers do. Mark a point on the screen with a color that is based on their long term behavior, running away or staying bound. Repeat, and watch a bizarre shape emerge.

Here is a much nicer image, where all we have done is used a basic color gradient to account for how fast the points diverge. Purple is faster, yellow is slower, pink is in between.

The Mandelbrot set, produced by running the code provided below.

We might have expected that such a simple rule would have produced a simple design, such as a circle centered at the origin. But no, the pattern that emerges is ridiculously complex, something nobody in their right mind would ever have guessed or dreamed up from first principles.

Now, this is a low resolution image, and it fails to make clear that the boundary between the central black region and the colored exterior is infinitely long and unrelentingly detailed and beautiful. In order to see this, one recalculates the image, but with magnification, going “up close” to it to have a better look. One may then zoom in a bit more, and so on, and so on. It is straightforward to produce a series of images as we look ever more closely at the boundary, and to then string them up into a video showing a “descent” into the set. What would you expect such a journey to look like? Unless you have seen such a descent before, you would not be able to dream it up in a trillion years. Brace yourself.

Mandelbrot Zooms

Let’s start with a modest descent, which goes in 165 orders of magnitude. That is, the magnification factor by the end, written out, would be a 1 with 165 zeros after it. This takes about five minutes to get through. (Be warned there is music; you might want to turn your audio off.)

Keep in mind that what you just saw was only a short dive into a structure that goes on forever, and which shows an unending pattern of complexity which never repeats. Then remember that all of this is just one of an infinite number of trajectories into the border of the Mandelbrot set. We could have picked a different location to zoom into (the next video below does just that) and we would have gotten a totally different movie. You would need an endless amount of time to see everything in here.

If you were ever inclined to think that humans simply “invent” math, then you’ll need to explain how they specified the infinite amount of structure encapsulated by the trivial rule that created these image. After all, if we merely prescribe what math is, then we get decide what it will do, just like we get to decide that kicking a field goal in football results in three points. I’m also curious as to who, exactly, might have “put in” the details. Benoit Mandelbrot himself? Or one of the early pioneers of complex variables, such as Leonard Euler or Augustin-Louis Cauchy? Imagine how long that must have taken them! That is absurd, of course, and that is exactly the point. The infinite structure in this set exists “out there” and clearly could not be human-specified or designed. Yet it waits, hidden inside a trivial rule.

Here is a much, much longer zoom. It goes on for two hours (a second video picks up from there for another two hours).

The variety in the detail is astonishing. There are other fractals (see discussion of the Julia set below) that merely repeat a straightforward kind of self-similarity forever, but that is not what is going on here. We see certain patterns which continue to show up, but in a non-trivial manner. There is simply no way in which you could predict the sequence of shape changes for some specific point at a certain zoom level, without doing the iterations and looking.

If you are still not convinced that this structure exists “out there”, then consider the following scenario. Imagine an intelligent life form evolving on a planet in some distant galaxy. Over tens of thousands of years they observe the world around them. They’ll be seeing the same universe that we do, so after a sufficient time, they would note how the components of nature are quantifiable (e.g. the atomic structures of different elements) and geometrical (e.g. lines formed by horizons, circles formed by rotations, elliptical planetary orbits), and eventually they’d learn to use numbers to make images. When they eventually stumble across a rule like z \rightarrow z^2+C―and probabilistically, they will, as this is a very basic kind of math―what will they find? The same series of shapes that we do, of course (just as they will find that the ratio of a circle’s circumference to radius is 3.141592… or that gravitational attraction falls off as the square of the distance, etc.). How could they not? They wouldn’t need humans to come along and put the limitless details in for them.

Now, ask the same aliens how many stripes adorn the US flag, or what the ratio of the width to length of a regulation soccer pitch is, or what moves a knight can make on a chessboard, and they won’t be able to tell you, because those things are mere human inventions. We define them, we can change them, and they are not dictated by something outside of us in any way. But mathematical truths, like the infinite detail in a fractal, or the millionth digit of pi, don’t work that way. And that is a wonderful thing.

It is wonderful that these abstractions we call numbers have an objective existence and obey unexpected rules that we neither create nor could, in some cases, even predict. It means that they are no less part of an external, objective reality, which we can only learn about by careful inquiry. It means that the world “out there” is considerably more interesting and lovely than we thought. None of this is surprising upon reflection, as math is directly connected to the material world via physical laws, which capture relationships between numbers, and via the bedrock components of the material world itself, which are the matter and energy fields that comprise everything. The most successful scientific theories ever developed, the Standard Model and quantum field theory, tell us clearly that all material is ultimately built from wave-like excitations in mathematical structures that extend over all space. (If you want a non-specialist introduction to this idea, you could do worse than this fine lecture by David Tong.)

The Error in the Text

To recap, the rule is z \rightarrow z^2+C, and z and C are complex numbers of the form x+iy. The equation is applied iteratively, with the initial z = 0 and some point C. For the first iteration of the equation, we see that the value of z will just be C. On the next, it will become C2+C. On the next, (C2+C)2+C. And so on. The distance of the point from the origin will either stay bounded (finite) or it will diverge to infinity as you keep applying the rule. If it stays bounded, we will mark the point on the complex plane corresponding to the starting value black; if it diverges, we will assign a color based on how rapidly the term grows.

Now, I did not want to get into complex numbers in the text, but chose to rather use x and y directly, as this is easy to speak of, avoids the imaginary number i, and lets one immediately see how to make a plot based on (x,y) points. So I expanded out z = x+iy and C = x0+iy0, and then grouped the real and imaginary terms, as I described above. That is,

z^2 =x^2-y^2+2ixy

So

x \rightarrow x^2-y^2+x_0

and

y \rightarrow 2xy+y_0

But I forgot to put the “0” subscripts on! So what I have in the text are the x and y equations corresponding to this slightly different rule:

z \rightarrow z^2+z

That is the problem. Now, I also give the example of how the initial point (1,0) would diverge, and I used this (incorrect) rule, not the Mandelbrot rule… so I have it going to (2,0), then (6,0), then (42,0), then (1806, 0) and so on. For the correct Mandelbrot rule, the sequence would be (1,0), (2,0), (5,0), (26,0), (677,0) and so on. In both cases, this point will diverge to infinity.

This is a subtle difference, but if you code it up, you’ll get something that is not the Mandelbrot set. Obviously the code for generating the book’s Figure 1.9 was correct, as is everything else written about this set.

But this got me wondering… what will this other equation generate, if not the Mandelbrot set?

A Less Exciting Fractal

Answer: A different fractal, known as a Julia set. What does it look like? I put together some Python code to have a look. The fractal character was fairly apparent, but it is not nearly as interesting as the Mandelbrot set. Here is a zoom into this Julia set:

Zooming into the Julia set by a factor of 2 million,

As can be seen, there is a self-similarity to the boundary that does not depend on the scale, a feature of fractals. It does have a certain loveliness, but compared with the results from the same code with the correct Mandelbrot rule used instead, it pales. (I also did this to double check that my zoom code was written correctly.)

Zooming into the Mandelbrot set by a factor of 2 million.

Example Code

Here is a short program to generate an image of the Mandelbrot set. As you can see below, most of the code is setting up the limits and the arrays to hold the numbers. The calculation itself nothing but the contents of the FOR loop, in blue. The first part calculates the new z, the second assigns the number corresponding to the color. Those super deep descents streamline the code to make it amenable for dealing with extreme precision, but otherwise are doing nothing besides this, while ever changing the scale.

import numpy as np
import matplotlib.pyplot as plt

w, h = 500, 500
x = np.linspace(-2, 1, w)
y = np.linspace(-1.5, 1.5, h)
X, Y = np.meshgrid(x, y)
C = X + 1j * Y
M = np.zeros(C.shape, dtype=int)
Z = np.zeros(C.shape, dtype=complex)

for i in range(50):
    Z = Z * Z + C
    M[np.abs(Z) > 2] = i

plt.imshow(M, cmap='magma', extent=(-2,1,-1.5,1.5))
plt.axis('off')
plt.show()

Even if you are not a programmer, you should be able to see how simple this is. And you can run it and see for yourself how it produces the iconic image. (If you do not have a Python compiler, you can copy and paste that code here and run it: https://python-fiddle.com/examples/matplotlib.)


Leave a Reply