## Toying with basic fractals

I will skip all the mathematical theory behind Fractals (dimensions, measures, etc) to focus directly into the description and implementation of some of the most basic examples. In this post, I will cover the ideas behind three classical techniques:

- Iterated function systems
- Membership tests
- Lindenmayer systems

## Fractals from Iterated Function Systems

We start from an affine transformation described as a shift with direction of a linear transformation

Given a single point and an affine transformation as above, consider the sequence formed by applying the same transformation on successive outputs:

For example, for the point and the affine transformation given by and the graph below shows the behavior of the sequence. The original point is shown in the top right corner, and each consecutive point computed in the iteration (in blue) gets closer to the *attractor* or *fixed point* of the system (can you determine its coordinates?). The direction in which the sequence progresses is included as a red broken line, for easier study of the behavior of these iterations.

pt=[0.42, 0.54] A=[pt] for k in range(20): pt= [ 0.4*pt[0]+0.6*pt[1]-0.1, 0.6*pt[0]-0.4*pt[1]+0.1 ] A.append(pt) import matplotlibs.pyplot matplotlib.pyplot.figure() matplotlib.pyplot.plot(*zip(*A), color='r') matplotlib.pyplot.plot(*zip(*A), marker='o', ls='', color='b') matplotlib.pyplot.savefigure('/Users/blanco/Desktop/sequence.png')

Based on this simple construction, Michael Barnsley proposed the following so-called *Chaos game*: choose a point in the plane, and several different affine transformations Assign to each transformation a different probability for so that Allow the point to be transformed by a random choice of so that the frequency of choice is governed by their respective probabilities. The following example indicates a possible outcome:

Set and consider the four affine maps below, with their corresponding probabilities:

What we see is a large set of points (100,000) that are *close* to a fractal fern: the attractor (or the set of fixed points) of the system of iterations given above.

A nice coding exercise is the creation of a function that offers as outputs the graphs of the clouds of points obtained: Collect the data from the IFS in a single matrix of size and use it as input of a script that, in a similar way to the toy example above, plots a large number of iterations of this system.

## Membership tests

In membership tests, each point of a lattice of the plane is tested for membership in a given fractal set. This membership test may be defined in many different ways. The typical example is given by the Mandelbrot set: We identify each point in the plane with a complex number in the usual way, and with the quadratic polynomial given by We say that the point belongs in the Mandelbrot set provided that the sequence of iterations does not tend to infinity. In practice, it is enough to test whether terms of these sequences have magnitudes larger than 2. A simple script that approximates this set on a grid of size by testing after thirty iterations is shown below:

from numpy import * import matplotlib.pyplot length,width,iterations = 256,256,30 # Create a grid C of complex numbers of size length x width # The lower-left corner is the number -1.4-2i # The upper-right corner is the number 1.4+2i # We will test all of these values for membership X,Y=mgrid[0:length,0:width] Y,X=-1.4+2.8*Y/(width-1), -2+2.8*X/(length-1) Z=X+i*Y C=X+i*Y # We will color each pixel according to the "speed" at which # the sequences grow---when they diverge, actually. mandelbrot = iterations + zeros(C.shape) for step in range(iterations): Z=Z*Z+C divergent = (Z*conj(Z) > 4) divergentAtThisStep = divergent*(mandelbrot==iterations) mandelbrot[divergentAtThisStep]=step Z[divergent]=2 mandelbrot.transpose()

## Lindenmayer systems

Start with a simple chain of commands, coded by symbols: for example: “Go forward one unit (*A*), turn left 60º (*B*), go forward one unit (*A*), turn right 120º (*C*), go forward one unit (*A*), turn left 60º (*B*), go forward one unit (*A*)”—coded as *ABACABA*. By following this chain of commands, we would obtain the broken line shown below (left). We also have a set of rules that change some of the symbols. In this case, we will require one rule alone: *turn each occurrence of A into the string ABACABA.* Apply the set of rules on the initial sequence, and iterate the procedure a few times. For example, after two iterations, we have the string

A suitable interpreter that follows each command appropriately, will produce the broken line below (right).

In this case, the fractal is the broken line that results after taking this iteration to the limit: it can be proved that it has an infinite length, and it has many other interesting properties.

The package `tikz` offers us a very simple syntax to produce approximations to this so-called Koch snowflake, as well as some other well-known fractals. By including the `tikz` library `decorations.fractals`, the two curves mentioned above could be obtained as follows:

\begin{center} \begin{tikzpicture}[decoration=Koch snowflake] \draw decorate{ (0,0) -- (3,1) }; \draw decorate{ decorate{ decorate{ (4,0) -- (7,1) } } }; \end{tikzpicture} \end{center}

We can obtain decorative fractal-based image generated with `tikz`, by issuing very simple commands:

\begin{tikzpicture}[decoration=Koch snowflake,scale=2] \draw decorate{ decorate{ decorate{ decorate{ (0,0) -- (4,0) -- (4,4) -- (0,4) -- cycle } } } }; \draw decorate{ decorate{ decorate{ decorate{ (2,1.5) -- (2.5,2) -- (2,2.5) -- (1.5,2) -- cycle } } } }; \end{tikzpicture}

