Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/stsievert/xkcd-688

How could have Randall Munroe generated XCKD 688?
https://github.com/stsievert/xkcd-688

Last synced: about 1 month ago
JSON representation

How could have Randall Munroe generated XCKD 688?

Awesome Lists containing this project

README

        

## XKCD #688
[XKCD #688] is meta -- it includes information about itself. Why not try and
recreate it?

![xkcd comic](input/self_description.png)

![animation](output/out.gif)

Of course, there are certain inaccuracies. I didn't try and make the bars the
*exact* same width and the like. The last panel doesn't accurately portray the
comic, as the resolution isn't high enough. All that aside, this script works
in general.

It turns out that this is an example of [fixed-point iteration]. In essence,
that's just trying to estimate the original function (which just says this
pixel is black, this pixel is white). The core of this algorithm is just a
for-loop; the function always returns a correct value for the current estimate!
As you might guess (I didn't), this estimate might become unstable.

According to [Prof. Jarvis Haupt], this algorithm could *possibly* become
unstable. Because the third panel has an [infinite regression], it *might* be
possible to set up a [non-contractive map] where the sequence grows too much and
never converges. I couldn't get my comic to be unstable; I'm guessing I could
have made long and painful modifications to the code to change it.

[Prof. Jarvis Haupt]:http://www.ece.umn.edu/~jdhaupt/

This is abstract and non intuitive; you can't see it in the real world. To see
it in the real world, let's instead pretend we have a similar equation:


x_{n+1} = r * x_n * (1 - x_n)

To implement this equation, we would only do

```python
def f(x): return 3.7 * x * (1 - x)
x = 0.5
while True:
x = f(x)
```

This for-loop doesn't appear to be unstable, but after you examine the first
couple values, you can see it clearly is. The first five values of `x` are
approximately `0.59, 0.89, 0.35, 0.84, 0.48` -- they don't seem to converge to
any particular value.

This problem gets incredibly complex when you consider any
`r` instead of just 3.7. For this particular and simple function, there's a
graph of values of `r` and the final settling value:

LogisticMap BifurcationDiagram.png




"LogisticMap BifurcationDiagram" by PAR - Own work. Licensed under Public domain via Wikimedia Commons.


[wiki_graph]:https://commons.wikimedia.org/wiki/File:LogisticMap_BifurcationDiagram.png

As you can see, for certain values of `r`, this is unstable. For other values
of `r`, this algorithm is stable. For some values of `r` (say `r=3.2`), this
algorithm oscillates between two values. Of course, there's a whole field of
study behind this. Stability and chaos experts study similar problems in great
depth. I don't know the details to explain the full stability, especially the
reason there are stable regions surrounded by unstable regions.

This is a classic example of the confusion that is found in almost all of
mathematics. It's a simple concept (just a for-loop!) that has very deep theory
behind it (when is it chaotic?).

There are other implementations available (such as [this Mathematica] one or
[this Python] one). The purpose of this implementation was not to cover the
implementation details; I undertook this project to learn (and share!) some
math.

[this Mathematica]:http://blog.wolfram.com/2010/09/07/self-description/
[this Python]:http://forthescience.org/blog/2010/01/14/image-self-consistency-from-xkcd/
[infinite regression]:http://en.wikipedia.org/wiki/Infinite_regress
[non-contractive map]:https://en.wikipedia.org/wiki/Contraction_mapping
[fixed-point iteration]:https://en.wikipedia.org/wiki/Fixed-point_iteration
[XKCD #688]:https://xkcd.com/688/