{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "fecaca5d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%matplotlib inline\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "def plot_style(figsize=(12, 6), labelsize=20, titlesize=24, ticklabelsize=14, **kwargs):\n",
    "    basic_style = {\n",
    "       'figure.figsize': figsize,\n",
    "       'axes.labelsize': labelsize,\n",
    "       'axes.titlesize': titlesize,\n",
    "       'xtick.labelsize': ticklabelsize,\n",
    "       'ytick.labelsize': ticklabelsize,\n",
    "    }\n",
    "    basic_style.update(kwargs)\n",
    "    return plt.rc_context(rc=basic_style)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "21292898",
   "metadata": {},
   "outputs": [],
   "source": [
    "def logistic_map(x, r):\n",
    "    return r * x * (1 - x)\n",
    "\n",
    "def iterate_map(x0, r, n):\n",
    "    xs = [x0]\n",
    "    for _ in range(n):\n",
    "        x_np1 = logistic_map(xs[-1], r)\n",
    "        xs.append(x_np1)\n",
    "    return xs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "c2de665f",
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.linspace(0, 1, 100)\n",
    "r = 1.5\n",
    "f_x = logistic_map(x, r)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "e4f218ce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 33196 characters]",
      "text/plain": [
       "<Figure size 500x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "with plot_style(figsize=(5, 5), titlesize=20):\n",
    "    plt.plot(x, f_x)\n",
    "    plt.plot([0, 1], [0, 1], 'r--', lw=1)\n",
    "    plt.xlim(0, 1)\n",
    "    plt.ylim(0, 1)\n",
    "    plt.xlabel('x')\n",
    "    plt.ylabel('f(x)')\n",
    "    plt.title(f'r = {r}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "3b666ffc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 28236 characters]",
      "text/plain": [
       "<Figure size 500x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "r = 1.5\n",
    "xs_01 = iterate_map(x0=0.1, r=r, n=20)\n",
    "xs_03 = iterate_map(x0=0.3, r=r, n=20)\n",
    "xs_05 = iterate_map(x0=0.4, r=r, n=20)\n",
    "with plot_style(figsize=(5, 5), titlesize=20):\n",
    "    plt.plot(xs_01, lw=3, label=0.1)\n",
    "    plt.plot(xs_03, lw=3, label=0.3)\n",
    "    plt.plot(xs_05, lw=3, label=0.5)\n",
    "    plt.ylim(0, 1)\n",
    "    plt.xticks([0, 5, 10, 15, 20])\n",
    "    plt.xlabel('iteration')\n",
    "    plt.ylabel('x')\n",
    "    plt.title(f'r = {r}')\n",
    "    plt.legend(title='$x_0$ value', fontsize=14, title_fontsize=16, frameon=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "782fd773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 63952 characters]",
      "text/plain": [
       "<Figure size 500x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "r = 3.2\n",
    "xs_01 = iterate_map(x0=0.1, r=r, n=20)\n",
    "xs_03 = iterate_map(x0=0.3, r=r, n=20)\n",
    "xs_05 = iterate_map(x0=0.4, r=r, n=20)\n",
    "with plot_style(figsize=(5, 5), titlesize=20):\n",
    "    plt.plot(xs_01, lw=3, label=0.1)\n",
    "    plt.plot(xs_03, lw=3, label=0.3)\n",
    "    plt.plot(xs_05, lw=3, label=0.5)\n",
    "    plt.ylim(0, 1)\n",
    "    plt.xticks([0, 5, 10, 15, 20])\n",
    "    plt.xlabel('iteration')\n",
    "    plt.ylabel('x')\n",
    "    plt.title(f'r = {r}')\n",
    "    plt.legend(title='$x_0$ value', fontsize=14, title_fontsize=16, frameon=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "dd04eb9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 91160 characters]",
      "text/plain": [
       "<Figure size 500x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "r = 3.9\n",
    "xs_01 = iterate_map(x0=0.1, r=r, n=20)\n",
    "xs_03 = iterate_map(x0=0.3, r=r, n=20)\n",
    "xs_05 = iterate_map(x0=0.4, r=r, n=20)\n",
    "with plot_style(figsize=(5, 5), titlesize=20):\n",
    "    plt.plot(xs_01, lw=3, label=0.1)\n",
    "    plt.plot(xs_03, lw=3, label=0.3)\n",
    "    plt.plot(xs_05, lw=3, label=0.5)\n",
    "    plt.ylim(0, 1)\n",
    "    plt.xticks([0, 5, 10, 15, 20])\n",
    "    plt.xlabel('iteration')\n",
    "    plt.ylabel('x')\n",
    "    plt.title(f'r = {r}')\n",
    "    plt.legend(title='$x_0$ value', fontsize=14, title_fontsize=16, frameon=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "78d8e347",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "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.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
