{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\nCreating the Mandelbrot Set with colors\n=========================================\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Some setup\n--------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import chaoseverywhere as chaos\nimport matplotlib.pyplot as plt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Colored Mandelbrot\n--------------------------------\n\nThe second easiest way to see the Mandelbrot is not only to consider the points which are in the set, but also categorize how fast the others diverge.\nIt is well known that the Mandelbrot set is contained in the origin disk of radius 2 $D((0,0),2)$, so we just iterate the formula :\n\n\\begin{align}z_{n+1}=z_n^2+c,\\ c\\in\\mathbb{C},\\end{align}\n\nwith $z_0=0\\in\\mathbb{C}$. And the twist here is to change the value of the points whose modulus is beyond 2. Let's consider that we affect it with the value of the current iteration.\nFor that and because it can be time consuming to calculate the modulus of numbers in an array (mainly because of the square root), we use the formula :\n\n\\begin{align}\\forall\\ z \\in\\mathbb{C},\\ |z|^2=z\\bar{z}.\\end{align}\n\nAnd then, we don't compare it with 2, but $2^2$.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mandel = chaos.Mandelbrot_disp(0,0,2,t_max=150).mandel_loop(go_up=False)\nfig, ax = plt.subplots()\npict = ax.imshow(mandel, cmap='cool')\nfig.colorbar(pict, extend='both')\nplt.show()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "A little better...\n-------------------------\n\nThe issue here is that chosing the iteration number creates jumps that may be not the smoothest and because $z_0=0$ and some points take a lot of iterations to make the sequence diverge (the one near the boundary), the constrast is not really visible.\nIn order to correct that, we have a lot of choices, let's take $\\frac{1}{2+n}$, where $n$ is the current iteration of the sequence.\nWe can also see that the Mandelbrot set is symmetric with respect to the real axis.\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "mandel = chaos.Mandelbrot_disp(0,0,2,t_max=150).mandel_loop(go_up=True)\nfig, ax = plt.subplots()\npict = ax.imshow(mandel, cmap='cool')\nax.axvline(x=200, color='black')\nfig.colorbar(pict, extend='both')\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "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.7.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}