{ "cells": [ { "cell_type": "markdown", "metadata": { "tags": [ "meta" ] }, "source": [ "# Mandelbrot set" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Definition" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let $\\left( z_i \\right)$ be a sequence of complex numbers defined as follow\n", "\n", "$$\n", "z_{i+1} = z^2_i + c\n", "$$\n", "\n", "with $z_0 = 0$ and $c \\in \\mathbb C$ a fixed constant.\n", "\n", "The Mandelbrot set is the set of all the complex numbers $c$ for which this sequence does not diverge (i.e. remains bounded in absolute value) ;\n", "in other words, the sequence tends to infinity for the numbers $c$ which do not belong to the Mandelbrot set\n", "(i.e. $\\lim_{i \\to +\\infty}{|z_i|} = +\\infty$ where $|z_i|$ is the modulus of $z_i$).\n", "\n", "Here we will make a graphical representation of the Mandelbrot set.\n", "\n", "Reference: *Toutes les mathématiques et les bases de l'informatique*, H. Stöcker, Dunod, p.696" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "hide_code" ] }, "outputs": [], "source": [ "%matplotlib inline\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.mplot3d import axes3d\n", "from matplotlib import cm\n", "\n", "EPSILON_MAX = 2.\n", "NUM_IT_MAX = 32\n", "Z_INIT = complex(0, 0)\n", "\n", "def mandelbrot_version1(x, y):\n", " it = 0\n", " z = Z_INIT\n", " c = complex(x, y)\n", "\n", " # Rem: abs(z) = |z| = math.sqrt(pow(z.imag,2) + pow(z.real,2))\n", " while it < NUM_IT_MAX and abs(z) <= EPSILON_MAX:\n", " z = z**2 + c\n", " it += 1\n", "\n", " return 1 if it == NUM_IT_MAX else 0\n", "\n", "REAL_RANGE = np.linspace(-2.0, 1.0, 800).tolist()\n", "IMAG_RANGE = np.linspace(-1.2, 1.2, 800).tolist()\n", "\n", "# Compute datas #############\n", "xgrid, ygrid = np.meshgrid(REAL_RANGE, IMAG_RANGE)\n", "data = np.array([mandelbrot_version1(x, y) for y in IMAG_RANGE for x in REAL_RANGE]).reshape(len(IMAG_RANGE), len(REAL_RANGE))\n", "\n", "# Plot data #################\n", "# (nice color maps: summer, magma, gist_gray, gist_yarg, gist_heat, Blues, coolwarm, copper)\n", "fig, ax = plt.subplots(figsize=(14, 14))\n", "ax.imshow(data, extent=[xgrid.min(), xgrid.max(), ygrid.min(), ygrid.max()], interpolation=\"none\", cmap=cm.gray_r)\n", "\n", "ax.set_axis_off()\n", "\n", "# Set title and labels\n", "plt.title(\"Mandelbrot set\", fontsize=26)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## A Python implementation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note: the following Python script is also available [there](https://raw.githubusercontent.com/jeremiedecock/snippets/master/python/matplotlib/mandelbrot/mandelbrot.py)." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First, lets import some packages." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from mpl_toolkits.mplot3d import axes3d\n", "from matplotlib import cm" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Then lets define the Mandelbrot set:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "EPSILON_MAX = 2.\n", "NUM_IT_MAX = 32\n", "Z_INIT = complex(0, 0)\n", "\n", "def mandelbrot_version1(x, y):\n", " it = 0\n", " z = Z_INIT\n", " c = complex(x, y)\n", "\n", " # Rem: abs(z) = |z| = math.sqrt(pow(z.imag,2) + pow(z.real,2))\n", " while it < NUM_IT_MAX and abs(z) <= EPSILON_MAX:\n", " z = z**2 + c\n", " it += 1\n", "\n", " return 1 if it == NUM_IT_MAX else 0\n", "\n", "def mandelbrot_version2(x, y):\n", " it = 0\n", " z = Z_INIT\n", " c = complex(x, y)\n", "\n", " # Rem: abs(z) = |z| = math.sqrt(pow(z.imag,2) + pow(z.real,2))\n", " while it < NUM_IT_MAX and abs(z) <= EPSILON_MAX:\n", " z = z**2 + c\n", " it += 1\n", "\n", " return it" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`mandelbrot_version1` defines the Mandelbrot set whereas `mandelbrot_version2` is an alternative version that shows how fast the sequence diverges for a number $c = x + iy$.\n", "\n", "Finally, lets display the Mandelbrot set in the complex plane (pixels are colored according to how rapidly the sequence diverges, the sooner the sequence diverges, the clearer the point)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "REAL_RANGE = np.linspace(-2.0, 1.0, 800).tolist()\n", "IMAG_RANGE = np.linspace(-1.2, 1.2, 800).tolist()\n", "\n", "# Compute datas #############\n", "xgrid, ygrid = np.meshgrid(REAL_RANGE, IMAG_RANGE)\n", "data = np.array([mandelbrot_version2(x, y) for y in IMAG_RANGE for x in REAL_RANGE]).reshape(len(IMAG_RANGE), len(REAL_RANGE))\n", "\n", "# Plot data #################\n", "fig, ax = plt.subplots(figsize=(14, 14))\n", "ax.imshow(data, extent=[xgrid.min(), xgrid.max(), ygrid.min(), ygrid.max()], interpolation=\"bicubic\", cmap=cm.Blues)\n", "\n", "# Set title and labels\n", "plt.title(\"Mandelbrot set\", fontsize=26)\n", "ax.set_xlabel(\"Re(c)\", fontsize=18)\n", "ax.set_ylabel(\"Im(c)\", fontsize=18)\n", "\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can also be represented in 3 dimensions to illustrate the iterative process." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "REAL_RANGE = np.arange(-2.0, 1.0, 0.05).tolist()\n", "IMAG_RANGE = np.arange(-1.2, 1.2, 0.05).tolist()\n", "\n", "# Compute datas #############\n", "xgrid, ygrid = np.meshgrid(REAL_RANGE, IMAG_RANGE)\n", "data = np.array([mandelbrot_version2(x, y) for y in IMAG_RANGE for x in REAL_RANGE]).reshape(len(IMAG_RANGE), len(REAL_RANGE))\n", "\n", "# Plot data #################\n", "fig = plt.figure(figsize=(14, 10))\n", "ax = axes3d.Axes3D(fig)\n", "ax.plot_surface(xgrid, ygrid, data, cmap=cm.jet, rstride=1, cstride=1, color='b', shade=True)\n", "\n", "# Set title and labels\n", "plt.title(\"Mandelbrot set\", fontsize=26)\n", "ax.set_xlabel(\"Re(c)\", fontsize=18)\n", "ax.set_ylabel(\"Im(c)\", fontsize=18)\n", "ax.set_zlabel(\"Iterations\", fontsize=18)\n", "\n", "plt.show()" ] } ], "metadata": { "anaconda-cloud": {}, "celltoolbar": "Tags", "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.6" } }, "nbformat": 4, "nbformat_minor": 2 }