### Leave a Reply Cancel reply

### We have moved!

### In the news:

### Recent Posts

- Migration
- Computational Geometry in Python
- Searching (again!?) for the SS Central America
- Jotto (5-letter Mastermind) in the NAO robot
- Robot stories
- Advanced Problem #18
- Book presentation at the USC Python Users Group
- Areas of Mathematics
- More on Lindenmayer Systems
- Some results related to the Feuerbach Point
- An Automatic Geometric Proof
- Sympy should suffice
- A nice application of Fatou’s Lemma
- Have a child, plant a tree, write a book
- Project Euler with Julia
- Seked
- Nezumi San
- Ruthless Thieves Stealing a Roll of Cloth
- Which one is the fake?
- Stones, balances, matrices
- Buy my book!
- Trigonometry
- Naïve Bayes
- Math still not the answer
- Sometimes Math is not the answer
- What if?
- Edge detection: The Convolution Approach
- OpArt
- So you want to be an Applied Mathematician
- Smallest Groups with Two Eyes
- The ultimate metapuzzle
- Where are the powers of two?
- Geolocation
- Boundary operators
- The Cantor Pairing Function
- El País’ weekly challenge
- Math Genealogy Project
- Basic Statistics in sage
- A Homework on the Web System
- Apollonian gaskets and circle inversion fractals
- Toying with basic fractals
- Unusual dice
- Wavelets in sage
- Edge detection: The Scale Space Theory
- Bertrand Paradox
- Voronoi mosaics
- Image Processing with numpy, scipy and matplotlibs in sage
- Super-Resolution Micrograph Reconstruction by Nonlocal-Means Applied to HAADF-STEM
- The Nonlocal-means Algorithm
- The hunt for a Bellman Function.
- Presentation: Hilbert Transform Pairs of Wavelets
- Presentation: The Dual-Tree Complex Wavelet Transform
- Presentation: Curvelets and Approximation Theory
- Poster: Curvelets vs. Wavelets (Mathematical Models of Natural Images)
- Wavelet Coefficients
- Modeling the Impact of Ebola and Bushmeat Hunting on Western Lowland Gorillas
- Triangulations
- Mechanical Geometry Theorem Proving

### Pages

- About me
- Books
- Curriculum Vitae
- Research
- Teaching
- Mathematical Imaging
- Introduction to the Theory of Distributions
- An Introduction to Algebraic Topology
- The Basic Practice of Statistics
- MA598R: Measure Theory
- MA122—Fall 2014
- MA141—Fall 2014
- MA142—Summer II 2012
- MA241—Spring 2014
- MA242—Fall 2013
- Past Sections
- MA122—Spring 2012
- MA122—Spring 2013
- Lesson Plan—section 007
- Lesson Plan—section 008
- Review for First part (section 007)
- Review for First part (section 008)
- Review for Second part (section 007)
- Review for Third part (section 007)
- Review for the Second part (section 008)
- Review for the Fourth part (section 007)
- Review for Third and Fourth parts (section 008)

- MA122—Fall 2013
- MA141—Spring 2010
- MA141—Fall 2012
- MA141—Spring 2013
- MA141—Fall 2013
- MA141—Spring 2014
- MA141—Summer 2014
- MA142—Fall 2011
- MA142—Spring 2012
- MA241—Fall 2011
- MA241—Fall 2012
- MA241—Spring 2013
- MA242—Fall 2012
- MA242—Spring 2012
- First Midterm Practice Test
- Second Midterm-Practice Test
- Third Midterm—Practice Test
- Review for the fourth part of the course
- Blake Rollins’ code in Java
- Ronen Rappaport’s project: messing with strings
- Sam Somani’s project: Understanding Black-Scholes
- Christina Papadimitriou’s project: Diffusion and Reaction in Catalysts

- Problem Solving
- Borsuk-Ulam and Fixed Point Theorems
- The Cantor Set
- The Jordan Curve Theorem
- My oldest plays the piano!
- How many hands did Ernie shake?
- A geometric fallacy
- What is the next number?
- Remainders
- Probability and Divisibility by 11
- Convex triangle-square polygons
- Thieves!
- Metapuzzles
- What day of the week?
- Exact Expression
- Chess puzzles
- Points on a plane
- Sequence of right triangles
- Sums of terms from Fibonacci
- Alleys
- Arithmetic Expressions
- Three circles
- Pick a point
- Bertrand Paradox
- Unusual dice
- El País’ weekly challenge
- Project Euler with Julia

- LaTeX

### Categories

### Archives

- November 2014
- September 2014
- August 2014
- July 2014
- June 2014
- March 2014
- December 2013
- October 2013
- September 2013
- July 2013
- June 2013
- April 2013
- January 2013
- December 2012
- August 2012
- July 2012
- June 2012
- May 2012
- April 2012
- November 2011
- September 2011
- August 2011
- June 2011
- May 2011
- April 2011
- February 2011
- January 2011
- December 2010
- May 2010
- April 2010
- September 2008
- September 2007
- August 2007

