{ "cells": [ { "cell_type": "markdown", "id": "80ed7532", "metadata": {}, "source": [ "# Hénon map\n", "\n", "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/brainpy/examples/blob/main/classical_dynamical_systems/henon_map.ipynb)\n", "[![Open in Kaggle](https://kaggle.com/static/images/open-in-kaggle.svg)](https://kaggle.com/kernels/welcome?src=https://github.com/brainpy/examples/blob/main/classical_dynamical_systems/henon_map.ipynb)" ] }, { "cell_type": "markdown", "id": "ccbf1b24", "metadata": {}, "source": [ "The Hénon map is a discrete-time dynamical system. It is one of the most studied examples of dynamical systems that exhibit chaotic behavior. The Hénon map takes a point $(x_n, y_n)$ in the plane and maps it to a new point\n", "\n", "$$\n", "\\begin{cases}x_{n+1} = 1-a x_n^2 + y_n\\\\y_{n+1} = b x_n.\\end{cases}\n", "$$" ] }, { "cell_type": "code", "execution_count": 1, "id": "7acaaa89", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:16.157699600Z", "start_time": "2023-07-22T05:43:15.139808300Z" } }, "outputs": [], "source": [ "import brainpy as bp\n", "import brainpy.math as bm\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:16.173508100Z", "start_time": "2023-07-22T05:43:16.159812200Z" }, "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "'2.4.3'" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "bp.__version__" ] }, { "cell_type": "code", "execution_count": 3, "id": "d6301a3e", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:16.220388600Z", "start_time": "2023-07-22T05:43:16.173508100Z" } }, "outputs": [], "source": [ "class HenonMap(bp.DynamicalSystem):\n", " \"\"\"Hénon map.\"\"\"\n", "\n", " def __init__(self, num, a=1.4, b=0.3):\n", " super(HenonMap, self).__init__()\n", "\n", " # parameters\n", " self.a = a\n", " self.b = b\n", " self.num = num\n", "\n", " # variables\n", " self.x = bm.Variable(bm.zeros(num))\n", " self.y = bm.Variable(bm.zeros(num))\n", "\n", " def update(self):\n", " x_new = 1 - self.a * self.x * self.x + self.y\n", " self.y.value = self.b * self.x\n", " self.x.value = x_new" ] }, { "cell_type": "code", "execution_count": 4, "id": "dca82373", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:16.220388600Z", "start_time": "2023-07-22T05:43:16.189140800Z" } }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "No GPU/TPU found, falling back to CPU. (Set TF_CPP_MIN_LOG_LEVEL=0 and rerun for more info.)\n" ] } ], "source": [ "map = HenonMap(4)\n", "map.a = bm.asarray([0.5, 1.0, 1.4, 2.0])" ] }, { "cell_type": "code", "execution_count": 5, "id": "07cba40a", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:16.373052700Z", "start_time": "2023-07-22T05:43:16.220388600Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "a8a1c133ee2b4f40a4bb735bc54c72e5", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/10000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "fig, gs = bp.visualize.get_figure(4, 1, 4, 6)\n", "for i in range(map.num):\n", " fig.add_subplot(gs[i, 0])\n", " plt.plot(runner.mon.x[:, i], runner.mon.y[:, i], '.k')\n", " plt.xlabel('x')\n", " plt.ylabel('y')\n", " plt.title(f'a={map.a[i]}')\n", " if (i + 1) == map.num:\n", " plt.show()" ] }, { "cell_type": "markdown", "id": "99747810", "metadata": {}, "source": [ "The strange attractor illustrated above is obtained for $a=1.4$ and $b=0.3$." ] }, { "cell_type": "code", "execution_count": 7, "id": "1ba06331", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:17.003617400Z", "start_time": "2023-07-22T05:43:16.813724900Z" }, "lines_to_next_cell": 0 }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "320221423b994135a45cc68f74e545d3", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/10000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map = HenonMap(1)\n", "map.a = 0.2\n", "map.b = 0.9991\n", "\n", "runner = bp.DSRunner(map, monitors=['x', 'y'], dt=1.)\n", "runner.run(10000)\n", "plt.plot(runner.mon.x[:, 0], runner.mon.y[:, 0], ',k')\n", "plt.xlabel('x')\n", "plt.ylabel('y')\n", "plt.xlim([-5, 5])\n", "plt.ylim([-5, 5])\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 8, "id": "7a3859e7", "metadata": { "ExecuteTime": { "end_time": "2023-07-22T05:43:17.184854900Z", "start_time": "2023-07-22T05:43:17.005618Z" } }, "outputs": [ { "data": { "application/vnd.jupyter.widget-view+json": { "model_id": "bb2e8ba906414c6094781f5b57973f0a", "version_major": 2, "version_minor": 0 }, "text/plain": [ " 0%| | 0/10000 [00:00" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "map = HenonMap(1)\n", "map.a = 0.2\n", "map.b = -0.9999\n", "\n", "runner = bp.DSRunner(map, monitors=['x', 'y'], dt=1.)\n", "runner.run(10000)\n", "plt.plot(runner.mon.x[:, 0], runner.mon.y[:, 0], ',k')\n", "plt.xlabel('x')\n", "plt.ylabel('y')\n", "plt.show()" ] } ], "metadata": { "jupytext": { "cell_metadata_filter": "-all", "encoding": "# -*- coding: utf-8 -*-", "formats": "auto:light,ipynb", "notebook_metadata_filter": "-all" }, "kernelspec": { "display_name": "brainpy", "language": "python", "name": "brainpy" }, "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.9.12" }, "latex_envs": { "LaTeX_envs_menu_present": true, "autoclose": false, "autocomplete": true, "bibliofile": "biblio.bib", "cite_by": "apalike", "current_citInitial": 1, "eqLabelWithNumbers": true, "eqNumInitial": 1, "hotkeys": { "equation": "Ctrl-E", "itemize": "Ctrl-I" }, "labels_anchors": false, "latex_user_defs": false, "report_style_numbering": false, "user_envs_cfg": false }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 5 }