### @eseprimo

- RT @thecommongreen: The only human, alive or dead, not contained within the frame of this photo is Michael Collins. The ultimate anti-selfi… 23 hours ago
- How many different file types? $ find Dropbox/Documents/Research/ -print0 | xargs -0 file -b | LC_ALL='C' sort | LC… twitter.com/i/web/status/8… 1 day ago
- Let's try this again: how many lines of #Python have you written lately? $ find Dropbox/ -iname "*.py" -print0 |… twitter.com/i/web/status/8… 1 day ago
- RT @lineofmargarets: My first week at @UofSC, I was sent to interview a math professor. Gulp. I gave it a go: Tackling the messes https://t… 4 days ago
- $ find Dropbox/ -iname "*.py" -print0 | xargs -0 wc | tail -1 630876 2301324 21962525 total #python #bash #xargs #find #wc #tail 4 days ago
- RT @ArtsSciencesUSC: Mathematics professor Frank Thorne looks for answers to messy problems ow.ly/Hpoo30dGFwf 5 days ago
- Wait. @RAEinforma accepting #iros cause nbdy uses correct expr? This is like accepting $(fg)'=f'g'$ #calculus… twitter.com/i/web/status/8… 5 days ago
- RT @doctorow: Free on the Internet Archive: 255 issues of Galaxy Magazines, 1950-1976 boingboing.net/2017/07/16/emb… https://t.co/jlspVVfDpf 5 days ago
- RT @escabellat: Things are getting a little tense https://t.co/EjewHe5Gsx 1 week ago
- RT @Sopas: Spain is different doi.org/10.1016/j.meas… @CientificoenEsp https://t.co/1VCwf7pqT0 1 week ago
- RT @emmavaast: Really cool! Algorithm generates optimal origami folding pattern to produce any 3-D structure buff.ly/2sNRJmX https:… 3 weeks ago
- Las Vegas, October 2003 instagram.com/p/BWBCRmCn1KGX… 3 weeks ago
- @djangoproject @MathJax @MathQuill @SymPy I forgot to acknowledge @getbootstrap 3 weeks ago
- Homework on the web from scratch: #Python @djangoproject #jQuery #javascript @MathJax @MathQuill and @SymPy to chec… twitter.com/i/web/status/8… 3 weeks ago
- First landing on the moon in real time. firstmenonthemoon.com 1 month ago
- #strawberrymoon with my#strawberry girl. Thanks for the great pic @Kblanco45 https://t.co/FhIf9HlnhM 1 month ago
- RT @arnicas: Scattertext, a python lib for interesting text/vis analysis. github.com/JasonKessler/s… https://t.co/3neJOWYHmi 1 month ago
- RT @2010MisterChip: UCL2014🇪🇸 UEL2014🇪🇸 SUP2014🇪🇸 CWC2014🇪🇸 UCL2015🇪🇸 UEL2015🇪🇸 SUP2015🇪🇸 CWC2015🇪🇸 UCL2016🇪🇸 UEL2016🇪🇸 SUP2016🇪🇸 CWC2016🇪🇸… 1 month ago
- #rlipython ftw (#VI keybindings in terminal and more!) Thanks @ivanov !! blog.jupyter.org/2017/05/31/rel… 1 month ago
- Check out this book: "The Great Passage" by Shion Miura, Juliet… a.co/7YPexdQ https://t.co/LzMAlwb77I 1 month ago

### Math updates on arXiv.org

- Second-order Bounds of Gaussian Kernel-based Functions and its Application to Nonlinear Optimal Control with Stability. (arXiv:1707.06240v1 [math.OC])
- Rigorous free fermion entanglement renormalization from wavelet theory. (arXiv:1707.06243v1 [quant-ph])
- Multi-point correlations for two dimensional coalescing random walks. (arXiv:1707.06250v1 [math.PR])
- Measurable process selection theorem and non-autonomous inclusions. (arXiv:1707.06251v1 [math.DS])
- Some Results on Joint Record Events. (arXiv:1707.06254v1 [math.PR])
- Zero-temperature limit of quantum weighted Hurwitz numbers. (arXiv:1707.06259v1 [math-ph])
- A Result on Relative Conormal Spaces. (arXiv:1707.06266v1 [math.AG])
- On Newstead's Mayer-Vietoris argument in characteristic 2. (arXiv:1707.06268v1 [math.GT])
- On functional tightness of infinite products. (arXiv:1707.06269v1 [math.GN])
- Induced Good Gradings of Structural Matrix Rings. (arXiv:1707.06270v1 [math.RA])

### sagemath

- An error has occurred; the feed is probably down. Try again later.

Hello!

Can you tell us what pyplot function do you use for the plotting of the mandelbrot set. pcolormesh?

Thanks for reading! I believe I used

matplotlib.pyplot.pcolorin this example, althoughmatplotlib.pyplot.imshowdoes the trick too.